def MouseDown(self, p): """ Indicates that there was a left click at point p (world coordinates) """ if self.mouseJoint != None: return # Make a small box. aabb = box2d.b2AABB() d = box2d.b2Vec2(0.001, 0.001) aabb.lowerBound = p - d aabb.upperBound = p + d # Query the world for overlapping shapes. body = None k_maxCount = 10 (count, shapes) = self.world.Query(aabb, k_maxCount) for shape in shapes: shapeBody = shape.GetBody() if shapeBody.IsStatic() == False and shapeBody.GetMass() > 0.0: if shape.TestPoint(shapeBody.GetXForm(), p): # is it inside? body = shapeBody break if body: md = box2d.b2MouseJointDef() md.body1 = self.world.GetGroundBody() md.body2 = body md.target = p md.maxForce= 1000.0 * body.GetMass() self.mouseJoint = self.world.CreateJoint(md) body.WakeUp()
def create_mouse_joint(self, x, y): """ create a mouse joint to the body on world coordinates x, y """ if self.mouse_joint: return world_coord = box2d.b2Vec2(self.debugDraw.screen_to_world((x, y))) aabb = box2d.b2AABB() aabb.lowerBound = world_coord - (0.001, 0.001) aabb.upperBound = world_coord + (0.001, 0.001) max_count = 10 count, shapes = self.world.Query(aabb, max_count) body = (body for shape, body in ((shape, shape.GetBody()) for shape in shapes) if not body.IsStatic() and body.GetMass() > 0.0 and shape.TestPoint(body.GetXForm(), world_coord)) body = list(itertools.islice(body, 1)) body = body[0] if len(body) == 1 else None if body: md = box2d.b2MouseJointDef() md.body1 = self.world.GetGroundBody() md.body2 = body md.target = world_coord md.maxForce = 1000 * body.GetMass() self.mouse_joint = self.world.CreateJoint(md) body.WakeUp()
def createJoint(self,ball,pos): mouseDef=Box2D.b2MouseJointDef() pos=self.s2wPos(pos) mouseDef.target=Box2D.b2Vec2(pos[0], pos[1]) mouseDef.maxForce=20000*ball.body.GetMass() mouseDef.body1=self.world.groundBody mouseDef.body2=ball.body mouseDef.collideConnected=True return self.world.CreateJoint(mouseDef).asMouseJoint()
def mouseJoint(self, body, pos, jointForce=100.0): pos = self.parent.to_world(pos) x, y = pos x /= self.parent.ppm y /= self.parent.ppm mj = box2d.b2MouseJointDef() mj.bodyA = self.parent.world.groundBody mj.bodyB = body mj.target = (x, y) mj.maxForce = jointForce * body.mass if 'getAsType' in dir(box2d.b2Joint): self.parent.mouseJoint = \ self.parent.world.CreateJoint(mj).getAsType() else: self.parent.mouseJoint = self.parent.world.CreateJoint(mj) body.awake = True
def MouseDown(self, p): """ Indicates that there was a left click at point p (world coordinates) """ if self.mouseJoint != None: return # Create a mouse joint on the selected body (assuming it's dynamic) # Make a small box. aabb = box2d.b2AABB() aabb.lowerBound = p - (0.001, 0.001) aabb.upperBound = p + (0.001, 0.001) # Query the world for overlapping shapes. body = None k_maxCount = 10 # maximum amount of shapes to return (count, shapes) = self.world.Query(aabb, k_maxCount) for shape in shapes: shapeBody = shape.GetBody() if not shapeBody.IsStatic() and shapeBody.GetMass() > 0.0: if shape.TestPoint(shapeBody.GetXForm(), p): # is it inside? body = shapeBody break if body: # A body was selected, create the mouse joint md = box2d.b2MouseJointDef() md.body1 = self.world.GetGroundBody() md.body2 = body md.target = p md.maxForce= 1000.0 * body.GetMass() self.mouseJoint = self.world.CreateJoint(md) body.WakeUp()
def MouseDown(self, p): """ Indicates that there was a left click at point p (world coordinates) """ if self.mouseJoint != None: return # Create a mouse joint on the selected body (assuming it's dynamic) # Make a small box. aabb = box2d.b2AABB() aabb.lowerBound = p - (0.001, 0.001) aabb.upperBound = p + (0.001, 0.001) # Query the world for overlapping shapes. body = None k_maxCount = 10 # maximum amount of shapes to return (count, shapes) = self.world.Query(aabb, k_maxCount) for shape in shapes: shapeBody = shape.GetBody() if not shapeBody.IsStatic() and shapeBody.GetMass() > 0.0: if shape.TestPoint(shapeBody.GetXForm(), p): # is it inside? body = shapeBody break if body: # A body was selected, create the mouse joint md = box2d.b2MouseJointDef() md.body1 = self.world.GetGroundBody() md.body2 = body md.target = p md.maxForce = 1000.0 * body.GetMass() self.mouseJoint = self.world.CreateJoint(md).getAsType() body.WakeUp()
def draw_blocks(self): for block in self.blockbodies.itervalues(): block.draw() blocks = Blocks() for l in range(-1,3): for i in range(-4,3): blocks.add_block(Block((i*8,l*8),2,2, 800, 'circle')) md = box2d.b2MouseJointDef() md.body1 = body # world.GetGroundBody() md.body2 = body md.target = body.GetWorldCenter() # toWorld(pygame.mouse.get_pos()) md.maxForce= 100000.0 mouseJoint = world.CreateJoint(md).getAsType() body.WakeUp() delete = [] class fwContactListener(box2d.b2ContactListener): """ Handles all of the contact states passed in from Box2D. """ test = None def __init__(self):