Jump to content
  • Advertisement
Sign in to follow this  
hlnsi

direction of rotation

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!