14 Replies - 4809 Views - Last Post: 13 April 2011 - 06:37 AM Rate Topic: -----

#1 MrSt0ne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 29-March 11

C++ SDL sprite jump

Posted 11 April 2011 - 06:27 PM

I was giving this a try on my own, and I managed to get the sprite image to move up on the 'Space' button. But the sprite will not come back down. I'm probably tackling this the wrong way, but could this be a start? And if anyone has any experience with sprite jumping, your knowledge would be appreciated.

Thanks.
if(keys[SDLK_SPACE])
		{
                        int jumpHeight = 15;
                        int jumping = 0;
			while(jumping++ < jumpHeight && playerJump == false)
			{
				if(jumping < jumpHeight)
				{
					playerJump = true;
					dstY -= jumpHeight;
					if(jumping >= jumpHeight)
					{
						playerJump = false;
						dstY += jumpHeight;
					}
				}
			}
		}


This post has been edited by MrSt0ne: 11 April 2011 - 06:28 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ SDL sprite jump

#2 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: C++ SDL sprite jump

Posted 12 April 2011 - 04:54 AM

if(jumping < jumpHeight)
                {
                    playerJump = true;
                    dstY -= jumpHeight;
                    if(jumping >= jumpHeight)
                    {
                        playerJump = false;
                        dstY += jumpHeight;
                    }
                }


This second if loop will never be true, as jumping or jump height will not change during the first if loop.

Also, on coming back to this loop to start making it come down, jumpHeight and jumping will be reset to their original values.
And you would have to be holding the space bar down if you want the guy to come down.

So my advice is to split the if statements into their own, and put them into a different bit of code and add a bool for if space has been pressed, and set it to false when it hasn't

Hope This Helps :)
Was This Post Helpful? 1
  • +
  • -

#3 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 999
  • View blog
  • Posts: 4,177
  • Joined: 14-February 08

Re: C++ SDL sprite jump

Posted 12 April 2011 - 09:36 AM

This can be achieved a lot easier by modelling gravity, objects fall at 9.8 metres per second per second and you can change the seconds to your frame time. When you want your character to jump you can apply an upward force and then your gravity will act upon it until it returns to the ground.

// apply gravity
acceleration.y -= gravity * frametime;

// update velocity
velocity += acceleration * frameTime;

// update position
position += velocity * frameTime;



You will still have some logic to do in your jumping code like sk1v3r suggested to make sure you cannot jump while already jumping.

This post has been edited by stayscrisp: 12 April 2011 - 09:38 AM

Was This Post Helpful? 1
  • +
  • -

#4 MrSt0ne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 29-March 11

Re: C++ SDL sprite jump

Posted 12 April 2011 - 11:32 AM

Is there an example I can look at somewhere ? I'm not trying to just copy paste, I have really gotten into wanting to make a small platform jumper game, but for me to understand things, it is easier watching a video of it being done, or to look at an example.

Also, I know there are lots of people out there that can't find a easy to understand way of doing this. So once I learn how to and understand it 100%, I'm going to make a video for everyone that is having trouble.

This post has been edited by MrSt0ne: 12 April 2011 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

#5 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 999
  • View blog
  • Posts: 4,177
  • Joined: 14-February 08

Re: C++ SDL sprite jump

Posted 12 April 2011 - 11:48 AM

What is it exactly that you don't understand?
Was This Post Helpful? 0
  • +
  • -

#6 MrSt0ne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 29-March 11

Re: C++ SDL sprite jump

Posted 12 April 2011 - 11:58 AM

Putting it all together the proper way to make it work. I can understand it much easier if I have a working example to look at.

Like the gravity, acceleration and frametime variables,, where do you get those from or how do you know what to set them as?

This post has been edited by MrSt0ne: 12 April 2011 - 12:01 PM

Was This Post Helpful? 0
  • +
  • -

#7 MrSt0ne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 29-March 11

Re: C++ SDL sprite jump

Posted 12 April 2011 - 12:29 PM

So the first thing I would have to do is apply some sort of gravity to my sprite image ?

That way, gravity is always applied to that image, not just when I hit the space bar to jump ?
Was This Post Helpful? 0
  • +
  • -

#8 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 999
  • View blog
  • Posts: 4,177
  • Joined: 14-February 08

Re: C++ SDL sprite jump

Posted 12 April 2011 - 12:48 PM

How are you moving your character when it's not jumping? Are you using vectors?
Was This Post Helpful? 0
  • +
  • -

#9 MrSt0ne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 29-March 11

Re: C++ SDL sprite jump

Posted 12 April 2011 - 12:51 PM

