Jumping problems in C++

Started by
8 comments, last by wicked357 15 years, 2 months ago
I have tried just about everything in my brain to get my sprite to jump and I just cannot figure it out. Can anyone help me here I went down to basics so things aren't too confusing and I'm not explaining a lot of my code so ill just stick to the part that counts.

//sprite structure
typedef struct
{
    int x,y;
    int movex,movey;
    int origY;
    int gravity;
} SPRITE;

//player state
enum State
{
    Walking,
    Jumping
};

//Init sprites properties
image.x = 50;
image.y = 204;
image.movex = 5;
image.movey = 5;
image.origY = 0;
image.gravity = 9.81;
or now here is the the movement code:

if(KEY_DOWN(VK_LEFT))
    image.x -= image.movex;

if(KEY_DOWN(VK_RIGHT))
    image.x += image.movex;

if(KEY_DOWN(VK_SPACE))
{
    //Jump logic goes here
}
Advertisement
C'mon not one person has a suggestion or something? I have googled anything and everything about jumping and can't seem to find an example or explanation that I can relate to my code. I have even looked at a lot of examples from people asking in previous threads about jumping, only problem I have is I don't know what a lot of there variables are for since they only post a small amount of there code, so I need something worked out for mine.
Can you detect is a sprite is on the ground? If so, a lot of the hard work has been done.

The main thing you need to do then is decouple your movement from the input. The problem with your method at the moment is that it assumes that things only happen while the user is pressing the keys. While it is reasonably true for moving left and right, if you jump you are committed to an action even if you stop pressing space.

Something like this:
// I am going to use C++ in this example:struct Sprite{    int position_x, position_y;    int velocity_x, velocity_y;    int acceleration_y; // you can include x acceleration if you wish    int runspeed, jumpspeed;    bool inAir; // this neatly covers jumping and falling.    // other stuff};bool hitsGround(const Sprite &sprite, /* ... other parameters ... */){     // whatever logic to work out if a sprite is on the ground}int main(){    Sprite player; // set the values here, or use a constructor call    // ...    while(running)    {        // input        if(key_down(VK_LEFT))        {            player.velocity_x = -player.runspeed;        }        else if(key_down(VK_RIGHT))        {            player.velocity_x = player.runspeed;        }        else        {            player.velocity_x = 0;        }        if(key_down(VK_SPACE))        {            player.velocity_y = 0;            player.acceleration_y = player.jumpspeed;            player.inAir = true;        }        // game logic        if( player.inAir )        {            player.velocity_y += player.acceleration_y;            player.acceleration_y -= /* gravity value */;            if( hitsGround(player, /* any other info needed */) )            {                player.inAir = false;            }        }        player.position_x += player.velocity_x;        player.position_y += player.velocity_y;        // drawing and other code    }}

You will probably want to use floats instead of integers for the various values.
So, what exactly is the problem you're having? Code snippets alone in this case aren't very enlightening: it would help if you'd explain your reasoning and your approach to the problem, as well as precisely what difficulties you've encountered.

In what way are you using the "gravity" field? What is your conceptual model of player movement and physics? How did you try to incorporate jumping into that model before, and how did it fail?

Basically, try to explain your whole situation a bit better, and it will be much easier for others to give you advice. [smile]


EDIT: Ah, ninja'd. [lol]

rip-off: any particular reason in your code it's subtracting gravitational acceleration from the player's acceleration every frame? Seems to me that once the player is in the air, the acceleration should be constant, since (in this basic example anyway) there are no other forces besides gravity acting on the player. For simplicity, I'd probably treat jumping as an instantaneous impulse giving an initial vertical velocity.
Basically I have a sprite and a background image, I have no actual collision going in this at all for now. The many problems I have faced is ending up with a bunch of nested if statements, that in the end doesn't end up doing what I want. I ended up jumping and staying where I was at, I have had the issue where I kept falling till I landed off the screen, but I didn't even jump yet!

