puelo
puelo

Reputation: 6007

Increasing gravitiy, but preserving height and distance of jump

I am currently working on a android game and wanted to add some variety to the gameplay by speeding up the overall movement (just jumping). I find the acceleration that the gravity of box2d added nice, so want to keep it. Increasing the gravity should help me to speed up the jump. But how do i calculate the respective x and y velocities so i preserve the height and the distance of the jump, so it remains the same. I found two different formulas to calculate something like this, but there are still some problem. The first would be:

g*h*m = v

This could be used to preverse the height.

v = (sqrt(d) * sqrt(g)) / sqrt(sin(2*angle))

Where d would be the distance, therefore this could be used the preserve the distance. The problem with both of them is, that because the gravity would be something like (0, 8), the x-value would never make sense. Is there some easy way to combine both methods, or just another formula i didn't find? I am also in for completly new ideas. Thanks a lot!

Upvotes: 0

Views: 376

Answers (3)

puelo
puelo

Reputation: 6007

Thanks for both of your answers, but that wasn't quite what i was looking for. I got is solved like this:

double velocity = (Math.sqrt(Entity.distance) * Math.sqrt(Math.abs(this.physicsWorld.getGravity().y))) / Math.sqrt(Math.sin(2*Entity.angle));
Entity.MAX_VELOCITY_X = (float) (velocity * Math.cos(Entity.angle));
Entity.MAX_VELOCITY_Y = (float) (velocity * Math.sin(Entity.angle));

I put this in the Update method of the entity, where distance is the distance i want the Entity to travel. Velocity and angle are the values for the polar-coordinates.

Upvotes: 1

iforce2d
iforce2d

Reputation: 8272

This might help: http://www.iforce2d.net/b2dtut/projected-trajectory See the section titled "How fast should it be launched to reach a desired height?"

Horizontal distance covered is just the horizontal part of the velocity multiplied by the time the jump takes. So if you increase gravity to make the jump take half as much time, you would need twice the horizontal speed to preserve the sideways distance of the jump.

Upvotes: 0

Shefchenko
Shefchenko

Reputation: 113

in onManageUpdate() method you can try this way -

                vel = playerBody.getLinearVelocity();
                if (vel.y < MAX_VELOCITY) {
                    playerBody.applyLinearImpulse(0, SPEED_JUMP, playerBody.getWorldCenter().x, playerBody.getWorldCenter().y);
                }

and only change the SPEED_JUMP for speeding up the jump.

Upvotes: 0

Related Questions