Started by Mar 11 2013 10:43 AM

,
6 replies to this topic

Posted 11 March 2013 - 10:43 AM

Hello there.

This question is about a platform game in box2D but I assume that it would be applicable to any platform game.

At the moment my character jumps by setting his vertical velocity, but I'm at a point where I want to tweak the gravity of the game but not affect the actual distance the player will jump, so I can adjust the feel of the gravity without constantly have to alter the player jump strength.

Gravity force vector in box2D is currently +22 on y.

I want the player body to be pushed upward exactly 3 metres.

The player can double jump, and I want this to happen regardless of what the current velocity of the player is. If the player is falling very quickly, hitting jump will cause them to stop and pop up 3 metres.

Thanks

This question is about a platform game in box2D but I assume that it would be applicable to any platform game.

At the moment my character jumps by setting his vertical velocity, but I'm at a point where I want to tweak the gravity of the game but not affect the actual distance the player will jump, so I can adjust the feel of the gravity without constantly have to alter the player jump strength.

Gravity force vector in box2D is currently +22 on y.

I want the player body to be pushed upward exactly 3 metres.

The player can double jump, and I want this to happen regardless of what the current velocity of the player is. If the player is falling very quickly, hitting jump will cause them to stop and pop up 3 metres.

Thanks

Posted 11 March 2013 - 11:51 AM

Can you code it in such a way that the gravity does not affect the player when jumping?

Posted 11 March 2013 - 02:10 PM

Can you code it in such a way that the gravity does not affect the player when jumping?

As far as I know in Box2D its not possible to define an object affected by all physics except gravity. It's all or none.

Posted 11 March 2013 - 03:27 PM

Think of it in reverse. What is the velocity of an object that has fallen 3 meters in a given gravity? Manipulating the equations for a falling body, v = g * sqrt(2d/g) where d is your 3 meters (or however high you want to jump) and g is your gravity constant. Once you have that velocity, that is the upward velocity your character needs to start at to hit an apex of 3 meters.

Posted 11 March 2013 - 04:05 PM

I guess what you need is to know which linear velocity to apply at the jump so that it reaches X meters high given a gravity, isn't it?

You can use basic newtonian physics / SUVAT equations of motion (http://en.wikipedia.org/wiki/Equations_of_motion):

S = VT + 0.5AT^2

S is the distance traveled at time t (meters)

V is the starting linear velocity (meters / second)

A is acceleration (meters / second ^2)

T is time (seconds)

In your case S is the jumping height and A is the Box2D gravity (as long as it's also in meters/second^2, it really should) and both may take different but known values. Therefore there are 2 unknowns, V and T. With just one equation it can't can't be solved: Sounds logic, the object can be at a distance S with infinte combinations of V and T. But the combination wee need is the one in which the object has velocity 0 when it reaches S meters (just the instant before it starts falling), so we can use one more formula:

V2 = V + AT

V2 is the final velocity (meters / second)

The others are the same as before

In your case we have

0 = V + AT

-AT = V

T = -V / A

Which can be substituted in the first equation:

S = VT + 0.5AT^2

S = V(-V / A) + 0.5A(-V / A)^2

S = -V^2 / A + 0.5 * V^2 / A

A*S = -V^2 + 0.5V^2

A*S = -0.5 * V^2

V = sqrt(A*S/-0.5)

When a jump takes place just calculate V using the appropiate S (desired jump height, always positive) and A (gravity, -22 or whatever but always negative) and apply it to the Box2D body. It won't matter if the object was already moving or not.

I hope this helps,

Lionel

PD: I think the maths are OK but please check them just in case.

PD2: Notice that even though the object will jump up up to S meters it will take more or less time depending on the gravity: slow jumps in low G and quick jumps in high G. This can't be avoided unless you fully control yourself the object's position & motion.

PD2: Notice this equation will work only if A (gravity) stays constant during the whole jump... I mean, If you are planning to change gravity quite often and it may happen at some point DURING the jump... then this won't work. I guess it will just need some more maths (but not too much).

Posted 11 March 2013 - 04:38 PM

Think of it in reverse. What is the velocity of an object that has fallen 3 meters in a given gravity? Manipulating the equations for a falling body, v = g * sqrt(2d/g) where d is your 3 meters (or however high you want to jump) and g is your gravity constant. Once you have that velocity, that is the upward velocity your character needs to start at to hit an apex of 3 meters.

Good thinking. Could you show how you got there? Which equations you used?

Posted 11 March 2013 - 05:40 PM

Good thinking. Could you show how you got there? Which equations you used?

the same ones you did, just different constants (acceleration A = g, S=d distance traveled in time t):

s(t)=v*t+(g*t^2)/2 (with s(t0)=d, at some arbitrary time t0, initial velocity v=0) => d=(g*t0^2)/2

=> **sqrt(2d/g) = t0** (I)

for any time t:

**v(t)=g*t** (II)

is true, by calculating total derivative of s: ds/dt = v (change of distance s per time t is velocity v by definition)

this is especially true for time t0:

(I) substituted into (II): v0 = v(t0) = g*sqrt(2d/g) = **sqrt(2dg)**

q.e.d.

so whats actually calculated here is the speed the object has *after* falling d units (starting at s=0, going to s=d, with initial velocity 0, trying to find end velocity v0).

since ideal newton physics are symmetric in time its the same the other way around.

**Edited by Tasche, 11 March 2013 - 05:56 PM.**