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.
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)/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 + (-4.9)t^2
Pick an initial velocity v
value to provide the upward movement when the jump starts, 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, 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. If the ground is flat, your jump ends when position = initial position
Pymunk is another promising one that you might want to take a look at.
Try pyODE, it is the python binding of open dynamic engine.
You can find more information here
According to the ODE website's installation instructions, the ODE package itself now contains Python bindings based on CPython; and the pyODE bindings are considered obsolete. Installation instructions are included in the above page.
By default this is for Python 2, but I was able to make this binding work with Python 3, too, with a minimum amount of work (Mac OS X). I could even run the tutorials.
This might be out of topic, but just for the record, here is what I had to change:
- I had to change
OpCode.h
by uncommenting the #defines
for sqrt
, sin
, cos
, asin
and acos
(lines 33-37, file version: March 20, 2001). This is an ugly hack that I needed because without this ODE itself did not compile with double precision arithmetic, which one needs to use with the python bindings if we can trust the documentation on the ODE page.
I had to change setup.py
by adding the following lines after line 18:
# bugfix: in Python3 read() returns bytes, which need to be converted
# to strings
try:
ode_cflags = [x.decode("utf-8") for x in ode_cflags]
ode_libs = [x.decode("utf-8") for x in ode_libs]
except:
# in Python2 we just continue
pass
To run the tutorials, in the demos
directory I used
$ 2to3 -w *.py
That's it.