Sign in to follow this  
Mania-92

Jumping in SDL animation tutorial (lazyfoo)

Recommended Posts

I'm following the SDL tutorials of lazyfoo, right now the animation tutorial. Well I have my foo walking great but I also want it to jump, not a simple jump right up but if the foo is walking right (for example) that it jumps with a bow to the right you know. I've no idea where to begin so if anyone could point me in the right direction that would be great, thanx.

Share this post


Link to post
Share on other sites
If jumping and moving are handled separately then this should be taken of automatically. What I mean is that even when the character is jumping, you should still be able to move it left and right (i.e., move it while it's still in mid air).

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
If jumping and moving are handled separately then this should be taken of automatically. What I mean is that even when the character is jumping, you should still be able to move it left and right (i.e., move it while it's still in mid air).

Then first I need to make it jump upwards, also got no idea how to "move" it up (jump) smoothly. Guess I could make it go a little bit upwards every second (or millisecond..) but I guess there must be a better way.

Share this post


Link to post
Share on other sites
Not sure what you mean. Off the top of my head I would do something like this:

if (keyDown(KEY_RIGHT))
xPos += xVel;
if (keyDown(KEY_SPACE)) {
jumping = true;
oldY = yPos;
}
if (jumping) {
jumpSum += jumpSpeed; tracks the amount of jumping
if (jumpSum >= jumpHeight)
jumpSpeed = -jumpSpeed;
if (jumpSum <= 0) {
jumpSum = 0; // Reset for next time
jumpSpeed = -jumpSpeed; // Or: jumpSpeed = abs(jumpSpeed);
yPos = oldY - jumpSpeed; // In case jumpSum was > 0 and is now < 0
jumping = false;
}
yPos += jumpSpeed;
}
This doesn't take collision detection into account and may have some other problems, but it might be a good start.

Share this post


Link to post
Share on other sites
Take a look at the motion tutorial. Now imagine that you add gravity. each frame, the dot falls, by adding (-9.81 / framerate) to its yVel.
Then, imagine that, following a certain event such as a keypress, you add 10 to the yVel. The dot will appear to "jump" and the jump will be in the direction it is already moving in. But, because you add gravity on each frame, the dot will follow a "parabola". A parabola is a curve where the change in each direction is a result of a seperate formula.

What I recommend you do, which is what I do, is to create a class similar to the Foo, but without the event handling. It simply knows how to load an image, break it into separate frames, and display the correct one depending on the current frame. Test this until it works. It should have an update() method.

Then, create a character class, using the motion tutorial. Instead of drawing a dot, your class should own a "foo" as one of its private methods, and then when you call show() it will call foo.show(). When this class updates, it calls foo.update() to ensure its animation is updated as well.
Instead of event handling for moving your character, I would (and do) use keystates, as demonstrated in the keystate tutorial.

This is an SDL version of the system I use in 3d, except I have further layers of abstration. I'm using OpenGL and my characters are MD2 animations, but the principle is the same. Each class does one specific job.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this