7

Long story short, I'm making a platform game. I'm not old enough to have taken Calculus yet, so I know not of derivatives or integrals, but I know of them. The desired behavior is for my character to automagically jump when there is a block to either side of him that is above the one he's standing on; for instance, stairs. This way the player can just hold left / right to climb stairs, instead of having to spam the jump key too.

The issue is with the way I've implemented jumping; I've decided to go mario-style, and allow the player to hold 'jump' longer to jump higher. To do so, I have a 'jump' variable which is added to the player's Y velocity. The jump variable increases to a set value when the 'jump' key is pressed, and decreases very quickly once the 'jump' key is released, but decreases less quickly so long as you hold the 'jump' key down, thus providing continuous acceleration up as long as you hold 'jump.' This also makes for a nice, flowing jump, rather than a visually jarring, abrupt acceleration.

So, in order to account for variable stair height, I want to be able to calculate exactly what value the 'jump' variable should get in order to jump exactly to the height of the stair; preferably no more, no less, though slightly more is permissible. This way the character can jump up steep or shallow flights of stairs without it looking weird or being slow.

There are essentially 5 variables in play:

h -the height the character needs to jump to reach the stair top<br>
j -the jump acceleration variable<br>
v -the vertical velocity of the character<br>
p -the vertical position of the character<br>
d -initial vertical position of the player minus final position<br>

Each timestep:<br>
j -= 1.5;          //the jump variable's deceleration<br>
v -= j;            //the jump value's influence on vertical speed<br>
v *= 0.95;         //friction on the vertical speed<br>
v += 1;            //gravity<br>
p += v;            //add the vertical speed to the vertical position<br>

v-initial is known to be zero<br>
v-final is known to be zero<br>
p-initial is known<br>
p-final is known<br>
d is known to be p-initial minus p-final<br>
j-final is known to be zero<br>

j-initial is unknown<br>

Given all of these facts, how can I make an equation that will solve for j?

tl;dr How do I Calculus?

Much thanks to anyone who's made it this far and decides to plow through this problem.

Edit: Here's a graph I made of an example in Excel. alt text

I want an equation that will let me find a value for A given a desired value for B. Since the jump variable decreases over time, the position value isn't just a simple parabola.

Zane Geiger
  • 277
  • 1
  • 3
  • 10
  • 1
    This, I reckon, belongs to either http://math.stackexchange.com/ or http://gamedev.stackexchange.com/ – soulseekah Dec 21 '10 at 06:42
  • Whatever you do, do not mix up math.stackexchange.com with mathoverflow.com! Also if you do post this on another site, post a link here so that interested Stack Overflow members can see the discussion there. – Tyler Dec 21 '10 at 09:50
  • 1
    Didn't know there was a gamedev.stackexchange, but that aside; congratulations on asking a clear, cogent and well formatted question. +1 I say. Well done :) – Binary Worrier Dec 21 '10 at 16:41
  • Maybe I've completely missed the question, but why can't you just run through your jump loop until you reach the appropriate height, and use the "time" value obtained from the terminal iteration? I'm assuming the input from the user is segregated into frames or something similarly discrete; it should be fairly trivial to find the appropriate number if you've already got the character jumping in response to player input. – Matt Mills Dec 22 '10 at 05:47

3 Answers3

1

There are two difficulties in play here. The first is that you don't actually have j -= 1.5, you have j = max(0, j - 1.5). That throws somewhat of a wrench into calculations. Also, your friction term v *= 0.95 makes direct solution difficult.

I would suggest using a lookup table for this. You can precalculate the desired a for each possible b, by trial and error (e.g. binary search on the values of a that give you the required b). Store the results in a table and just do a simple table lookup during the game.

Keith Randall
  • 22,985
  • 2
  • 35
  • 54
1

If I neglect the friction term, and assume that j reaches zero before v reaches zero, I get after a page of calculations that:

b = 1/(8*(deceleration^2)*gravity)*j0^4 - 1/(6*deceleration^2)*j0^3

the solution to this is quite long, but equal approximately (for 10 < b < 400) to:

j0 = (10*(deceleration^2)*gravity*b)^0.25
tsvikas
  • 16,004
  • 1
  • 22
  • 12
  • Wow, thanks for your dedication! I'll test this to ensure it works, and thanks again for contributing. I'm just unclear on what term deceleration corresponds to. – Zane Geiger Dec 22 '10 at 18:52
  • it's the constants from your text. deceleration=1.5, gravity=1 – tsvikas Dec 22 '10 at 23:45
1

After extensive use of Excel 2010 and its Seek Goal function, I was able to make a table of values, and Excel gave me an approximate trendline and equation for it, which I tweaked until it worked out. The equation is j = 3.35 * h ^ 0.196, where j is the initial jump force and h is the height required to jump. Thanks for your help.

Zane Geiger
  • 277
  • 1
  • 3
  • 10