Sign in to follow this  
Edward472

Jumping Physics I got. Keypresses I dont...

Recommended Posts

I am working on a game. And i have got my jumping physics down. The only problem is that the user has to keep the button pressed or it wont finish. If i replace my "if" statement with a "while" the jump happens in less than a second. Any thoughts on how i could get this to 1) check for a key press and not a key hold down (I tried to remove the & bitwise operator. But that caused the same problem with constant jumping) 2) get the code to work the same?


double velocity = .06;
double max = -.07;

void CCamera::Jump()
{
if(mPos.y<=2.5)
{
mPos.y=2.5;
mView.y=2;
}
if(velocity>max)
{
mPos.y+=velocity;
mView.y+=velocity;
velocity-= .0003;
}

Share this post


Link to post
Share on other sites
I think you might have inadvertently posted this in the wrong forum. (You might PM a mod and see if they'll move it to Game Development or For Beginners for you.)

Share this post


Link to post
Share on other sites
First of all, you need to consider elapsed time here.

velocity-= .0003; will yield different results under different frame rates.


The way I handle simple jumping is essentially like this:

1.) detect user has pushed the jump key.

2.) if ( not already jumping )
jump velocity.y = jump initial velocity.


in the Animate() portion of this object:

if ( jump velocity.y > 0 )
jump velocity.y = max ( jump velocity.y - elapsed Time * gravity, 0 )

are you having trouble detecting key presses? where's that code?

Share this post


Link to post
Share on other sites
I keep my keyboard input in my main.


void Keyboard_Input()
{
if(GetKeyState('W') & 0x80)
{
objCamera.Move_Camera( CAMERASPEED);

}

if(GetKeyState('S') & 0x80)
{
objCamera.Move_Camera(-CAMERASPEED);
}

if(GetKeyState('A') & 0x80)
{
objCamera.Strafe(-CAMERASPEED);
}

if(GetKeyState('D') & 0x80)
{
objCamera.Strafe(CAMERASPEED);
}
if(GetKeyState(VK_CONTROL))
{

objCamera.Jump(jump);
}
}





Also elapsed time? All implementation of getting elapsed time rely on GLUT. And the fuction getGLUT(elapsedtime) returns the time since glutINIT() was called. I dont know if i can just stick glut code in.

Share this post


Link to post
Share on other sites
I think what you need to do is to separate the "frame rate" at which your physics simulation runs from the actual rendering frame rate.

Try this:

/* global */
int last_tick = 0;
const int physics_fps = 60; /* desired framerate of your physics simulation */


then before calling glutMainLoop() (probably inside main()):
last_tick = glutGet( GLUT_ELAPSED_TIME );


and finally, in your rendering function:
int tick = glutGet( GLUT_ELAPSED_TIME );
while ( last_tick < tick ) {
/* this is where you move things around */

last_tick += 1000 / physics_fps;
}

/* regular drawing code goes here */

Share this post


Link to post
Share on other sites
bool player_is_jumping = false;


if(SPACE)
{
player_is_jumping = true;
}


UpdatePlayer()
{
if(player_is_jumping = true)
{
//jumping code
//if player done jumping
//player_is_jumping = false
}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this