All computer source code presented on this page, unless it includes attribution to another author, is provided by Ed Halley under the Artistic License. Use such code freely and without any expectation of support. I would like to know if you make anything cool with the code, or need questions answered.
# physics - a simple framework for very lightweight physics simulations


A simple framework for very lightweight physics simulations.


    This library creates a basis for simulating physical interactions
    between simple bodies.  Suitable for little pygame usage, or demos.
    For much more full-featured physics, check out the PyODE bindings.


    Ed Halley ( 17 October 2007


import math
import vectors ; from vectors import *
import interpolations ; from interpolations import *


class Body (object):
    '''The base class of all simple physical entities.'''

    def __init__(self):
        '''There are no members in the abstract root body.'''

    def collides(self, other): return False
    def attracts(self, other): return 0.0


class ParticleBody (Body):
    '''The simplest body has position and mass but no size or orientation.'''

    def __init__(self):
        super(ParticleBody, self).__init__()
        self.position = V()
        self.mass = 1

class RigidBody (ParticleBody):
    '''A physical body that has size and orientation but no flexibility.'''

    def __init__(self):
        super(RigidBody, self).__init__()
        self.orientation = Q()
        #TODO: inertial distribution of mass
        #TODO: circumscribed radius (radius of farthest matter)

class ComplexBody (Body):
    '''A multi-body system, such as particles. Bodies are independent.'''

    def __init__(self, *args):
        super(ComplexBody, self).__init__()
        self.particles = set([])
        for arg in args:

class SpringBody (ComplexBody):
    '''A two-body system, with a classic spring distance relationship.'''

    def __init__(self, this, that, ideal=1.0, stiff=1.0):
        super(SpringBody, self).__init__(this, that)
        self.ideal = ideal
        self.stiff = stiff

class ElasticBody (ComplexBody):
    '''A single body comprised of many spring-attached massive particles.'''

    def __init__(self, bodies, springs):
        super(ElasticBody, self).__init__()
        self.springs = set([])
        # need caller to offer list of spring pairs or a SpringBody per pair


class ReferenceFrame (object):

    def visit(self, time):
        self.time = time
        self.bodies = {} # { body : [force,accel,torque] accumulator }

    # list of objects
    def advance(self, time):
        dt = time - self.time
        if zero(dt):
        # for each body,
        #     accumulate forces
        # for each body,
        #     see if moving f=ma would collide before or at time
        # for each collision,
        #     figure actual time of each collision
        # recurse self.advance(time of earliest collision)
        # recurse self.advance(original target time)



def __test__():
    from testing import __ok__
    print 'Testing physical simulation classes...'

if __name__ == '__main__':
    raise Exception, \
        'This module is not a stand-alone script.  Import it in a program.'

Contact Ed Halley by email at
Text, code, layout and artwork are Copyright © 1996-2013 Ed Halley.
Copying in whole or in part, with author attribution, is expressly allowed.
Any references to trademarks are illustrative and are controlled by their respective owners.
Make donations with PayPal - it's fast, free and secure!