def on_mouse_button(self, button, down): if self.hover > 0 and down: self.wm.focus(self.hover) if button == 1 and self.hover_caption and down: if self.dragged_window: self.stop_drag(True) globals.gui.mouse_mode(False) else: self.dragged_window = self.hover_caption self.dragged_window.set_physics(True) offset = LVector3(0, 0, 50) self.hand.node.setPos(self.hover_caption_pos + offset) self.hand_constraint = BulletHingeConstraint( self.hand.phy_node, self.dragged_window.phy_node, Point3(0, 0, 0), self.dragged_window.box.node.getPos() + offset, LVector3(1, 0, 0), LVector3(1, 0, 0), False) # self.hand_constraint = BulletSphericalConstraint(self.hand.phy_node, self.dragged_window.phy_node, # Point3(0, 0, 0), self.dragged_window.box.node.getPos()) globals.gui.world.attachConstraint(self.hand_constraint) # self.dragged_window.set_physics(False) self.hold_mouse = self.mouse_pos[0], self.mouse_pos[1] globals.gui.mouse_mode(True) return if button == 3 and (self.dragged_window or self.rotate_window): self.reset_mouse = True self.rotate_window = self.dragged_window if down else None if self.dragged_window: self.dragged_window.set_physics(False) globals.gui.mouse_mode(down) self.stop_drag(False) return self.handle_mouse_button_event(button, down)
def setup(self): self.worldNP = render.attach_new_node('World') # World self.debugNP = self.worldNP.attach_new_node(BulletDebugNode('Debug')) self.debugNP.show() self.debugNP.node().show_wireframe(True) self.debugNP.node().show_constraints(True) self.debugNP.node().show_bounding_boxes(False) self.debugNP.node().show_normals(False) self.world = BulletWorld() self.world.set_gravity(LVector3(0, 0, -9.81)) self.world.set_debug_node(self.debugNP.node()) # Box A shape = BulletBoxShape(LVector3(0.5, 0.5, 0.5)) bodyA = BulletRigidBodyNode('Box A') bodyNP = self.worldNP.attach_new_node(bodyA) bodyNP.node().add_shape(shape) bodyNP.set_collide_mask(BitMask32.all_on()) bodyNP.set_pos(-2, 0, 1) visNP = loader.load_model('models/box.egg') visNP.clear_model_nodes() visNP.reparent_to(bodyNP) self.world.attach(bodyA) # Box B shape = BulletBoxShape(LVector3(0.5, 0.5, 0.5)) bodyB = BulletRigidBodyNode('Box B') bodyNP = self.worldNP.attach_new_node(bodyB) bodyNP.node().add_shape(shape) bodyNP.node().set_mass(1.0) bodyNP.node().set_deactivation_enabled(False) bodyNP.set_collide_mask(BitMask32.all_on()) bodyNP.set_pos(2, 0, 1) visNP = loader.load_model('models/box.egg') visNP.clear_model_nodes() visNP.reparent_to(bodyNP) self.world.attach(bodyB) # Hinge pivotA = LPoint3(2, 0, 0) pivotB = LPoint3(-4, 0, 0) axisA = LVector3(0, 0, 1) axisB = LVector3(0, 0, 1) hinge = BulletHingeConstraint(bodyA, bodyB, pivotA, pivotB, axisA, axisB, True) hinge.set_debug_draw_size(2.0) hinge.set_limit(-90, 120, softness=0.9, bias=0.3, relaxation=1.0) self.world.attach(hinge)
def setup(self): self.worldNP = render.attachNewNode('World') # World self.debugNP = self.worldNP.attachNewNode(BulletDebugNode('Debug')) self.debugNP.show() self.debugNP.node().showWireframe(True) self.debugNP.node().showConstraints(True) self.debugNP.node().showBoundingBoxes(False) self.debugNP.node().showNormals(False) self.world = BulletWorld() self.world.setGravity(Vec3(0, 0, -9.81)) self.world.setDebugNode(self.debugNP.node()) # Box A shape = BulletBoxShape(Vec3(0.5, 0.5, 0.5)) bodyA = BulletRigidBodyNode('Box A') bodyNP = self.worldNP.attachNewNode(bodyA) bodyNP.node().addShape(shape) bodyNP.setCollideMask(BitMask32.allOn()) bodyNP.setPos(-2, 0, 1) visNP = loader.loadModel('models/box.egg') visNP.clearModelNodes() visNP.reparentTo(bodyNP) self.world.attachRigidBody(bodyA) # Box B shape = BulletBoxShape(Vec3(0.5, 0.5, 0.5)) bodyB = BulletRigidBodyNode('Box B') bodyNP = self.worldNP.attachNewNode(bodyB) bodyNP.node().addShape(shape) bodyNP.node().setMass(1.0) bodyNP.node().setDeactivationEnabled(False) bodyNP.setCollideMask(BitMask32.allOn()) bodyNP.setPos(2, 0, 1) visNP = loader.loadModel('models/box.egg') visNP.clearModelNodes() visNP.reparentTo(bodyNP) self.world.attachRigidBody(bodyB) # Hinge pivotA = Point3(2, 0, 0) pivotB = Point3(-4, 0, 0) axisA = Vec3(0, 0, 1) axisB = Vec3(0, 0, 1) hinge = BulletHingeConstraint(bodyA, bodyB, pivotA, pivotB, axisA, axisB, True) hinge.setDebugDrawSize(2.0) hinge.setLimit(-90, 120, softness=0.9, bias=0.3, relaxation=1.0) self.world.attachConstraint(hinge)
def setupDoor(self, _obj, _eggFile): shape = BulletBoxShape(Vec3(1.7 / 2, 0.6 / 2, 0.2 / 2)) node = BulletRigidBodyNode(_obj.getTag("door")) node.addShape(shape) node.setMass(1) node.setDeactivationEnabled(False) np = self.rootNode.attachNewNode(node) np.setCollideMask(BitMask32.bit(2)) np.setPos(_obj.getPos()) np.setHpr(_obj.getHpr()) self.parent.physics_world.attachRigidBody(node) _obj.reparentTo(np) _obj.setPos(np.getPos() - _obj.getPos()) # Setup hinge if _obj.getTag("door") == "left" and self.isHingeLeftSet != True: pos = Point3(-2.0, 0, 0) #hingeLeft.getPos() axisA = Vec3(0, 1, 0) hinge = BulletHingeConstraint(node, pos, axisA, True) hinge.setDebugDrawSize(0.3) hinge.setLimit(-10, 58, softness=0.9, bias=0.3, relaxation=1.0) self.parent.physics_world.attachConstraint(hinge) self.isHingeLeftSet = True self.parent.game_doors["left"] = np self.parent.game_doors["left_hinge"] = hinge if _obj.getTag("door") == "right" and self.isHingeRightSet != True: pos = Point3(2.0, 0, 0) #hingeLeft.getPos() axisA = Vec3(0, -1, 0) hinge = BulletHingeConstraint(node, pos, axisA, True) hinge.setDebugDrawSize(0.3) hinge.setLimit(-10, 58, softness=0.9, bias=0.3, relaxation=1.0) self.parent.physics_world.attachConstraint(hinge) self.isHingeRightSet = True self.parent.game_doors["right"] = np self.parent.game_doors["right_hinge"] = hinge
def __init__(self, world, render, position, direction, side, torso, limits): radius = 0.15 bicep_length = 0.75 forearm_length = 0.75 in_limit, out_limit, forward_limit, backward_limit, twist_limit = limits torso_pos = torso.getPos() x, y, z = position bicep_y = y + direction * bicep_length / 2 forearm_y = y + direction * bicep_length + forearm_length / 2 bicep_node = BulletRigidBodyNode('Bicep') bicep_node.addShape(BulletCapsuleShape(radius, bicep_length, 1)) bicep_node.setMass(0.25) bicep_pointer = render.attachNewNode(bicep_node) bicep_pointer.setPos(x, bicep_y, z) world.attachRigidBody(bicep_node) forearm_node = BulletRigidBodyNode('Forearm') forearm_node.addShape(BulletCapsuleShape(radius, forearm_length, 1)) forearm_node.setMass(0.25) forearm_pointer = render.attachNewNode(forearm_node) forearm_pointer.setPos(x, forearm_y, z) world.attachRigidBody(forearm_node) rotation = LMatrix3((-direction, 0, 0), (0, 0, -side), (0, -side * direction, 0)) bicep_to_shoulder = Vec3(0, -direction * bicep_length / 2, 0) torso_to_shoulder = Vec3(0, y + torso_pos[1], z - torso_pos[2]) bicep_shoulder_frame = make_rigid_transform(rotation, bicep_to_shoulder) torso_shoulder_frame = make_rigid_transform(rotation, torso_to_shoulder) shoulder = BulletGenericConstraint(torso.node(), bicep_node, torso_shoulder_frame, bicep_shoulder_frame, True) shoulder.setDebugDrawSize(0.3) world.attachConstraint(shoulder, True) elbow_axis = Vec3(0, 0, side) forearm_to_elbow = Point3(0, -direction * forearm_length / 2, 0) bicep_to_elbow = Point3(0, direction * bicep_length / 2, 0) elbow = BulletHingeConstraint(bicep_node, forearm_node, bicep_to_elbow, forearm_to_elbow, elbow_axis, elbow_axis, True) elbow.setDebugDrawSize(0.3) world.attachConstraint(elbow, True) for axis in range(3): shoulder.getRotationalLimitMotor(axis).setMaxMotorForce(200) elbow.setMaxMotorImpulse(200) shoulder.setAngularLimit(0, -in_limit, out_limit) shoulder.setAngularLimit(1, -twist_limit, twist_limit) shoulder.setAngularLimit(2, -backward_limit, forward_limit) elbow.setLimit(0, 180) self.render = render self.position = position self.bicep = bicep_pointer self.forearm = forearm_pointer self.shoulder = shoulder self.elbow = elbow self.transform = LMatrix3(-side * direction, 0, 0, 0, -direction, 0, 0, 0, 1) self.side = side self.lines = LineNodePath(name='debug', parent=self.render, colorVec=VBase4(0.2, 0.2, 0.5, 1)) axes = LineNodePath(name='axes', parent=self.render) paths = [ dict(color=VBase4(1, 0, 0, 1)), dict(color=VBase4(0, 1, 0, 1)), dict(color=VBase4(0, 0, 1, 1)) ] for i in range(3): axis = shoulder.getAxis(i) * 0.25 paths[i]['points'] = [axis] draw_lines(axes, *paths, origin=position)
def construct(self, node1, node2, point1, point2): self.constr = BulletHingeConstraint(node1, node2, point1, point2)