My plan here is I can move left and right, I want to be able to jump y amount and be able to come down on a smooth rate not instant so it looks like nothing happened. I want to actually have a sort of simple physics involved to pull me back to the ground int. I have been looking up stuff and using this site and trying different variations for over 8 hours today, I am tired of getting stumped. I wish I had real substance to go off of, but I think that would just complicate what I am trying to do all together.
It sounds like what you've been doing is just trying to "code as you go," which is definitely going to be a confusing and frustrating experience if you don't already have a good idea of how your system is going to work. It's always a good idea to clearly think out your approach and create a design before you start writing code.

rip-off made a very good suggestion when he said you should decouple movement from input. What he meant (and illustrated with his code sample) is that you should give the player object physical properties like velocity and acceleration, instead of just directly changing the position of the player whenever the movement keys are pressed.

Then, each frame you can add the player's velocity to their current position to make them move. Also each frame, subtract your gravitational acceleration constant from their upward velocity (unless they're already on the ground). The movement keys can then work by changing the player's velocity: if the left or right keys are pressed, the player should have a corresponding negative or positive X velocity. When the spacebar is pressed, you can just give the player an initial upward velocity, and your gravitational acceleration will take care of the rest. Adjusting the initial jump velocity will give you different jumping heights.
Well actually the whole intention to this program was to make it jump moving forward and backwards is easy, that was just added because I want to be able to jump in the direction I am going so if I am standing still I jump up and fall down, if I am walking right I make a right jump and so on... The last 3 weeks I have been using c++ and directx to make games and so far I can do all the basics needed like load sprites load surfaces draw them and make them move, I thought I should move on to the next which was jumping and I have been stuck all day. I am been looking up different kinds of AI and mathematics, but still no luck that shows the exact easy to understand theory on gravity. Let me reiterate that I started directx/c++ programming 3 weeks ago I have taken 3 c++ classes already, but I have been doing mainly C# in the last 6 months.
Quote:Original post by wicked357
Well actually the whole intention to this program was to make it jump moving forward and backwards is easy, that was just added because I want to be able to jump in the direction I am going so if I am standing still I jump up and fall down, if I am walking right I make a right jump and so on... The last 3 weeks I have been using c++ and directx to make games and so far I can do all the basics needed like load sprites load surfaces draw them and make them move, I thought I should move on to the next which was jumping and I have been stuck all day. I am been looking up different kinds of AI and mathematics, but still no luck that shows the exact easy to understand theory on gravity. Let me reiterate that I started directx/c++ programming 3 weeks ago I have taken 3 c++ classes already, but I have been doing mainly C# in the last 6 months.


Jump while moving forward and backwards should be easy like suggested.

What you should do is check that the person is not in the air before commiting the left and right movement. Then while in the air your horizontal velocity will be constant and all you have to do is maintain that velocity while handling the vertical velocity. That way jumping right will be /\ jumping left will be opposite and jumping stood still will be |.

Mr. Accident Acceleration is constant yes being -9.81m/s/s however you can't just decide to end acceleration mid-air until you have reached terminal velocity, rip-off's example is a bit strange though having another look.

Edit: Spelling -_-

[Edited by - ExcessNeo on January 30, 2009 3:08:48 PM]
Quote:Original post by ExcessNeo

Mr. Accident Acceleration is constant yes being -9.81m/s/s however you can't just decide to end acceleration mid-air until you have reached terminal velocity, rip-off's example is a bit strange though having another look.

But either way, gravitational acceleration should be subtracted from velocity, not the player's acceleration. If you subtract 9.81m/s^2 from the player's acceleration every frame, it is no longer an acceleration constant, but a jerk constant. Not counting air resistance, gravitation produces a constant acceleration, so the player's acceleration should not change unless another force acts upon them (for example, the ground).

For simple jumping physics, air resistance isn't going to be a significant factor. But if you did want to add it to your model, you wouldn't do it by subtracting g from the object's acceleration every frame; for a decent approximation you could introduce an opposing force that scales linearly with the velocity of the object, and just add that to the gravitational acceleration vector to determine the object's acceleration at any given time.
I was able to get this figured out, I reworked the code several times and finally after a day and a half I have completed it. It isn't the best jumping physics by any means, but it works and that is all I wanted. If you want to take a look at it I posted it in this forum.

http://www.gamedev.net/community/forums/topic.asp?topic_id=522772

This topic is closed to new replies.

Advertisement