It is currently Sat Dec 07, 2013 11:41 am

 All times are UTC

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Need help with my jump physics codePosted: Tue May 08, 2012 5:33 am
 Level 1 Cleric

Joined: Tue Feb 21, 2012 10:24 pm
Posts: 10
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?

Thanks!
Code:
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 jumping

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 here

if(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...
}

Top

 Post subject: Re: Need help with my jump physics codePosted: Tue May 08, 2012 8:07 am
 Shake'n'Baker

Joined: Thu Dec 29, 2011 2:33 pm
Posts: 62
I assume you have a while-game-running-loop, yes?

Code:
if (GetAsyncKeyState(VK_UP)== TRUE)     // jump key
{
player->jump = true
}

Code:
In the game loop{
if (player->jump == true){
jump_function(player)
}

In the jump_function, you define how high you want the player to jump.
It should increase the jump while it's not at its limit.
When it is at it's limit, you can decrease the y value, or
set the players attribute to falling, so your gravity does what you want(so did I).

Code:
jump_function(player){
if (player->jump_steps <= MAX_JUMP){
increase the y value
}else{
decrease y value or set attribut jumping to false
}

I hope I hit your problem and you understand what I am trying to say
Good luck!

Top

 Post subject: Re: Need help with my jump physics codePosted: Sun May 13, 2012 1:19 am
 Level 1 Cleric

Joined: Tue Feb 21, 2012 10:24 pm
Posts: 10
aestond wrote:
I assume you have a while-game-running-loop, yes?

Code:
if (GetAsyncKeyState(VK_UP)== TRUE)     // jump key
{
player->jump = true
}

Code:
In the game loop{
if (player->jump == true){
jump_function(player)
}

In the jump_function, you define how high you want the player to jump.
It should increase the jump while it's not at its limit.
When it is at it's limit, you can decrease the y value, or
set the players attribute to falling, so your gravity does what you want(so did I).

Code:
jump_function(player){
if (player->jump_steps <= MAX_JUMP){
increase the y value
}else{
decrease y value or set attribut jumping to false
}

I hope I hit your problem and you understand what I am trying to say
Good luck!

Hi aestond, thanks for the reply. I didn't use a function but was able to get it working with your help. I just put the entire code under with the key press input that ran constantly in the main game engine loop. Thanks for your help.

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 3 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: Google [Bot] and 1 guest

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ Forums    Forum Rules and Posting Guidelines Wiki Discussion    Help    Content Issues Game Programming Discussion    C and C++ Game Programming    Java Game Programming    Language Agnostic Programming    .NET Game Programming    VB Game Programming    Mobile Game Programming    Web-Based Game Programming    Other Languages    OpenGL Development    Direct X Development Game Development Discussion    Game Design    Game Media Off-Topic Discussion    Announcements    Off-Topic    Community Projects    News