if(keys[SDLK_LEFT])
{
    dstX -= 2;

}

That is how I'm moving the character at the moment, moving left and right is not a problem.
I did some more searching and found a simlilar post to what I have started. So maybe that could help me too.

http://www.dreaminco...character-jump/
Was This Post Helpful? 0
  • +
  • -

#10 MrSt0ne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 29-March 11

Re: C++ SDL sprite jump

Posted 12 April 2011 - 01:00 PM

Here is what I have so far. But when I hold the space bar down, my sprite will continue to move up the screen. Also when I let go of the space bar, my sprite will not return to his original y position.
float jumpVelocity = 2;
bool playerJumping = false;
float gravity = 0.2f;

if (keys[SDLK_SPACE] && !playerJumping)
{ playerJumping = true; }
			
if (playerJumping)
{
	dstY -= jumpVelocity;
	jumpVelocity -= gravity;
}


Was This Post Helpful? 0
  • +
  • -

#11 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: C++ SDL sprite jump

Posted 12 April 2011 - 02:02 PM

if that is all declared in that point when the logic gets there, then jump velocity will always be 2.

to put it simply the way that I would do this is a bit like in this link
Now I know, this is about ragdolls, but if you look at the forces and look at the way that gravity is applied to the download movement, then you should understand a little about what stayscrisp was trying to say :)

While I know that all of it isn't relevant, you wanted an example and I believe that there is a deal of relevance in the part that I pointed out :)
Was This Post Helpful? 0
  • +
  • -

#12 MrSt0ne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 29-March 11

Re: C++ SDL sprite jump

Posted 12 April 2011 - 03:12 PM

Wow,, that seems pretty confusing to me. Like you said, I think there is tons of stuff in there that I don't need to worry about, but that make it confusing for me. I'm trying hard to figure this out, and I've been banging my head for a couple days now. If anyone else wants to chime in, your more than welcome. In the mean time, I will continue to look at the ragdoll example you gave me. Thanks.
Was This Post Helpful? 0
  • +
  • -

#13 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: C++ SDL sprite jump

Posted 12 April 2011 - 04:58 PM

uhhh ,,, sorry for confusing you :/
my point was the small bit about acceleration.
if you have a constant gravity force of say, 0.1, working on the character every frame, then the acceleration will be + 0.1.

Therefore, your character will always be falling down unless he is in contact with the ground or something.

The benefit of this article was that you could add a one-time force of say -15 to the character to make it go up, and you wouldn't need to worry about it anymore, because
y += y - oldY + ay;


so ay is acceleration. If it is only gravity, then the character moves down as much as it did last frame, plus gravity.

So if the up force is 15 then it will move up by fifteen, then less and less as gravity works on it.
This way you add the force of -15 once, and the character jumps up but comes down naturally with gravity.
Was This Post Helpful? 0
  • +
  • -

#14 MrSt0ne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 29-March 11

Re: C++ SDL sprite jump

Posted 12 April 2011 - 05:17 PM

Thanks sk1v3r,
I will see what I can do now. I feel kinda dumb. I never thought about the sprite always having the gravity applied to it. I thought I would only need it when the player jumps. But this way is much better. I'll go pound my head for a bit til I get this working.

Thanks again.
Was This Post Helpful? 0
  • +
  • -

#15 MrSt0ne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 29-March 11

Re: C++ SDL sprite jump

Posted 13 April 2011 - 06:37 AM

Thanks everyone for helping out with this. You wouldn't believe how excited I got when my little sprite character was able to jump up and down in my program. The gravity phsyics aren't the best, but it works for now.
Here are some parts of the code that is what I used to get this to work.
// Variables
int velX , velY;
const int gravity = 1;
// Player function
void draw_playerSprite(int srcX, int srcY, int xx, int yy, int width, int height, SDL_Surface* source, SDL_Surface* destination)
{
	SDL_Rect src;
	src.x = srcX;
	src.y = srcY;
	src.w = width;
	src.h = height;
	
	SDL_Rect dst;
	dst.x = xx;
	dst.y = yy;
	dst.w = width;
	dst.h = height;
	SDL_BlitSurface(source, &src, destination, &dst);
}
// Player Gravity
velY += gravity;
xx += velX;
yy += velY;
if(keys[SDLK_SPACE] && yy >= groundHeight)
	velY = -15;

// Apply image
draw_playerSprite(srcX, srcY, xx, yy, width, height, playerSprite, screen);



If anyone needs help, please send me a PM and I can try to help you as well. Just make sure you have SDL setup properly first. Thanks again to all those that helped. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1