Need help with my jump physics code
-
I need some help with the jump physics example I found on the web. The following code makes my character go up and down, but I have to press and hold down the arrow-up key on my keyboard. I want my character to complete a full jump cycle without having to hold the arrow-up key down. I want to be able to press down on the key once and release to do a complete jump. Do I need to somehow include a do while loop? :confused Thanks! :)
bool jumping;
const float step = 0.05f; // how much the player will move per frame when jumping
const float limit = 1.0f; // the maximum height the player can reach while jumpingif (GetAsyncKeyState(VK_UP)== TRUE) // jump key
{static float jump = 0.0f;
static float strength = limit;if(jumping){
jump += step;
if(jump <= limit){
strength -= step;
player.y += (step * strength);
}
else{
strength += step;// detect if moving the player down would put him below the ground
// 0.0f is assumed to be the ground hereif(player.y - (step * strength) <= 0.0f)){
player.y = 0.0f;jumping = false; // the player has landed
jump = 0.0f;
strength = limit; // reset the strength
}else{
player.y -= (step * strength);
}
}
}
// rest of rendering...
} -
I need some help with the jump physics example I found on the web. The following code makes my character go up and down, but I have to press and hold down the arrow-up key on my keyboard. I want my character to complete a full jump cycle without having to hold the arrow-up key down. I want to be able to press down on the key once and release to do a complete jump. Do I need to somehow include a do while loop? :confused Thanks! :)
bool jumping;
const float step = 0.05f; // how much the player will move per frame when jumping
const float limit = 1.0f; // the maximum height the player can reach while jumpingif (GetAsyncKeyState(VK_UP)== TRUE) // jump key
{static float jump = 0.0f;
static float strength = limit;if(jumping){
jump += step;
if(jump <= limit){
strength -= step;
player.y += (step * strength);
}
else{
strength += step;// detect if moving the player down would put him below the ground
// 0.0f is assumed to be the ground hereif(player.y - (step * strength) <= 0.0f)){
player.y = 0.0f;jumping = false; // the player has landed
jump = 0.0f;
strength = limit; // reset the strength
}else{
player.y -= (step * strength);
}
}
}
// rest of rendering...
}OK, this code is posted "out of context" (it simply starts with the "if" statement, not how you got there). A reasonable assumption given your problem statement is that this code is run as part of an "OnChar()" or other event *caused* by your holding a key down. If that's the case, and you want to convert this to code that runs in a loop, you need to address two things. 1) what event starts the "jumping"? 2) what event stops the "jumping"? Clearly, right now event 1) is you holding down a key, event 2) is when you let it go. Letting the key go is *not* an event, it's the absence of a key_down event. You'll need to figure out the "stop" event first.
-
OK, this code is posted "out of context" (it simply starts with the "if" statement, not how you got there). A reasonable assumption given your problem statement is that this code is run as part of an "OnChar()" or other event *caused* by your holding a key down. If that's the case, and you want to convert this to code that runs in a loop, you need to address two things. 1) what event starts the "jumping"? 2) what event stops the "jumping"? Clearly, right now event 1) is you holding down a key, event 2) is when you let it go. Letting the key go is *not* an event, it's the absence of a key_down event. You'll need to figure out the "stop" event first.
Hi Chuck O'Toole, thanks for the quick reply. These are global variables:
bool jumping;
const float step = 0.05f; // how much the player will move per frame when jumping
const float limit = 1.0f; // the maximum height the player can reach while jumpingAnd the rest of the code below is contained inside of my game rendering function. The jump code works when I press the arrow-key, it just does't work correctly.
BOOL StartGame ()
{
if (GetAsyncKeyState(VK_UP)== TRUE) // jump key
{static float jump = 0.0f;
static float strength = limit;if(jumping){
jump += step;
if(jump <= limit){
strength -= step;
player.y += (step * strength);
}
else{
strength += step;// detect if moving the player down would put him below the ground
// 0.0f is assumed to be the ground hereif(player.y - (step * strength) <= 0.0f)){
player.y = 0.0f;jumping = false; // the player has landed
jump = 0.0f;
strength = limit; // reset the strength
}else{
player.y -= (step * strength);
}
}
}
// rest of rendering...
return TRUE;
} -
I need some help with the jump physics example I found on the web. The following code makes my character go up and down, but I have to press and hold down the arrow-up key on my keyboard. I want my character to complete a full jump cycle without having to hold the arrow-up key down. I want to be able to press down on the key once and release to do a complete jump. Do I need to somehow include a do while loop? :confused Thanks! :)
bool jumping;
const float step = 0.05f; // how much the player will move per frame when jumping
const float limit = 1.0f; // the maximum height the player can reach while jumpingif (GetAsyncKeyState(VK_UP)== TRUE) // jump key
{static float jump = 0.0f;
static float strength = limit;if(jumping){
jump += step;
if(jump <= limit){
strength -= step;
player.y += (step * strength);
}
else{
strength += step;// detect if moving the player down would put him below the ground
// 0.0f is assumed to be the ground hereif(player.y - (step * strength) <= 0.0f)){
player.y = 0.0f;jumping = false; // the player has landed
jump = 0.0f;
strength = limit; // reset the strength
}else{
player.y -= (step * strength);
}
}
}
// rest of rendering...
}If I got you: use a tristate variable instead of a boolean, say
jump_state
.enum JumpState
{
JS_IDLE,
JS_START,
JS_JUMPING
};//..
jump_state = JS_IDLE; // initialization
// ...
// main loop
if (GetAsyncKeyState(VK_UP) == TRUE)
if (jump_state == JS_IDLE)
jump_state = JS_START;
else
if (jump_state == JS_START)
jump_state == JS_JUMPING;
//...if ( jump_state == JUMPING)
{
// here do a step of jumping movement
if ( landed )
jump_state == JS_IDLE;
}Veni, vidi, vici.
-
If I got you: use a tristate variable instead of a boolean, say
jump_state
.enum JumpState
{
JS_IDLE,
JS_START,
JS_JUMPING
};//..
jump_state = JS_IDLE; // initialization
// ...
// main loop
if (GetAsyncKeyState(VK_UP) == TRUE)
if (jump_state == JS_IDLE)
jump_state = JS_START;
else
if (jump_state == JS_START)
jump_state == JS_JUMPING;
//...if ( jump_state == JUMPING)
{
// here do a step of jumping movement
if ( landed )
jump_state == JS_IDLE;
}Veni, vidi, vici.
Nice idea
-
Nice idea
-
If I got you: use a tristate variable instead of a boolean, say
jump_state
.enum JumpState
{
JS_IDLE,
JS_START,
JS_JUMPING
};//..
jump_state = JS_IDLE; // initialization
// ...
// main loop
if (GetAsyncKeyState(VK_UP) == TRUE)
if (jump_state == JS_IDLE)
jump_state = JS_START;
else
if (jump_state == JS_START)
jump_state == JS_JUMPING;
//...if ( jump_state == JUMPING)
{
// here do a step of jumping movement
if ( landed )
jump_state == JS_IDLE;
}Veni, vidi, vici.
Hey guys, thanks again for y'alls help. I got it working now. You guys are great! :)
-
Hey guys, thanks again for y'alls help. I got it working now. You guys are great! :)