I got little problem with Camera.Direction.
When i was make minimap, i use radians for rotation of TCastleTransform.
It seems like:

``````StartingAngle := -Pi * 0.25 * Player.LookTo;
Map.MapCanvas.Rotation := Vector4(0, 0, 1, StartingAngle + RDir * Pi * 0.25);
Map.MapFlexible.Rotation := Vector4(0, 0, 1, StartingAngle + RDir * Pi * 0.25);
Map.Rotation := False;
``````

, where LookTo is a number 0, 1, 2…, 7. Zero is a look to the north, 1 - to the north-east and seven - north-west; RDir - rotation direction, 1 or -1 (in real this code bigger for smooth rotation, but this is principal part to set position).
So, when i multiply 45 deegres (Pi*0.25) to 0-7, i get angle of rotation. It works nice with minimap TCastleTransforms.
But it work wrong with camera in 3D scene: only .LookTo := 7, 1 or 0 can set camera to the right position, other values don’t move they. I observe to .LookTo and radians calculation in console, howewer they are ok. Moreover, minimap use this variables too and work like they must, but camera freezed between .LookTo 1 and 7.
I maked simple procedure for set camera inside .Update:

``````procedure CameraRotation(var Viewport: TCastleViewport; var Player: THero);
begin
Viewport.Camera.Direction := Vector3(-Pi * 0.25 * Player.LookTo, 0, 0);
end;
``````

This is results in console: (.LookTo / radians; Camera doesn’t change position between 1 and 7):

0
0.00000000000000000000E+0000
7
5.49778714378213816723E+0000
6
4.71238898038468985788E+0000
5
3.92699081698724154809E+0000
4
3.14159265358979323851E+0000
3
2.35619449019234492894E+0000
2
1.57079632679489661926E+0000
1
7.85398163397448309628E-0001
0
0.00000000000000000000E+0000
7
5.49778714378213816723E+0000
0
0.00000000000000000000E+0000
1
7.85398163397448309628E-0001
2
1.57079632679489661926E+0000
3
2.35619449019234492894E+0000
4
3.14159265358979323851E+0000
5
3.92699081698724154809E+0000
6
4.71238898038468985788E+0000
7
5.49778714378213816723E+0000

This isn’t the way to apply your `LookTo` value.

The `Camera.Direction` (actually TCastleTransform.Direction) is a direction along which the camera is looking at.

Rotate using Rotation. The rotation is performed around the Center point. The rotation may be alternatively controlled using the Direction and Up vectors.

See also Direction docs: Castle Game Engine: CastleTransform: Class TCastleTransform . In particular,

The Direction and Up vectors should always be normalized (have length 1). When setting them by these properties, we will normalize them automatically.

So all the instructions doing

``````Camera.Direction := Vector3(something, 0, 0)
``````

are really equivalent to just

``````Camera.Direction := Vector3(1, 0, 0)
``````

To set the direction like you want:

1. Calculate direction x and y components from your angle. Similar to what we shown in other thread ( Calculation of objects positions with using sine and cosine - #2 by eugeneloza ) – use SinCos on the angle derived from `LookTo` (like `StartingAngle`), sinus goes to X, cosinus goes to Y.

2. Or, set `Camera.Rotation`, and then you can rotate it quite similarly to what you do with `Map.MapCanvas.Rotation` you show.

1 Like

Thanks.
Sometimes i incorrectly understand documentation… Must take more time to English learning
So, i think to use .Rotation to camera, because it will allow use same procedure for two Viewports - minimap and environment observer… Or .Direction can take something benefits in compare with .Rotation in my case?

Underneath, both ways (setting `Rotation` or setting `Direction`) are equivalent. That is, setting `Direction` internally actually just uses `OrientationFromDirectionUp` and then it sets the `Rotation`.

So use whichever one it is simpler to “think about” in a particular case.