Advertisement
Guest User

Stu

a guest
Sep 29th, 2009
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.01 KB | None | 0 0
  1. """
  2. This is a port of http://code.google.com/p/pymunk/wiki/SlideAndPinJointsExample to
  3. NodeBox, instead of PyGame, as in the tutorial.
  4. """
  5.  
  6. print "-- START --"
  7.  
  8. import pymunk as physics
  9. import math
  10.  
  11. #global width, height
  12.  
  13. size(600, 600)
  14.  
  15. speed(50) # Bigger numbers go faster ;-)
  16.  
  17. def setup():
  18.     global world
  19.    
  20.     world = Stage(WIDTH, HEIGHT)
  21.     size(600, 600)
  22.    
  23. def draw():
  24.     global world
  25.    
  26.     world.update()
  27.     world.draw()
  28.  
  29.  
  30. class Ball(object):
  31.     def __init__(self):
  32.         self.mass = 1
  33.         self.radius = 14
  34.        
  35.         # All bodies must have their moment of inertia set. If our object is
  36.         # a normal ball we can use the predefined function moment_for_circle
  37.         # to calculate it given its mass and radius.
  38.         self.inertia = physics.moment_for_circle(self.mass, 0, self.radius, (0,0))
  39.        
  40.         # After we have the inertia we can create the body of the ball.
  41.         self.body = physics.Body(self.mass, self.inertia)
  42.        
  43.         self.x = random(600)
  44.         self.y = 600
  45.        
  46.         # set its position
  47.         self.body.position = self.x, self.y
  48.        
  49.         # In order for it to collide with things, it needs to have one (or many) collision shape(s)
  50.         self.shape = physics.Circle(self.body, self.radius, (0,0))
  51.  
  52.  
  53. class Stage(object):
  54.     def __init__(self, width, height):
  55.         self.width = width
  56.         self.height = height
  57.        
  58.         # The first thing we should do before anything else is to initialize pymunk
  59.         physics.init_pymunk()
  60.    
  61.         # We then create a space and set its gravity to something good.
  62.         self.space = physics.Space()
  63.         self.space.gravity = (0.0, -900.0)
  64.        
  65.         self.balls = []
  66.         self.lines = []
  67.        
  68.         self.add_lines()
  69.  
  70.     def add_lines(self):
  71.         # We create a "static" body with infinite mass and inertia. The important step is to never
  72.         # add it to the space. pm.inf is actually just a variable (1e100) so big it is converted
  73.         # to infinity, and you can use 1e100 or another big number if it makes you feel better :)
  74.         rotation_center_body = physics.Body(physics.inf, physics.inf)
  75.         rotation_center_body.position = (300, 300)
  76.        
  77.         #rotation_limit_body = physics.Body(physics.inf, physics.inf)
  78.         #rotation_limit_body.position = (200, 200)
  79.        
  80.         body = physics.Body(10, 10000)
  81.         body.position = (300, 300)
  82.        
  83.         # A line shaped shape is created here.
  84.         l1 = physics.Segment(body, (-150.0, 0), (255.0, 0.0), 5.0)
  85.         l2 = physics.Segment(body, (-150.0, 0), (-150.0, 50.0), 5.0)
  86.        
  87.         # A pin joint allow two objects to pivot about a single point. In our case one of the
  88.         # objects will be stuck to the world.
  89.         rotation_center_joint = physics.PinJoint(body, rotation_center_body, (0, 0), (0, 0))
  90.  
  91.         # Create a slide joint. It behaves like pin joints but have a minimum and maximum distance.
  92.         #joint_limit = 25
  93.         #rotation_limit_joint = physics.SlideJoint(body, rotation_limit_body, (-100, 0), (0, 0), 0, joint_limit)
  94.        
  95.         # Remember to not add the body to the shape as we want it to be static
  96.         self.space.add(l1, l2, body, rotation_center_joint) #, rotation_limit_joint)
  97.  
  98.         self.lines.append(l1)
  99.         self.lines.append(l2)
  100.  
  101.     def add_ball(self):
  102.         print "Adding a Ball!"
  103.         b = Ball()
  104.        
  105.         # Add the body and shape to the space to include it in our simulation
  106.         self.space.add(b.body, b.shape)
  107.  
  108.         self.balls.append(b)
  109.  
  110.     def update(self):
  111.         # In our main loop we call the step() function on our space.
  112.         # It will do a physics step. Note: It is best to keep the stepsize
  113.         # constant and not adjust it depending on the framrate. The physic
  114.         # simulation will work much better with a constant step size.
  115.         self.space.step(1/50.0)
  116.        
  117.         if (random(10) > 8):
  118.             self.add_ball()
  119.    
  120.     def draw(self):
  121.         stroke(0.1)
  122.        
  123.         # Hard-code draw the pivot
  124.         oval(295, self.height-305, 10, 10)
  125.        
  126.         # Hard-code draw the limit thing
  127.         #oval(200, self.height-375, 75, 75)
  128.        
  129.         for l in self.lines:
  130.             e1 = l.body.position + l.a.rotated(math.degrees(l.body.angle))
  131.             e2 = l.body.position + l.b.rotated(math.degrees(l.body.angle))
  132.  
  133.             line(e1.x, self.height - e1.y, e2.x, self.height - e2.y)
  134.        
  135.         fill(0.6, 0.0, 0.0)
  136.  
  137.         dead_balls = []
  138.        
  139.         for ball in self.balls:
  140.             if (ball.body.position.y < 100):
  141.                 dead_balls.append(ball)
  142.                 continue
  143.  
  144.             #print "Draw at", ball.body.position.x, ",", ball.body.position.y
  145.             oval(ball.body.position.x, self.height - (ball.body.position.y + 20), 20, 20)
  146.  
  147.         for ball in dead_balls:
  148.             self.space.remove(ball.shape, ball.body)
  149.             self.balls.remove(ball)
  150.  
  151.  
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement