Archived

This topic is now archived and is closed to further replies.

Zorbfish

Jumping

Recommended Posts

Here''s a snippet of my jump code:
  
if(KEYDOWN(VK_SPACE))
{
        Hero.Setf(JUMPING);
        jump = Hero.posY - 30;
};

if(!Hero.Checkf(JUMPING) && Hero.posY < ZERO_LEVEL)
{
	Hero.velY += 9.8 * time_mod;
}
else
{
        Hero.velY = 0;
};

if(Hero.Checkf(JUMPING) && Hero.posY > jump)
{
	Hero.velY += -9.8 * 0.9f;
}
else
{
	Hero.Unsetf(JUMPING);
};
  
Extra details: Checkf( flag ) returns true if the flag is set to the object Unsetf( flag ) turns off the object flag Setf ( flag ) turns on the object flag The code works fine, the character bounces on command when the user presses the space bar. The problem is that if the space bar is held down or tapped when the player is falling, that object flies up and away The jump variable is set always 30 units above the player''s position so that even if they are not at ground level they can jump. Can anyone suggest either a new design to this code or just point out a way to fix it so I can''t jump until the object hits the ground?

Share this post


Link to post
Share on other sites
Assuming that Checkf(JUMPING) returns false if the hero is on the ground...


    
if(KEYDOWN(VK_SPACE) && !Hero.Checkf(JUMPING))
// JUMP





Share this post


Link to post
Share on other sites
If only it was that easy :D
If you trace through my code the JUMPING flag is switched off when the object reaches jump height (30 units above its original position). The JUMPING flag is turned off at this point so that the piece of code that simulates gravity can work. So that solution still allows the user to hold space down and continue rising up. Thanks for trying, but that still isn''t a valid solution.

Zorb

Share this post


Link to post
Share on other sites
All I can think of at present is that you need to track the ground level and test whether the current position is on the ground. If they are then execute the jumping code otherwise ignore the jumping code.

Other than that I can''t help you ... not touched character movement on ground yet.

Tina

Share this post


Link to post
Share on other sites
Well then how could you jump if you were on an elevated platform?
I agree picking a reference level for falling (that''s the ZERO_LEVEL) is needed but if you ignore jumping if you''re not on ground level the player would stand on an elevated platform unable to jump. That''s why jump is there to allow jumping from any level using the player''s original y position as the reference level.

Share this post


Link to post
Share on other sites
The only way I see is by checking the player''s "on ground" state.
Whenver the player hits an elevated platform or the floor on his way downwards the "on ground" state is activated indicating that the jump sequence is complete and that the player is able to jump again.
Hope that helps...

Share this post


Link to post
Share on other sites
I did implement a similar kind of thing to what you have, but rather than using a JUMPING flag, I had a flag called FALLING. Whenever an object was falling, I used v=u+at formula on that object''s velocity.
Now in order to jump, I simply set the object to falling with an initial velocity with a direction opposite to gravity.

And like origil said, I had to check the object relative to the ground height in order to unset the FALLING flag.

Sorry bout the last answer, I''ll read people''s code properly next time.

Share this post


Link to post
Share on other sites