Python Forum

Full Version: How to simulate gravity?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is there any physic library for Python where I can just toggle enable or disable the physic that I one?
If not ,how do I create a game like stack or simulate minecraft sand or gravel?(if you don't know the game,please search it first) 

Some of the library I have downloaded and currently using is 
1)pygame
2)OpenGL
Can this be use for Gravity simulation?
Minecraft uses block distrubance.
1. if block is remove then effect nieghbors (left, right, front, back, up).
Then sand block checks down. if empty or liquid then moves then send nieghbors distrubance.

2. If sand just entry new block check if it can fall.

gravity is just a const variable that tells how fast the block falls.
Gravity is just a constant acceleration toward the center of a body, on earth this is 9.81 m/s/s which means the velocity of a thing will increase in speed by 9.81 meters per second, every second the particle is in motion. If this is for a space simulation you will have to calculate it yourself using the gravitational constant of 6.674 and the mass of the body.

It should be easy to do, you will need to set the refresh rate of your application specifically or at least know what it is, and using 10 as the constant for gravity is plenty close enough if you want to keep nice easy values to work with. I have not tried this myself, but if I were, i would use one of newtons laws of motion:
v = u +at
v^2 = u^2 + 2as
s = u + 1/2at^2

where:
v = velocity 
u = initial velocity (often 0)
a = acceleration (in your case this will be 9.81 or 10)
t = time (amount of seconds in the air)
s = displacement



Work in seconds and meters, if you are asking this question then work in seconds and meters don't think you will get the right results in hours or minutes, miles or kilometers. Calculate first then convert it to your desired units. Any or the above equations will do, they just depend on what data you have.

I hope this has helped in some way.
I am not aware of a library. Most devs use their own. 

You can see an example in pygame of gravity physics on a player in a platforming scenerio
https://github.com/Mekire/pygame-samples...latforming

If you want to play around with the gravity, just change the value of _Physics.grav
(Jan-24-2017, 09:14 AM)hsunteik Wrote: [ -> ]Is there any physic library for Python

Like this one? http://www.pymunk.org/en/latest/

Or the Open Dynamics Engine has python bindings, if that's something you want to swing with: http://www.ode.org/ode.html