I am trying to create a jump method in XNA, but I am facing a lot of problems, it doesn’t work for me, I’ve been trying doing it for like 2 hours long and still no “luck”. Can anybody give me a code sample, or at least a direction?
Note: I want the jump to be realistic, using gravity and such.
Thank you!
I erased all my work but here’s the latest I tried, I know it shouldn’t work for sure, but still..
public void Jump(GameTime gameTime)
{
float currentTime = (float)0.1;
if (position.Y == 200)
{
position.Y += velocity.Y*currentTime -gravity * (float)(Math.Pow(currentTime,2)) / 2;
}
if (position.Y == 200 + jumpHeight)
{
position.Y -= velocity.Y * currentTime - gravity * (float)(Math.Pow(currentTime, 2)) / 2;
}
}
I see that you’ve learned the equations of motion from your code (which I don’t think was in place when I made my comment). However you’re a little off, your issue is with your time variable. You’re passing in your game time, but then using
.1ffor your time variable. What you really want for your time variable is the time since you started the jump. Further,position.Yis unlikely to ever be exactly equal to200or200 + jumpHeight. It’s a float (I assume), so you can never trust that it’ll be a nice round number. If you want to specify an exact maximum jump height, you’ll have to perform some equations before and set yourvelocity.Yaccordingly (solve for when velocity equals 0 i.e. the top of your jump).So, to fix your original code I think something like this will work, albeit totally untested: