Python physics library?

Are there any good modern physics libraries for Python for Linux? I just get into Python using PyGame, but PyGame doesn't have a physics library, that's not cool. I spent about two hours trying to find a good physical library, but it is like trying to grab oil; I can not do it.

I don't need a physical engine at all; all I want to do is program the object to "jump" up and then fall back to the ground. It seems that there are some simple collisions (which PyGame can handle, I think), but this is the actual calculation of the jump that surpassed me. If it turns out that there are no good physics libraries, the problem seems simple enough that I can just try to find the basic equation of acceleration and the equation of gravity and try to apply them ... I would like to avoid for this nonetheless.

Thanks for any help.

+8
python pygame physics
source share
4 answers

All the basic kinematic equations of physics are all you need. Despite the fact that the questions had already been answered, if I were you, I still did it manually, because using the library seems redundant. Run the equation for speed:

velocity = initial velocity + (acceleration * time) 

From there we integrate to find the position:

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

Your jump is designed to calculate the y-position of the character, so just use this equation to calculate the y-position and the toy with an initial speed and acceleration. The standard acceleration due to gravity is -9.8 meters per second ^ 2 (at least on the surface of the earth - these are different different planets of the planet). Start from the starting position regardless of your character, or 0 if the earth is 0.

So:

 y = vt + (-4.9)t^2 

Choose an initial speed v to provide upward movement at the start of the jump, and t should be the elapsed playing time since it started to jump.

For this you need only one line of code, no libraries needed!

EDIT: what to do when you land.

So, in the real world, acceleration is caused by unbalanced forces. Gravity always acts on you, but when you stand in the earth, it is counteracted and canceled by the "normal force" of the earth. As long as the ground you are standing on is strong enough to support your weight, the ground will push back and resist gravity, and you will not accelerate down. Therefore, in your game, if you are not really simulating forces, simply change the acceleration from -9.8 to 0 when your character touches the ground. If the ground is flat, your jump ends when position = initial position

0
source share

Pymunk is another promising thing you can take a look at.

+5
source share

Try pyODE, this is a bunch of python open dynamic engine.

You can find more information here.

+4
source share

According to the ODE website installation instructions , the ODE package itself now contains CPython-based Python bindings; and pyODE bindings are deprecated. Installation instructions are on the page above.

By default, this is for Python 2, but I was able to do this work with Python 3, too, with a minimal amount of work (Mac OS X). I could even run tutorials.

It may not be off topic, but just for the record, here's what I had to change:

  • I had to modify OpCode.h by uncommenting #defines for sqrt , sin , cos , asin and acos (lines 33-37, file version: March 20, 2001), This is the ugly hack I need, because without it ODE itself didn't intend with double precision arithmetic to be used with 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 

What is it.

0
source share

All Articles