First person camera jump code not working

Started by
2 comments, last by Aardvajk 9 years, 10 months ago

I cant seem to get this code working,its suppose to let a first person style camera jump up and back down,and land back on the terrains y heightmap,thanks if you can help

//in camera.cpp


 
D3DXVec3Normalize(&dir, &dir);
D3DXVECTOR3 newPos = mPosW + dir*mSpeed*dt;
 
if( terrain != 0)
{
// New position might not be on terrain, so project the
// point onto the terrain.
 
//newPos.y = terrain->getHeight(newPos.x, newPos.z) + offsetHeight;
 
// Now the difference of the new position and old (current) 
// position approximates a tangent vector on the terrain.
D3DXVECTOR3 tangent = newPos - mPosW;
D3DXVec3Normalize(&tangent, &tangent);
 
// Now move camera along tangent vector.
mPosW += tangent*mSpeed*dt;
 
// After update, there may be errors in the camera height since our
// tangent is only an approximation.  So force camera to correct height,
// and offset by the specified amount so that camera does not sit
// exactly on terrain, but instead, slightly above it.
mPosW.y = terrain->getHeight(mPosW.x, mPosW.z) + offsetHeight;
 
if(canJump)
{
static float timeadded;
timeadded += dt;
float CurJump=JUMPAMP * sin(timeadded * D3DX_PI / JUMPTIME);//Store the height of the jump in progress to avoid 2 calls to sin()
mPosW.y = terrain->getHeight(mPosW.x, mPosW.z) + offsetHeight + CurJump;
if (CurJump<0) //If we have reached the ground lock the height back to the terrain and end the jump with jump=0
{
mPosW.y = terrain->getHeight(mPosW.x, mPosW.z) + offsetHeight;
canJump = false;
timeadded = 0;
}
else
{
mPosW.y = terrain->getHeight(mPosW.x, mPosW.z) + offsetHeight;
}
}
else
{
mPosW.y = terrain->getHeight(mPosW.x, mPosW.z) + offsetHeight;
}
}
else
{
mPosW = newPos;
}
 
:)
Advertisement

You can't just dump code on us (code you've copied from elsewhere without understanding as per the last thread) and expect us to "fix" it when you haven't even told us what the problem is.

My advice would be: stop copy-pasting code from other sources into your project without understanding it and expecting it to work. Also, when asking for help, demonstrate that you have done more than simply copy-paste code from other sources into your project. Tell us what you have tried, and what isn't working.

You can't just dump code on us (code you've copied from elsewhere without understanding as per the last thread) and expect us to "fix" it when you haven't even told us what the problem is.

My advice would be: stop copy-pasting code from other sources into your project without understanding it and expecting it to work. Also, when asking for help, demonstrate that you have done more than simply copy-paste code from other sources into your project. Tell us what you have tried, and what isn't working.

Actual i am trying to learn game development by modifying books source code,etc adding new things such as run,jump,add an fps weapon, there is nothing wrong with it,its the best way to learn i think.

the problem is nothing is happening when i press "spacebar", it should perform a sin wave jump but theres no response whats so ever.

:)
Okay. So a skill you need to learn is debugging. You can either use the debugger in your IDE or insert trace statements into the code that output to the terminal or a file, or to OutputDebugString and so on.

This way you can confirm the method is being run when you hit space. If so, you can trace through and look at what is happening to each variable and so on.

My first guess would be that you aren't assigning mPosW to your camera position after the call but without wider context it is impossible to say.

This topic is closed to new replies.

Advertisement