From 191893b5f035acc0f9d5dfb20c2819065a75c42c Mon Sep 17 00:00:00 2001 From: Alex Levenson Date: Thu, 10 Jul 2008 19:01:23 +0000 Subject: Adding more types of joints... --- diff --git a/elements/add_objects.py b/elements/add_objects.py index 72dc21e..6ce7773 100644 --- a/elements/add_objects.py +++ b/elements/add_objects.py @@ -445,8 +445,52 @@ class Add: # Define the body x, y = c = tools_poly.calc_center(vertices_orig_reduced) return self.poly((x,y), vertices, dynamic, density, restitution, friction) - - def joint(self, *args): + + # Alex Levenson's added joint methods: + def distanceJoint(self,b1,b2,p1,p2): + # Distance Joint + p1 = self.parent.to_world(p1) + p2 = self.parent.to_world(p2) + p1x, p1y = p1 + p2x, p2y = p2 + p1x /= self.parent.ppm + p1y /= self.parent.ppm + p2x /= self.parent.ppm + p2y /= self.parent.ppm + p1 = box2d.b2Vec2(p1x, p1y) + p2 = box2d.b2Vec2(p2x, p2y) + jointDef = box2d.b2DistanceJointDef() + jointDef.Initialize(b1, b2, p1, p2) + jointDef.collideConnected = True + self.parent.world.CreateJoint(jointDef) + + def fixedJoint(self, *args): + if len(args) == 2: + # Fixed Joint to the Background, don't assume the center of the body + b1 = self.parent.world.GetGroundBody() + b2 = args[0] + p1 = self.parent.to_world(args[1]) + p1x, p1y = p1 + p1x /= self.parent.ppm + p1y /= self.parent.ppm + p1 = box2d.b2Vec2(p1x, p1y) + + jointDef = box2d.b2RevoluteJointDef() + jointDef.Initialize(b1, b2, p1) + + self.parent.world.CreateJoint(jointDef) + elif len(args) == 1: + # Fixed Joint to the Background, assume the center of the body + b1 = self.parent.world.GetGroundBody() + b2 = args[0] + p1 = b2.GetWorldCenter() + + jointDef = box2d.b2RevoluteJointDef() + jointDef.Initialize(b1, b2, p1) + + self.parent.world.CreateJoint(jointDef) + + def joint(self, *args): print "* Add Joint:", args if len(args) == 4: @@ -476,22 +520,7 @@ class Add: elif len(args) == 3: # Revolute Joint pass - - elif len(args) == 2: - - # Revolute Joint to the Background, don't assume the center of the body - b1 = self.parent.world.GetGroundBody() - b2 = args[0] - p1 = self.parent.to_world(args[1]) - p1x, p1y = p1 - p1x /= self.parent.ppm - p1y /= self.parent.ppm - p1 = box2d.b2Vec2(p1x, p1y) - - jointDef = box2d.b2RevoluteJointDef() - jointDef.Initialize(b1, b2, p1) - - self.parent.world.CreateJoint(jointDef) + elif len(args) == 1: # Revolute Joint to the Background, assume the center of the body b1 = self.parent.world.GetGroundBody() diff --git a/tools.py b/tools.py index be0b3b7..0c13581 100644 --- a/tools.py +++ b/tools.py @@ -267,17 +267,19 @@ class JointTool(Tool): self.jb2 = self.game.world.get_bodies_at_pos(event.pos) # if we have two distinct bodies, add a joint! if self.jb1 and self.jb2 and str(self.jb1) != str(self.jb2): - self.game.world.add.joint(self.jb1[0],self.jb2[0],self.jb1pos,self.jb2pos) + self.game.world.add.distanceJoint(self.jb1[0],self.jb2[0],self.jb1pos,self.jb2pos) # If there's only one body, add a fixed joint elif self.jb2: - self.game.world.add.joint(self.jb2[0],self.jb2pos) + self.game.world.add.fixedJoint(self.jb2[0],self.jb2pos) # regardless, clean everything up self.jb1 = self.jb2 = self.jb1pos = self.jb2pos = None if event.button == 3: - # add a centered fisxed joint + # add a centered fixed joint self.jb2 = self.game.world.get_bodies_at_pos(event.pos) if self.jb2: - self.game.world.add.joint(self.jb2[0]) + self.game.world.add.fixedJoint(self.jb2[0]) + # regardless, clean everything up + self.jb1 = self.jb2 = self.jb1pos = self.jb2pos = None def draw(self): if self.jb1: pygame.draw.line(self.game.screen,(100,180,255),self.jb1pos,pygame.mouse.get_pos(),3) -- cgit v0.9.1