Exemple #1
0
    def start(self):
        for component in self.node.data:
            if isinstance(component, Rigidbody):
                # Check if parent has rigidbody, and use that node if it does
                for p_component in self.node.parent.data:
                    if isinstance(p_component, Rigidbody):
                        self.parent_path = p_component.body_path
                        self.update_parent = False

                if self.parent_path is None:
                    parent_node = BulletRigidBodyNode(self.node.parent.name + "_node")
                    parent_node.set_mass(0)
                    self.parent_path = EComponent.panda_root_node.attach_new_node(parent_node)

                self.parent_path.setPos(helper.np_vec3_to_panda(self.node.parent.transform.get_world_translation()))
                rot = np.degrees(self.node.parent.transform.get_world_rotation())
                self.parent_path.setHpr(LVector3f(rot[1],
                                                  rot[0],
                                                  rot[2]))
                self.parent_path.setScale(helper.np_vec3_to_panda(self.node.parent.transform.get_world_scale()))

                # Create constraint
                child_transform = TransformState.make_pos(LVector3f(0, 0, 0))
                node_pos = self.node.transform.get_translation() * self.node.transform.get_world_scale()
                parent_transform = TransformState.make_pos(helper.np_vec3_to_panda(node_pos))
                constraint = BulletConeTwistConstraint(component.body_path.node(),
                                                       self.parent_path.node(),
                                                       child_transform,
                                                       parent_transform)
                constraint.set_limit(float(self.property_vals["swing_1"]),
                                     float(self.property_vals["swing_2"]),
                                     float(self.property_vals["max_twist"]))
                EComponent.physics_world.attachConstraint(constraint)
    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, 4)

        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(0, 0, 0)

        visNP = loader.load_model('models/box.egg')
        visNP.clear_model_nodes()
        visNP.reparent_to(bodyNP)

        self.world.attach(bodyB)

        # Cone
        frameA = TransformState.make_pos_hpr(LPoint3(0, 0, -2),
                                             LVector3(0, 0, 90))
        frameB = TransformState.make_pos_hpr(LPoint3(-5, 0, 0),
                                             LVector3(0, 0, 0))

        cone = BulletConeTwistConstraint(bodyA, bodyB, frameA, frameB)
        cone.set_debug_draw_size(2.0)
        cone.set_limit(30, 45, 170, softness=1.0, bias=0.3, relaxation=8.0)
        self.world.attach(cone)