idish
idish

Reputation: 3260

Creating a jump function

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;
        }
    }

Upvotes: 0

Views: 1848

Answers (2)

House
House

Reputation: 3496

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 .1f for your time variable. What you really want for your time variable is the time since you started the jump. Further, position.Y is unlikely to ever be exactly equal to 200 or 200 + 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 your velocity.Y accordingly (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:

   public void Jump(GameTime gameTime)
   {
       if(jumping && position.Y > groundLevelAtPlayer) {
           //Get the total time since the jump started
           float currentTime = gameTime.totalTime - character.timeofjumpStart;
           //gravity should be a negative number, so add it
           position.Y += (velocity.Y * currentTime)
               + (gravity * ((float)(Math.Pow(currentTime, 2)) / 2));
       }
       else 
       {
           jumping = false;
       }
   }

Upvotes: 1

npinti
npinti

Reputation: 52185

How exactly do you want to make your player jump? Since you mentioned realism I am assuming that you want your player to jump and move across both the X and Y axis, so that it will move along an arc(parabola if you will).

If that is what you are after, you will need to replicate projectile motion. This previous SO thread contains various methods in which you could implement such motion.

Edit: You should be able to use the same equations presented. For vertical jumps, the angle will be 90 degrees, or pi/2 if you are working with radians. If you are pressing your direction keys, you will have to use the same equations. The angle at which you want your player to start the jump will have to be selected by yourself. Usually maximum range is obtained at an angle of 45 degrees (pi/4) assuming that the same force is used, so your option is really between 0 and 45 degrees.

Upvotes: 0

Related Questions