|
-
Dec 8th, 2004, 12:04 AM
#1
Thread Starter
PowerPoster
C++: Camera Math - Pitch, RotateY -- Confirm If Correct
Check this out and let me know if this is correct:
I am having my mind blown trying to get a proper 3rd person camera setup.
I got this far, and I can get it to move and follow something but its all messed up and at times goes into weird and crazy positions. Sometimes it even just locks. So I figure might as well see if the math I am using is correct, if so then it is obviously my implimentation which needs work.
Thanks.
PHP Code:
// Rotates camera's eye around the world Y-axis
// Using your standard Y-axis rotation matrix, we'll rotate around the Y-axis
// ca == cosf(angle) sa == sinf(angle)
// [ ca 0 -sa ]
// [ 0 1 0 ]
// [ sa 0 ca ]
void Camera::RotateY(USINT angle, const D3DVECTOR &vTarget)
{
float xxx=0, zzz=0;
float sinAng=0, cosAng=0;
// Get sin()/cos() of angle
/*angle *= D3D.ToRad
sinAng = sinf(angle);
cosAng = cosf(angle);*/
cosAng = m_VectorCircle[angle].u;
sinAng = m_VectorCircle[angle].v;
// First translate to the world's origin
m_Eye -= vTarget;
// Save off forward components for computation?????????
xxx = m_Eye.x;
zzz = m_Eye.z;
// Rotate forward vector
m_Eye.x = xxx * cosAng + zzz * sinAng;
m_Eye.z = xxx * -sinAng + zzz * cosAng;
// Translate back
m_Eye += vTarget;
SetTarget(vTarget); // Build the camera's axes
}
void Camera::Pitch(USINT angle, const D3DVECTOR &vTarget)
{
// Compute the sin and cos of the angle
//float sinAng = sinf(angle);
//float cosAng = cosf(angle);
float cosAng = m_VectorCircle[angle].u;
float sinAng = m_VectorCircle[angle].v;
m_Eye -= vTarget; // Translate to world's origin
// Calculate new eye X component
float xxx = (m_Right.x * m_Right.x * (1.0f - cosAng) + cosAng) * m_Eye.x +
(m_Right.x * m_Right.y * (1.0f - cosAng) + m_Right.z * sinAng) * m_Eye.y +
(m_Right.x * m_Right.z * (1.0f - cosAng) - m_Right.y * sinAng) * m_Eye.z;
// Calculate new Y component
float yyy = (m_Right.x * m_Right.y * (1.0f - cosAng) - m_Right.z * sinAng) * m_Eye.x +
(m_Right.y * m_Right.y * (1.0f - cosAng) + cosAng) * m_Eye.y +
(m_Right.y * m_Right.z * (1.0f - cosAng) + m_Right.x * sinAng) * m_Eye.z;
// Calculate new Z component
float zzz = (m_Right.x * m_Right.z * (1.0f - cosAng) + m_Right.y * sinAng) * m_Eye.x +
(m_Right.y * m_Right.z * (1.0f - cosAng) - m_Right.x * sinAng) * m_Eye.y +
(m_Right.z * m_Right.z * (1.0f - cosAng) + cosAng) * m_Eye.z;
m_Eye = D3DVECTOR(xxx, yyy, zzz); // Set the eye
m_Eye += vTarget; // Translate back
// Get vector from target to eye
D3DVECTOR Vec = m_Eye - vTarget;
//Vec.normalize();
normalize(Vec);
// Position eye so that's always "kCamDist" from target
m_Eye = vTarget + (Vec * m_Away);
SetTarget(vTarget); // Build the camera's axes
}
// Builds the camera's axes by using the eye, target, and world's up vector
void Camera::SetTarget(const D3DVECTOR &PlayerPos)
{
// Compute new forward vector (line of sight vector)
m_Forward = PlayerPos - m_Eye;
//forward.normalize();
normalize(m_Forward);
// Compute new right vector
//m_Right = kWorldUp.crossProduct(forward);
m_Right = CrossProduct(m_Up, m_Forward);
//m_Right.normalize();
normalize(m_Right);
// Compute new camera up vector
//m_Up = forward.crossProduct(right);
m_Up = CrossProduct(m_Forward, m_Right);
}
// Normalizes the CVector (ie makes it a unit vector)
void normalize(D3DVECTOR &v)
{
//assert(!TEqual(magnitude(v), 0.0f, .001f)); // Make sure length isn't zero
float oneOverLen = 1.0f / magnitude(v);
v.x *= oneOverLen;
v.y *= oneOverLen;
v.z *= oneOverLen;
}
// Checks for two floats to be equal within a certain tolerance
bool TEqual(float a, float b, float t)
{
return ((a > b - t) && (a < b + t));
}
// Returns the magnitude (ie length or norm) of the CVector
float magnitude(D3DVECTOR &v)
{
return sqrtf((v.x * v.x) + (v.y * v.y) + (v.z * v.z));
}
Last edited by Electroman; Dec 8th, 2004 at 07:21 PM.
Reason: Added Language to title.
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Dec 8th, 2004, 02:23 PM
#2
Thread Starter
PowerPoster
Re: Camera Math - Pitch, RotateY -- Confirm If Correct
So I guess I am to believe no body on these forums has got this far?
Thanks.....
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Dec 8th, 2004, 03:35 PM
#3
Hyperactive Member
Re: Camera Math - Pitch, RotateY -- Confirm If Correct
Don't give up Hals. I would help....but I dont even know about that much math...I never made it past Algebra1 in Highschool lol. Though I love math.
-
Dec 8th, 2004, 04:48 PM
#4
Thread Starter
PowerPoster
Re: Camera Math - Pitch, RotateY -- Confirm If Correct
I am getting it working just fine...I think.
It still acts up a bit, and gets a bit shaky when moving on angles. But it stays on the player now always. It never locks up.
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Dec 8th, 2004, 07:48 PM
#5
Ex-Super Mod'rater
Re: Camera Math - Pitch, RotateY -- Confirm If Correct
 Originally Posted by Halsafar
So I guess I am to believe no body on these forums has got this far?
Thanks.....
I'd actually say the problem is that you've not said what the theory behind your camera is. Also instead of posting the solid code maybe if you could show what the calculations are trying to do we could aid better at spotting the problem.
You could give an example of how it could be used like I'm not sure why your bringing Pitch into it if its a 3rd person camera?
I'm pretty sure many people on this forum have made there own camera routines before but many are specific to their own games.
If I was to do a 3rd person camera I'd have the LookAt location always the player and let the cameras actual position "glide" around behind them.
When your thread has been resolved please edit the original post in the thread (  )
and amend "-[RESOLVED]-" to the end of the title and change the icon to  , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

-
Dec 8th, 2004, 09:38 PM
#6
Thread Starter
PowerPoster
Re: C++: Camera Math - Pitch, RotateY -- Confirm If Correct
Yes, that is what I'm doing. But as you saw when I uploaded my game awhile ago the camera was faultered.
The camera is in a very weak stage right now. I developed a nice new 3rd person camera which nicely follows the player like in GTA for example, and is always looking at the player.
I am having several problems.
The major one being, my camera follows me to a T if I am going forwards at any angle, if I begin to move backwards tho...It trails of to an odd angle and is unable to get itself back on track.
I cannot seem to get the camera to follow at a distance less than the speed it and the player are travelling...So its always far away from the player.
I cannot seem to figure out how to give the player free rotation of the camera....Well okay I know how, and I have it down but I cannot seem to figure out how to tell which the way the camera is pointing to determine which way forward is based on where the player is looking.
If you have played any ps2 games especially, with the 2 analog sticks, essentially you can control you guy by only moving the camera wth one stick and holding forward with the other. It is pretty much how I played through Chaos Legion, Castlevania, and many more.
Edit:
I believe my Forward VECTOR is to blame on the going backwards and the camera messing up.
Another Edit: It seems as if I can move backwards fine...for awhile, the length at which I can move backwards varies, on what I do not know. But after awhile the camera spins far out to the opposite angle it was on and loose's track of the object.
More Edit:
I have changed so much. Still the camera always follows at a distance equal to the speed of the player.
Farout Edit:
I have realized that for the camera to dictate the players angle that the camera should probably also dictate the players new position. The player on the other hand dictates the speed at which the camera will move. That way the camera's angle can always be the forward button. So does that mean that backwards should always be the opposite angle the camera is at?
Last edited by Halsafar; Dec 8th, 2004 at 10:31 PM.
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|