# 2D animated sprite jumping and gravity physics [duplicate]

I'm currently writing a Megaman clone for my school project. So far all I did was making the character run and stay put when needed. Know I want to be able to make him jump using the 'X' button. I already have the animation sheet of the jump. But I really can't figure out how to do it and how the physics work after reading so much about it both here and other places. I think you get what I mean. From the state of staying put or running, going to a jumping position and landing. Also to be able to press left/right and the jump button to jump in the direction(I know It's stupid that I explain all of this because you probably know all this but just in case). Anyway this is my code for Game1: http://pastebin.com/zYM5gnRD And this is the code for my Animated Sprites class: http://pastebin.com/5H36YAdb I'm not saying it's the smartest writing, but it's only a school project so I need it to work so I'm fine with it. Thanks a lot, a lot in advance for those who read this and are going to help.

## marked as duplicate by Panda Pajama, bummzack, MichaelHouse♦Mar 25 '15 at 21:43

• You may receive a better response if you edit this to provide stronger focus on a specific question and include any relevant code snippets. As an aside when starting XNA I got a lot of value from studying the Platformer starter kit (now phased out but a backup is available here. – Kelly Thomas Oct 4 '14 at 23:09
• If you're interested in the physics, you may want to take a look at this question – Panda Pajama Mar 25 '15 at 9:15

http://www.rodedev.com/tutorials/gamephysics/

float jumpHeight = 4; //defining the jump height isn't needed. You need only determine the y velocity versus gravity. I suppose that jumpHeight could be the y velocity. You can use trigonometry to determine the jump velocity at a specific angel as shown on the link I provided. Grant you this isn't exactly in xna persay, but programming has sort of a universal explanation once you learn one then it isn't hard to pick up on the others contextually.

var velocity.y = 4;
var gravity = 1;
var moving = true;

function GameLoop()
{
if(moving == true){
velocity.y = velocity.y - gravity;
}
}


believe that more or less covers the gravity piece you asked about anyhow. It's all in that link I posted. If you aren't jumping high enough turn up the velocity.y if you are reaching the height of your jump and then zipping down way to fast decrease gravity. Will take some playing with to get it right. Perhaps if you have already created the game environment in which you intend to move Megaman on you could load the enviroment and do your own alpha testing :-)