lab_geometry/simulation/gravity.py

14 lines
518 B
Python

def get_gravity_force(a, b, g):
"""Gravity is a force of attraction between objects.
The attraction between two objects is equal to their masses
multipled together, divided by the square of the distance between them,
times a constant g. This is the real-life definition of gravity.
"""
a_to_b = b.position - a.position
distance = a_to_b.mag()
force = a_to_b.scale(g * a.mass * b.mass / (distance * distance))
return force
a.acceleration += force
b.acceleration -= force