# direction of rotation

This topic is 5225 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, I've been trying to sort out some automatic camera rotation w.r.t. a given lookat point as it moves along a path. I'm taking the dot product of the forward vector of the camera at the last position and the vector from eyepoint to lookat point at the current position and working out the angle with arccos of the dot product. The problem is to work out the sign of the rotation angle... I just can't see a way of doing this without recording the last position of the camera and doing loads of if statements to workout in which direction the camera should be rotating. Is there any other, more straight forward way of doing this? Cheers, Hlnsi

##### Share on other sites
2D, 3D? I assume 3D.

Usually, rotating a camera towards a point requires just an inverse cosine and a cross product.

like this...

Vector CamDir;
Vector TargetDir;
Vector RotAxis;

RotAxis = CamDir x TargetDir;
RotAxis.Normalise();
angle = acos(CamDir . TargetDir);

rot_speed = angle / frame_timestep;

if (angle > max_rot_speed) angle = max_rot_angle;
if (angle < -max_rot_speed) angle = -max_rot_speed;

Camera.RotateAroundAxis(RotAxis, rot_speed * frame_timestep);

...anyway, if there is a sign you need, then it would be simply checking the dot product of the axis of rotation you are using, and the natural axis of rotation above.

Vector MyAxisOfRotation = ....;
Vector NaturalAxis = CamDir x TargetDir;
float sign = SignOf(MyAxisOfRotation.Dot(NaturalAxis));
......

I'm not 100% sure about the direction of the natural axis. could be TargetDir x CamDir, but it's easy enough to find out.

if your camera is more like a gun turret, then you need to Inverse-transform the target into the camera orientation, and compute the elevation (along the turret's strafe axis) and rotation *along the turret's deviations.

Vector p = (Target - campos) * camorient.transpose();
p.normalise();
float delta_rotation = atan2(p.z, p.x);
float delta_elevation = asin(p.y);

Limit(delta_rotation, max_rot_speed * timestep);
Limit(delta_elevation, max_elev_speed * timestep);

camera.rotate(camera.UpAxis, delta_rotation);
camera.rotate(camera.StrafeAxis, delta_elevation);

there are no signs involved. the turret should rotate automatically towards its target in the fastest possible rotation. make sure you rotate first before elevating.

my 2 cents.

##### Share on other sites
This last method works like a charm, besides I had to ad a minus sign on that asin... probably due to my own pitch function!

thanks a million

##### Share on other sites
cool. coz I made that stuff up :) (untested is the word, really).

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633770
• Total Posts
3013760
×