示例#1
0
    def joint(self, *args):
        print "* Add Joint:", args

        if len(args) == 5:
            # Tracking Joint
            b1, b2, p1, p2, flag = args

            p1 = self.to_b2vec(p1)
            p2 = self.to_b2vec(p2)

            jointDef = box2d.b2DistanceJointDef()
            jointDef.Initialize(b1, b2, p1, p2)
            jointDef.collideConnected = flag

            self.parent.world.CreateJoint(jointDef)

        elif len(args) == 4:
            # Distance Joint
            b1, b2, p1, p2 = args

            p1 = self.to_b2vec(p1)
            p2 = self.to_b2vec(p2)

            jointDef = box2d.b2DistanceJointDef()
            jointDef.Initialize(b1, b2, p1, p2)
            jointDef.collideConnected = True

            self.parent.world.CreateJoint(jointDef)

        elif len(args) == 3:
            # Revolute Joint between two bodies (unimplemented)
            pass

        elif len(args) == 2:
            # Revolute Joint to the Background, at point
            b1 = self.parent.world.groundBody
            b2 = args[0]
            p1 = self.to_b2vec(args[1])

            jointDef = box2d.b2RevoluteJointDef()
            jointDef.Initialize(b1, b2, p1)
            self.parent.world.CreateJoint(jointDef)

        elif len(args) == 1:
            # Revolute Joint to the Background, body center
            b1 = self.parent.world.groundBody
            b2 = args[0]
            p1 = b2.GetWorldCenter()

            jointDef = box2d.b2RevoluteJointDef()
            jointDef.Initialize(b1, b2, p1)

            self.parent.world.CreateJoint(jointDef)
示例#2
0
    def joint(self, *args):
        print "* Add Joint:", args

        if len(args) == 5:
            # Tracking Joint
            b1, b2, p1, p2, flag = args

            p1 = self.to_b2vec(p1)
            p2 = self.to_b2vec(p2)

            jointDef = box2d.b2DistanceJointDef()
            jointDef.Initialize(b1, b2, p1, p2)
            jointDef.collideConnected = flag

            self.parent.world.CreateJoint(jointDef)

        elif len(args) == 4:
            # Distance Joint
            b1, b2, p1, p2 = args

            p1 = self.to_b2vec(p1)
            p2 = self.to_b2vec(p2)

            jointDef = box2d.b2DistanceJointDef()
            jointDef.Initialize(b1, b2, p1, p2)
            jointDef.collideConnected = True

            self.parent.world.CreateJoint(jointDef)

        elif len(args) == 3:
            # Revolute Joint between two bodies (unimplemented)
            pass

        elif len(args) == 2:
            # Revolute Joint to the Background, at point
            b1 = self.parent.world.groundBody
            b2 = args[0]
            p1 = self.to_b2vec(args[1])

            jointDef = box2d.b2RevoluteJointDef()
            jointDef.Initialize(b1, b2, p1)
            self.parent.world.CreateJoint(jointDef)

        elif len(args) == 1:
            # Revolute Joint to the Background, body center
            b1 = self.parent.world.groundBody
            b2 = args[0]
            p1 = b2.GetWorldCenter()

            jointDef = box2d.b2RevoluteJointDef()
            jointDef.Initialize(b1, b2, p1)

            self.parent.world.CreateJoint(jointDef)
示例#3
0
文件: tools.py 项目: hspadim/bridge
    def handleEvents(self,event):
        #look for default events, and if none are handled then try the custom events 
        if super(BridgeJointTool,self).handleEvents(event):
            return
        if event.type != MOUSEBUTTONUP or event.button != 1:
            return

        bodies = self.game.world.get_bodies_at_pos(event.pos,
            include_static=True)
        if not bodies or len(bodies) > 2:
            return
            
        jointDef = box2d.b2RevoluteJointDef()
        if len(bodies) == 1:
            if not bodies[0].IsStatic():
                if event.pos[1] > 550 and (event.pos[0] < 350 or event.pos[0] > 850):
                    jointDef.Initialize(self.game.world.world.GetGroundBody(), 
                        bodies[0], self.to_b2vec(event.pos))
                else:
                    return
            else:
                return                   
        elif len(bodies) == 2: 
            if bodies[0].IsStatic():
                jointDef.Initialize(self.game.world.world.GetGroundBody(), 
                    bodies[1], self.to_b2vec(event.pos))
            elif bodies[1].IsStatic():
                jointDef.Initialize(self.game.world.world.GetGroundBody(), 
                    bodies[0], self.to_b2vec(event.pos))
            else:
                jointDef.Initialize(bodies[0], bodies[1], self.to_b2vec(event.pos))
        joint = self.game.world.world.CreateJoint(jointDef)
        self.game.bridge.joint_added(joint)
