spacepaste

  1.  
  2. #!/usr/bin/env python
  3. import pygame
  4. import ode
  5. default_density = 2500
  6. default_radius = 0.5
  7. white = (255, 255, 255)
  8. blue = (0, 0, 255)
  9. class Pendulum(object):
  10. def __init__(self, ball, joint):
  11. self.ball = ball
  12. self.joint = joint
  13. class Block(object):
  14. def __init__(self, block, joint):
  15. self.block = block
  16. self.joint = joint
  17. def coord((x, y, z)):
  18. return int(100 + x*20), int(300 - y*20)
  19. def init_scene():
  20. # Position and density information
  21. pendulum_density = default_density
  22. base_density = default_density
  23. pendulum_balls = [(0, 8, 0), (3, 5, 0), ( 7, 4, 0), (12, 3, 0)]
  24. pendulum_joints = [(5, 8, 0), (7, 8, 0), (10, 8, 0), (12, 8, 0)]
  25. # Create world
  26. world = ode.World()
  27. world.setGravity((0, -9.81, 0))
  28. # Create base
  29. b_body = ode.Body(world)
  30. b_mass = ode.Mass()
  31. b_mass.setBox(base_density, 13, 2, 2)
  32. b_body.setMass(b_mass)
  33. b_body.setPosition((8.5, 8.5, 0))
  34. b_joint = ode.SliderJoint(world)
  35. b_joint.attach(b_body, ode.environment)
  36. base = Block(b_body, b_joint)
  37. # Create pendulums
  38. p_list = []
  39. for p_pos, j_pos in zip(pendulum_balls, pendulum_joints):
  40. p_body = ode.Body(world)
  41. p_mass = ode.Mass()
  42. p_mass.setSphere(default_density, default_radius)
  43. p_body.setMass(p_mass)
  44. p_body.setPosition(p_pos)
  45. # Create & connect joint
  46. joint = ode.BallJoint(world)
  47. joint.attach(p_body, b_body)
  48. joint.setAnchor(j_pos)
  49. print "connected {0} to {1}".format(p_pos, j_pos)
  50. # Instantiate container class
  51. p_list.append(Pendulum(p_body, joint))
  52. # return base, p_list
  53. return world, base, p_list
  54. def init_graphics():
  55. pygame.init()
  56. screen = pygame.display.set_mode((600, 600))
  57. return screen
  58. def main(screen, world, base, p_list):
  59. clock = pygame.time.Clock()
  60. fps = 60
  61. dt = 1.0/fps
  62. while not pygame.event.get(pygame.QUIT):
  63. pygame.event.get([pygame.KEYDOWN, pygame.MOUSEMOTION])
  64. world.step(dt)
  65. # render world
  66. screen.fill(white)
  67. for p in p_list:
  68. pygame.draw.circle(screen, blue, coord(p.ball.getPosition()), 10)
  69. pygame.draw.line(screen, blue, coord(p.joint.getAnchor()), coord(p.ball.getPosition()), 1)
  70. for p in p_list:
  71. pygame.draw.circle(screen, blue, coord(p.joint.getAnchor()), 10)
  72. pygame.draw.line(screen, blue, coord(p_list[0].joint.getAnchor()), coord(p_list[-1].joint.getAnchor()), 20)
  73. #for a, j in zip(base.anchors, base.joints):
  74. # pygame.draw.circle(screen, blue, coord(a.getPosition()), 10)
  75. # pygame.draw.line(screen, blue, coord(a.getPosition()), coord(j.getAnchor()), 1)
  76. pygame.display.flip()
  77. # tick pygame's clock
  78. clock.tick(fps)
  79. if __name__=="__main__":
  80. screen = init_graphics()
  81. world, base, p_list = init_scene()
  82. main(screen, world, base, p_list)
  83.