Jump to content
  • Advertisement
Sign in to follow this  
majc

Need help with crouch and jump imput keys

This topic is 4838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I already made forward,strafe,back movements and im having problems with jump and crouch when i use the crouch key the cam is pointed to the ground. This is my function input: void input() { //Just like in tutorial 4, where we moved the teapot by using //the keyboard and mouse input, we will move the camera vectors //(position and look at) with the keyboard and mouse. But this //time, we will get hard core with the math. But, don't be too //scared, once you created the right movement code, you don't //need to recreate it : you use CUT and PASTE. //Check if we pressed the UP arrow key, if so, then we are //walking forward. if(pInput->IsKeyPressed(TV_KEY_UP)) sngWalk = 1; //If we are not walking forward, maybe we are walking backward //by using the DOWN arrow? If so, set walk speed to negative. if(pInput->IsKeyPressed(TV_KEY_DOWN)) sngWalk = -1; //Check if we pressed the LEFT arrow key, if so, then strafe //on the left. if(pInput->IsKeyPressed(TV_KEY_LEFT)) sngStrafe = 1; //If we are not strafing left, maybe we want to strafe to the //right, using the RIGHT arrow? If so, set strafe to negative. if(pInput->IsKeyPressed(TV_KEY_RIGHT)) sngStrafe = -1; //Now, for the mouse input... long tmpMouseX = 0; long tmpMouseY = 0; long tmpMouseB1 = 0; long tmpMouseB2 = 0; long tmpMouseB3 = 0; long tmpMouseScrollOld = 0; long tmpMouseScrollNew = 0; //We pass the actual value of the mouse scroller to the variable //that holds the old mouse scroller value. tmpMouseScrollOld = tmpMouseScrollNew; //By using GetMouseState, we get the movement of the mouse //with the speed of the movement. The fastest the mouse movement //will be, the higher will be the return. pInput->GetMouseState(&tmpMouseX, &tmpMouseY, (short *)&tmpMouseB1, (short *)&tmpMouseB2, (short *)&tmpMouseB3, &tmpMouseScrollNew); //From the mouse return values, we update the camera angles //by adding or substracting the mouse return value. sngAngleX = sngAngleX - ((float)tmpMouseY / 100.0f); sngAngleY = sngAngleY - ((float)tmpMouseX / 100.0f); //We will add a simple check, so we can't look up at more //than 80 degrees nor down than -80 degrees. if(sngAngleX > 1.3f) sngAngleX = 1.3f; if(sngAngleX < -1.3f) sngAngleX = -1.3f; //Okay, now for the smothing of the movement... We checked //above if we were pressing a key. If so, then we updated the //movement variable to 1 (positive or negative). Here, we //lower this value until it get to 0. This method give us a //smoother camera movement. We start by updating the forward //and backward (walk) movement. if(sngWalk > 0) { sngWalk = sngWalk - 0.005f; if(sngWalk < 0) sngWalk = 0; } if(sngWalk < 0) { sngWalk = sngWalk + 0.005f; if(sngWalk > 0) sngWalk = 0; } //Now, we update the left and right (strafe) movement. if(sngStrafe > 0) { sngStrafe = sngStrafe - 0.005f; if(sngStrafe < 0) sngStrafe = 0; } if(sngStrafe < 0) { sngStrafe = sngStrafe + 0.005f; if(sngStrafe > 0) sngStrafe = 0; } if(pInput->IsKeyPressed(TV_KEY_C)) { sngPositionY = sngPositionY - 10.0f; } fTimeElapsed = pEngine->TimeElapsed(); //Update the vectors using the angles and positions. sngPositionX = sngPositionX + (cosf(sngAngleY) * sngWalk / 5 * fTimeElapsed) + (cosf(sngAngleY + 3.141596f / 2.0f) * sngStrafe * fTimeElapsed); sngPositionZ = sngPositionZ + (sinf(sngAngleY) * sngWalk / 5 * fTimeElapsed) + (sinf(sngAngleY + 3.141596f / 2.0f) * sngStrafe * fTimeElapsed); sngLookAtX = sngPositionX + cosf(sngAngleY); sngLookAtY = sngPositionY + tanf(sngAngleX); sngLookAtZ = sngPositionZ + sinf(sngAngleY); //New : because we are using a landscape with up and down, we //can't let the camera at the same height. We want the camera to //follow the height of the map, so we use the "get height". Also, //because we want to have the effect that we are not a mouse, //we will add some height to the height returned... sngPositionY = pLand->GetHeight(sngPositionX, sngPositionZ) + 100; //With the new values of the camera vectors (position and //look at), we update the scene's camera. pScene->SetCamera(sngPositionX, sngPositionY, sngPositionZ, sngLookAtX, sngLookAtY, sngLookAtZ); if(pInput->IsKeyPressed(TV_KEY_ESCAPE)) //Check if ESCAPE has been pressed. { PostQuitMessage(0); } } Some help please...

Share this post


Link to post
Share on other sites
Advertisement
ahh right, its in the only part where there's no comments... ;)

As you can see, when the C key is pressed, you're decreasing your Y position... the thing is that you update it right after with :

sngPositionY = pLand->GetHeight(sngPositionX, sngPositionZ) + 100;

What I'd do is add a variable like fCamHeight, and depending on your stance, set it at what ever you want, and have your final sngPositionY be the sum of the terrain height + fCamHeight...

For you cam looking down... that's cause you calculate a lower Y value (for your crouch position), and calculate the look at BEFORE resetting it to the standing height

Fixing your sngPositionY should solve your problem.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!