# Platformer AI Jump Calculation

Thank you for taking the time to read my question, I will try to keep it as concise as I can.

I am making a 2D-platformer game. I am in the process of programming AI that can intelligently traverse their terrain. AI are able to jump, in a fashion similar to many other 2D-platformers:

• When the AI jumps, it is given a fixed initial Y-Velocity.
• The AI has a flexible time where it may ignore gravity to give it a variable jump height/air time. (This mimics the way that the player is able to jump, where the player character may jump higher if the player holds down the jump-button longer.)
• After this time expires, or the AI "releases the jump button", gravity begins to affect the jump arc. The acceleration rate of gravity is also constant.
• The velocity on the X-axis is constant.

Question: How would an AI find the minimum amount of time that it will have to "hold down its jump button" to clear an expanse between a starting jump point, and a desired target?

• You should provide some code as well as what you tried and why it didn't work so that we can more accurately help you the problem. Commented Aug 26, 2016 at 8:01
• Take a look at the command pattern which won't directly help but it will help you have generic "jump" methods etc for both player and AI. As for this specific problem you may want to look at Ray Casting or perhaps just do some simpler collision PREVENTION. Have your AI character poll for any upcoming blocks/terrain and you probably only need the Y position of that tile, then its just a bit of trig or other maths and make sure they get enough velocity both up and across to make the gap. Commented Aug 26, 2016 at 8:02

If you fix the point you jump from and the point you want to land at you can divide the movements into two parts (t1 & t2 in the equautions) and get a system of 2 equations, which you can solve (under the assumption that all length have the right sign).

I chose the smaller of the two roots, because the second should discribe jumping past the point and then falling backwards with negative t2, which doesn't make sense.

You might have to pad the solution a bit, since your physic system is probably not continuous and thus you probably cannot start and stop jumping at the exact moment (or collide with the second platform, if your landing isn't right on the edge)

• I'm sorry to trouble you again - I'm trying to write this out in code, and realized that I can't exactly write it out because solving for t1 requires knowing what sy is, but solving sy requires knowing what t2 is, which requires knowing what t1 is. How would I write this out in code? Commented Sep 9, 2016 at 17:15
• Or maybe I'm misunderstanding what your variables are supposed to be. I'm reading 'sx' as being 'xDistance', but maybe it would more accurately be 'coordinate of target x'? Which would in turn mean that 'sy' might be 'coordinate of target y'? If that's the case, solving for t1 becomes much easier. Commented Sep 9, 2016 at 17:33

The order in which you compute values in this problem can create different edge cases.

The above answer assumes knowledge of s_x and s_y, the x and y distances you want to travel in the air during your jump. You do know s_y based on the vertical distance between platforms (assuming your platforms are flat). This is just a word of warning that the optimal s_x may be trickier to determine.

You cannot necessarily assume that you will jump from the very edge of the ledge you are on. If the platform is narrow and low you may jump over it and into a gap on the opposite side of it. You also cannot necessarily assume that you will land on the near side of the destination platform. Supposing the platform you are on is also very small, you may not be able to jump from far enough back to land on the front of the destination platform. There is also the case where you have to jump over a wall (the platforms have almost no x gap and a wide y gap). In this case, if you need to maintain your horizontal velocity, you'll need to jump significantly earlier than required by the gap.

Additional information about your problem might make these issues simpler to handle. Do you know that either platform is particularly large? Can you stop moving forward in midair? Do you want to maintain maximum x velocity whenever possible (for speed-running, say)?

In the most general case if the two platforms cover x-values in segments [x_0,x_1] and [x_2,x_3], then you at least have bounds for s_x. (x_3 - x_0) >= s_x >= (x_2 - x_1).

In general, you want to pick the smallest s_x possible, it just might be the case that there's such a thing as an s_x that is too small.