Jump to content
  • Advertisement
Sign in to follow this  
Alex Swinney

Making camera jump/crouch?

This topic is 4996 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

How should I make the character(camera) jump and crouch (not concurrently)? I tried to use a float timeDelta that was the time difference between frames and subtracting that variable from the current camera height until I got to a certain position. That didn't work other than to give my app the appearance of an earthquake. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Alex,

Do you mind posting a code fragment? Might help. A couple of things that come to mind. First, you should take the frame time and apply a multiplier, e.g., something like 0.1 or 0.025, to cause the camera crouch or jump occur over a longer period of time. THe earthquake-ness could just be simply due to the camera moving too much each frame. Second, you'll get better results if you turn the crouch or jump motion into something physically based, e.g., a crouch would have the camera moving downward at a constant velocity rather than a fixed amount each frame. And, the jump motion could be simulated by giving the camera an initial velocity upwards and then allowing gravity to pull it back down. In any case, use a frame-rate-independent approach to implementing the motion. This will ensure you get consistent results regardless of the actual frame rate, e.g., it'll look good on slow and fast CPU's and GPU's. Do a forum search in this forum for frame rate independent physics for more help here.

Share this post


Link to post
Share on other sites
Quote:
Original post by grhodes_at_work
Alex,

Do you mind posting a code fragment? Might help. A couple of things that come to mind. First, you should take the frame time and apply a multiplier, e.g., something like 0.1 or 0.025, to cause the camera crouch or jump occur over a longer period of time. THe earthquake-ness could just be simply due to the camera moving too much each frame. Second, you'll get better results if you turn the crouch or jump motion into something physically based, e.g., a crouch would have the camera moving downward at a constant velocity rather than a fixed amount each frame. And, the jump motion could be simulated by giving the camera an initial velocity upwards and then allowing gravity to pull it back down. In any case, use a frame-rate-independent approach to implementing the motion. This will ensure you get consistent results regardless of the actual frame rate, e.g., it'll look good on slow and fast CPU's and GPU's. Do a forum search in this forum for frame rate independent physics for more help here.


Graham,

It was a really simple 'if else' statement, nothing spectacular. This is out of a function that updates the camera that is being called directly before everything is rasterized.

Global/passed vars:
1) timeDelta is the time difference between when the previous frame was created compared to when this frame was created.
2) currentHeight/crouchHeight: self-explanatory
3) crouching: boolean var that tells whether the user wishes to crouch
4) pos: vector of 3-space that is member of camera class denoting the camera's position within the application.


This first 'if' statement is put in place when the crouch begins and the currentHeight is greater than the crouchHeight (both global constants).


//if camera is crouching & hasn't fully descended:
else if(crouching && currentHeight >= crouchHeight)
{//camera is in the process of crouching
this->crouch(timeDelta);
}

This next block is after the camera has reached crouchHeight or above:

else if(crouching && currentHeight <= crouchHeight)
{//camera has finished crouching
this->pos.y = crouchHeight;
this->setPosition(&pos);
crouching = false;
}

Finally, here is the code that tried to perform the crouch, with TheTerrain being of my terrain class and TheCamera being my camera object (I realize that I need not use TheCamera, could use this-> instead). TheTerrain->getHeight() gets the height at a specified x by z location (returns the y) and I know that function works correctly. 'height' is a global constant defined to be the height that the camera sits above the terrain height.

void Camera::crouch(float timeDelta)
{
currentHeight -= timeDelta/5;
float height = TheTerrain->getHeight(this->pos.x, this->pos.z);
this->pos.y = height + currentHeight;
TheCamera.setPosition(&pos);
}


Let me know if I missed anything else, Graham, in terms of variables and such. Thanks for the help!

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!