|
Programmer's Notebook |
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.
python/ bindings.py boards.py buzz.py cache.py cards.py constraints.py csql.py english.py getopts.py gizmos.py goals.py improv.py interpolations.py namespaces.py nihongo.py nodes.py octalplus.py patterns.py persist.py physics.py pids.py pieces.py quizzes.py recipes.py relays.py romaji.py ropen.py sheets.py strokes.py subscriptions.py svgbuild.py testing.py things.py timing.py ucsv.py useful.py uuid.py vectors.py weighted.py java/ CSVReader.java CSVWriter.java GlobFilenameFilter.java RegexFilenameFilter.java StringBufferOutputStream.java ThreadSet.java Throttle.java TracingThread.java Utf8ConsoleTest.java droid/ ArrangeViewsTouchListener.java DownloadFileTask.java perl/ CVQM.pm Kana.pm Typo.pm cxx/ CCache.h equalish.cpp |
Download physics.py
|
# physics - a simple framework for very lightweight physics simulations ''' A simple framework for very lightweight physics simulations. ABSTRACT 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. AUTHOR Ed Halley (ed@halley.cc) 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.''' pass 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) pass 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: self.particles.add(arg) 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 pass #---------------------------------------------------------------------------- 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): return # 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) pass pass #---------------------------------------------------------------------------- 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
ed@halley.cc. 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. |
|