Elliot Bonneville - 2 years ago 212
Python Question

# Python physics library?

Are there any good up-to-date physics libraries for Python that are for Linux? I'm just getting into Python using PyGame, but PyGame's lack of a physics library isn't cool. I spent about two hours trying to find a good physics library but it's like trying to grab oil; I can't seem to do it.

I barely need a physics engine at all; all I want to do is program an object to 'jump' up and then fall back to the ground. There seems to be some simple collisions going on (which PyGame can handle, I think) but it's the actual jump calculation that's stumping me. If it turns out that there aren't any good ususable physics libraries, the problem seems simple enough that I might just try to find a basic acceleration equation and a gravity equation and try to apply those... I'd like to avoid having to do that, though.

Thanks for any help.

Answer Source

The basic physics kinematic equations are all you need. Even though the questions was already answered, if I were you, I'd still do it by hand just because using a library seems like overkill. Start the equation for velocity:

``````velocity = initial velocity + (acceleration * time)
``````

From there, we integrate to find position:

``````position = initial position + (initial velocity * time) + (acceleration * time^2)
``````

Your jump is looking to calculate the y position of the character, so just use that equation to calculate y position and toy with initial velocity and acceleration. Standard acceleration due to gravity is -9.8 meters per second^2 (at least on the surface of the earth - it's different son different planets). Start with initial position whatever your character is at, or 0 if the ground is 0 to you.

So:

``````y = vt + (-9.8)t^2
``````

Pick a `v` value, and t should be the elapsed game time since he started jumping.

You only need one line of code to do this, no libraries necessary!

EDIT: dealing with what to do when you land.

So in the real world, acceleration is caused by unbalanced forces. Gravity is always acting on you, but when you're standing in the ground, it's being countered and canceled out by the "normal force" of the ground. As long as the ground you're standing on is strong enough to support your weight (weight being technically the force due to gravity of a given mass), the ground will push back up and counter gravity and you will not accelerate downward. So in your game, if you're not actually simulating forces, just change the acceleration from -9.8 to 0 when your character is touching the ground.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download