Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

c++: Camera Update function completely broken

Started by Halsafar Feb 22, 2005 at 3:14 PM 1 replies 450+ views
Original Post
Halsafar
Halsafar
What em I doing wrong?!?!?! I Call Update() and pass in the velocity of the unit the camera is focus'd on. The camera follows fine but falls behind the object rather quickly. Like a fast moving object gains speed on the camera somehow If the object goes up, the camera goes down and vice versa. The functions are commented //-------------------- //Updates the Camera // Last modified: 06.02.05 15:04 (Halsafar) //-------------------- void Camera::MoveCamera(const Vector &vDir, Vector &vVelocity) { // Move the camera on the X and Z axis. m_vEye.x += vDir.x * vVelocity.x; m_vEye.y += vDir.y * vVelocity.y; m_vEye.z += vDir.z * vVelocity.z; // Move the view along with the position m_vTarget.x += vDir.x * vVelocity.x; m_vTarget.y += vDir.y * vVelocity.y; m_vTarget.z += vDir.z * vVelocity.z; } //-------------------- //Update the camera's location with a unit vector * velocity // Last modified: 06.02.05 15:04 (Halsafar) //-------------------- void Camera::Update(Vector &vVelocity) { // First we need to get the direction at which we are looking. Vector LookDirection; // The look direction is the view (where we are looking) minus the position (where we are). LookDirection = m_vTarget - m_vEye; // Normalize the direction. LookDirection.Normalize(); // Call UpdateCamera to move our camera in the direction we want. MoveCamera(LookDirection, vVelocity); }
stevenmarky
stevenmarky
Quote:

// Move the camera on the X and Z axis.
m_vEye.x += vDir.x * vVelocity.x;
m_vEye.y += vDir.y * vVelocity.y;
m_vEye.z += vDir.z * vVelocity.z;


Seems you are moving the camera on all the axis.

anyway, if the velocity of the camera is decreasing then either parts of vDir or vVelocity are negative/decreasing.
Check then out in your debugger with your variable watch or figure out in your code where it is happening.

Your movement code seems strange (to me) anyway.
You are multiplying a velocity by a direction..why?
I would have:
//If the direction of the object you are looking at is normalised you can just multiply it by a constant:velocity=direction.normalised()*speed; // where speed is a float of how fast we are moving toward the objecteyePosition=eyePosition+(velocity*timeInterval);



JohnBolton
JohnBolton
Quote:
Original post by Halsafar
...
m_vEye.x += vDir.x * vVelocity.x;
m_vEye.y += vDir.y * vVelocity.y;
m_vEye.z += vDir.z * vVelocity.z;

// Move the view along with the position
m_vTarget.x += vDir.x * vVelocity.x;
m_vTarget.y += vDir.y * vVelocity.y;
m_vTarget.z += vDir.z * vVelocity.z;
...
The code is wrong. The velocity vector already has the components for each axis so multiplying by vDir gives you the wrong result. Suppose vDir is [.707, .707, 0.] and vVelocity is [2., 2., 0.]. That means the camera will move [1.414, 1.414, 0.] ([.707 * 2., .707 * 2., 0. * 0.]), but the object will move [2., 2., 0.]. You want to move the camera the same as the object it is following so just add the velocity (as long as "vVelocity" is distance/frame).
...   m_vEye.x += vVelocity.x;   m_vEye.y += vVelocity.y;   m_vEye.z += vVelocity.z;   // Move the view along with the position   m_vTarget.x += vVelocity.x;   m_vTarget.y += vVelocity.y;   m_vTarget.z += vVelocity.z;... 
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Topic Locked

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

Sign in to reply to this topic.