Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Camera Positioning

Started by gameplayprogammer 1 reply 500+ views
Original Post
gameplayprogammer
gameplayprogammer
I have positioned a camera to follow a target. How would i make the camera move so that the target is always at an angle x from the camera on all axes? I think i need to recalculate the yaw and pitch so the camera tilts and pans so the angle between the camera vector and the target vector stays constant. Any ideas Here is what i am doing currently, this code will follow the target but will not maintain the angle, camera seems to pan over and around the target instead of staying alongside the target at a set distance away. Scale(&invCamPos, &camPos, -1.0f); // Translate the camera back to the rotation origin... Translate(cameraFrame, &invCamPos, rwCOMBINEPOSTCONCAT); ZDist.x = 0.0f; ZDist.y = 0.0f; ZDist.z = dist; target.x = t.x; target.y = t.y; target.z = t.z; cameraMatrix->pos.x = cameraMatrix->pos.y = cameraMatrix->pos.z = 0.0f; Transform( &temp, &ZDist, cameraMatrix ); Subtract( &camPos, ⌖, &temp ); // Get the camera's UP vector and use this as the axis of rotation... Rotate(cameraMatrix, &up, pitch, combineReplace); Rotate(cameraMatrix, GetRight(cameraMatrix), yaw, postConcatenate); // Translate the camera back to its original position... Translate(cameraFrame, &camPos, postConcatenate);
TheAdmiral
TheAdmiral
Yep, if possible, avoid the triginometry; it will make your arithmetic much simpler. If you need to calculate the angles to feed them into something else, you're probably still better off doing it this way and then using inverse trig to determine the angles afterwards.

If your target object has position A then you should create a displacement vector from the object X = (x, x, x) to the camera. You said you wanted the camera to be fixed wrt to the object, but you didn't specify if this was in world or object coordinates.

If you meant world, the situation is about as simple as can be: At any time, the camera's position is A [plus] X and its orientation is always -X.
If you were after object coordinates, the situation is the same, but you'll need to rotate X according to the object's orientation before using it.
Edit: This is best achieved by recycling the object's world matrix when you set your view matrix.

Regards
Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.