示例#4
0
    def handleEvents(self, event):
        #look for default events, and if none are handled then try the custom events
        if super(BridgeJointTool, self).handleEvents(event):
            return
        if event.type != MOUSEBUTTONUP or event.button != 1:
            return

        bodies = self.game.world.get_bodies_at_pos(event.pos,
                                                   include_static=True)
        if not bodies or len(bodies) > 2:
            return

        jointDef = box2d.b2RevoluteJointDef()
        if len(bodies) == 1:
            if not bodies[0].IsStatic():
                if event.pos[1] > 550 and (event.pos[0] < 350
                                           or event.pos[0] > 850):
                    jointDef.Initialize(self.game.world.world.GetGroundBody(),
                                        bodies[0], self.to_b2vec(event.pos))
                else:
                    return
            else:
                return
        elif len(bodies) == 2:
            if bodies[0].IsStatic():
                jointDef.Initialize(self.game.world.world.GetGroundBody(),
                                    bodies[1], self.to_b2vec(event.pos))
            elif bodies[1].IsStatic():
                jointDef.Initialize(self.game.world.world.GetGroundBody(),
                                    bodies[0], self.to_b2vec(event.pos))
            else:
                jointDef.Initialize(bodies[0], bodies[1],
                                    self.to_b2vec(event.pos))
        joint = self.game.world.world.CreateJoint(jointDef)
        self.game.bridge.joint_added(joint)
示例#5
0
 def jointMotor(self, b1, b2, p1, torque=900, speed=-10):
     p1 = self.to_b2vec(p1)
     jointDef = box2d.b2RevoluteJointDef()
     jointDef.Initialize(b1, b2, p1)
     jointDef.maxMotorTorque = torque
     jointDef.motorSpeed = speed
     jointDef.enableMotor = True
     self.parent.world.CreateJoint(jointDef)
示例#6
0
 def jointMotor(self,b1,b2,p1,torque=900,speed=-10):
     p1 = self.to_b2vec(p1)
     jointDef = box2d.b2RevoluteJointDef()
     jointDef.Initialize(b1, b2, p1)
     jointDef.maxMotorTorque = torque
     jointDef.motorSpeed = speed
     jointDef.enableMotor = True
     self.parent.world.CreateJoint(jointDef)
示例#7
0
 def revoluteJoint(self,b1,b2,p1):
     # revolute joint between to bodies
     p1 = self.to_b2vec(p1)                        
         
     jointDef = box2d.b2RevoluteJointDef()
     jointDef.Initialize(b1, b2, p1)
     
     self.parent.world.CreateJoint(jointDef)
示例#8
0
    def revoluteJoint(self, b1, b2, p1):
        # revolute joint between to bodies
        p1 = self.to_b2vec(p1)

        jointDef = box2d.b2RevoluteJointDef()
        jointDef.Initialize(b1, b2, p1)

        self.parent.world.CreateJoint(jointDef)
示例#9
0
 def motor(self, body,pt,torque=900,speed=-10):           
     # Fixed Joint to the Background with a motor on it
     b1 = self.parent.world.GetGroundBody()
     pt = self.to_b2vec(pt)                        
         
     jointDef = box2d.b2RevoluteJointDef()
     jointDef.Initialize(b1, body, pt)
     jointDef.maxMotorTorque = torque
     jointDef.motorSpeed = speed
     jointDef.enableMotor = True
     self.parent.world.CreateJoint(jointDef)
示例#10
0
 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.to_b2vec(args[1])                        
          
         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)  
示例#11
0
    def motor(self, body, pt, torque=900, speed=-10):
        # Fixed Joint to the Background with a motor on it
        b1 = self.parent.world.GetGroundBody()
        pt = self.to_b2vec(pt)

        jointDef = box2d.b2RevoluteJointDef()
        jointDef.Initialize(b1, body, pt)
        jointDef.maxMotorTorque = torque
        jointDef.motorSpeed = speed
        jointDef.enableMotor = True
        self.parent.world.CreateJoint(jointDef)
示例#12
0
    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.to_b2vec(args[1])

            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)
示例#13
0
    def motor(self, body, pt, torque=900, speed=-10):
        # Revolute joint to the background with motor torque applied
        b1 = self.parent.world.groundBody
        pt = self.to_b2vec(pt)

        jointDef = box2d.b2RevoluteJointDef()
        jointDef.Initialize(b1, body, pt)
        jointDef.maxMotorTorque = torque
        jointDef.motorSpeed = speed
        jointDef.enableMotor = True

        self.parent.world.CreateJoint(jointDef)
示例#14
0
    def motor(self, body, pt, torque=900, speed=-10):
        # Revolute joint to the background with motor torque applied
        b1 = self.parent.world.groundBody
        pt = self.to_b2vec(pt)

        jointDef = box2d.b2RevoluteJointDef()
        jointDef.Initialize(b1, body, pt)
        jointDef.maxMotorTorque = torque
        jointDef.motorSpeed = speed
        jointDef.enableMotor = True

        self.parent.world.CreateJoint(jointDef)
示例#15
0
    def joint(self, *args):        
        print "* Add Joint:", args

        if len(args) == 4:
            # Distance Joint
            b1, b2, p1, p2 = args

            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)           
             
        elif len(args) == 3:
            # Revolute Joint
            pass

        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)
示例#16
0
    def joint(self, *args):
        print "* Add Joint:", args

        if len(args) == 4:
            # Distance Joint
            b1, b2, p1, p2 = args

            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)

        elif len(args) == 3:
            # Revolute Joint
            pass

        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)