aligning an object to point in same direction as camera
hi all,
i''m trying to build a space sim. i have my ship in the space and i have a camera that i can look around the space in. now what i''m trying to do is have the the ship point in the same direction as the camera. so if the camera is pointing toward (1,1,1) i want to rotate the ship and have it point toward (1,1,1).
does anyone know a simple way of doing this?
thxs
All you need to do if get the orientation matrix of your camera, and apply it to the spaceship 
Is you do not have an orientation matrix for your camera, you''d usually have a direction towards where the camera points, and a Up vector towards the vertical of the camera. If your camera always stays roughly horizontal, Up would be (0, 1, 0)
so
Another way to do rotation, is to get the axis of rotation between your spaceship direction and the camera direction, workout the angle, and do a rotation using this axis and the angle
the advantage of doing things like that is that you can make the spaceship rotate slowly towards the camera direction, by clamping the AngleOfRotation by a maximum and minimum rotational speed.

Is you do not have an orientation matrix for your camera, you''d usually have a direction towards where the camera points, and a Up vector towards the vertical of the camera. If your camera always stays roughly horizontal, Up would be (0, 1, 0)
so
Vector Dir = Camera.Dir;Vector Up = Camera.Up; // maybe = (0, 1, 0)Vector Side = (Up.Cross(Dir)).UnitVector();Up = Dir.Cross(Size); // re-normalise the Up vector, in case Up was not 100% perpendicular to Dir.Matrix3x3 Orient;Orient.e11 = Side.x; Orient.e21 = Side.y; Orient.e31 = Side.z; Orient.e12 = Up.x; Orient.e22 = Up.y; Orient.e32 = Up.z; Orient.e13 = Dir.x; Orient.e23 = Dir.y; Orient.e33 = Dir.z; SpaceShip.SetOrientation(Orient);
Another way to do rotation, is to get the axis of rotation between your spaceship direction and the camera direction, workout the angle, and do a rotation using this axis and the angle
void RotateSpaceShipTowardsCamera(){ // axis of rotation Vector Axis = SpaceShip.Dir.Cross(Camera.Dir); float AxisLength = Axis.GetLength(); // cosine of angle of rotation required float CosAngle = SpaceShip.Dir.Dot(Camera.Dir); // spaceship is pointing towards or back to the camera if (AxisLength() < 0.0000001f) { // pointing back, rotate 180 degrees if (CosAngle < 0.0f) { // find axis of rotation perpendicular to direction of camera Vector Dir1 = Camera.Dir; Vector Dir2 = (fabs(Dir1.x) > fabs(Dir1.y))? Vector(0, 1, 0) : (fabs(Dir1.y) > fabs(Dir1.z))? Vector(0, 0, 1) : Vector(1, 0, 0); Axis = Dir1.Cross(Dir2); Axis.Normalise(); // rotate 180 degrees SpaceShip.Rotate1(PI, Axis); } return; } // normalise axis, and calcualte the angle required from the cosine Axis /= AxisLength; float AngleOfRotation = acos(CosAngle); // rotate around an axis, with an angle SpaceShip.Rotate(AngleOfRotation, Axis);}
the advantage of doing things like that is that you can make the spaceship rotate slowly towards the camera direction, by clamping the AngleOfRotation by a maximum and minimum rotational speed.
Everything is better with Metal.
thanks alot. the RotateSpaceShipTowardCamera is exactly what i wanted to do. i appreciate it alot. now to work this into my code 
thanks again.

thanks again.
tell me if it works. I never really tried
I thought about implementing heat seeking missiles that way.

Everything is better with Metal.
worked perfectly!!! even added a time based movement so it only moves toward the camera a little each frame. you rock!!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement