tutorial1.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python
  2. # http://pyode.sourceforge.net/tutorials/tutorial1.html
  3. # pyODE example 1: Getting started
  4. # modified by Gideon Klompje (removed literals and using
  5. # 'ode.Mass.setSphereTotal' instead of 'ode.Mass.setSphere')
  6. import ode
  7. # Simulation constants
  8. GRAVITY = (0, -9.81, 0)
  9. SPHERE_RADIUS = 0.05
  10. SPHERE_MASS = 1.0
  11. SPHERE_START_POS = (0, 2, 0)
  12. SPHERE_FORCE = (0, 200, 0) # Initial force to apply to the sphere
  13. TIME_STEP = 0.04
  14. TIME_STOP = 2.0 # When to stop the simulation
  15. # Create a world object
  16. world = ode.World()
  17. world.setGravity(GRAVITY)
  18. # Create a spherical body inside the world
  19. body = ode.Body(world)
  20. mass = ode.Mass()
  21. mass.setSphereTotal(SPHERE_MASS, SPHERE_RADIUS)
  22. body.setMass(mass)
  23. body.setPosition(SPHERE_START_POS)
  24. body.addForce(SPHERE_FORCE)
  25. # Do the simulation...
  26. if __name__ == "__main__":
  27. total_time = 0.0
  28. while total_time < TIME_STOP:
  29. # output the body's position and velocity
  30. x, y, z = body.getPosition()
  31. u, v, w = body.getLinearVel()
  32. print "%1.2fsec: pos=(%6.3f, %6.3f, %6.3f) vel=(%6.3f, %6.3f, %6.3f)" % \
  33. (total_time, x, y, z, u, v, w)
  34. # advance the simulation
  35. world.step(TIME_STEP)
  36. total_time += TIME_STEP