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 node_pos = self.node.transform.get_translation( ) * self.node.transform.get_world_scale() constraint = BulletSphericalConstraint( component.body_path.node(), self.parent_path.node(), LVector3f(0, 0, 0), LVector3f(node_pos[0], node_pos[1], node_pos[2])) EComponent.physics_world.attachConstraint(constraint)
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 handle_update(self): # Update parent node if self.update_parent: 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()))
def on_gui_update(self): gizmo_pos = self.node.transform.get_world_translation() if self.x_arrow_gizmo is not None: self.x_arrow_gizmo.get_geom().set_shader_input( "gizmo_pos", helper.np_vec3_to_panda(gizmo_pos)) self.y_arrow_gizmo.get_geom().set_shader_input( "gizmo_pos", helper.np_vec3_to_panda(gizmo_pos)) self.z_arrow_gizmo.get_geom().set_shader_input( "gizmo_pos", helper.np_vec3_to_panda(gizmo_pos)) if self.x_ring_gizmo is not None: self.x_ring_gizmo.get_geom().set_shader_input( "gizmo_pos", helper.np_vec3_to_panda(gizmo_pos)) self.y_ring_gizmo.get_geom().set_shader_input( "gizmo_pos", helper.np_vec3_to_panda(gizmo_pos)) self.z_ring_gizmo.get_geom().set_shader_input( "gizmo_pos", helper.np_vec3_to_panda(gizmo_pos)) if self.x_handle_gizmo is not None: self.x_handle_gizmo.get_geom().set_shader_input( "gizmo_pos", helper.np_vec3_to_panda(gizmo_pos)) self.y_handle_gizmo.get_geom().set_shader_input( "gizmo_pos", helper.np_vec3_to_panda(gizmo_pos)) self.z_handle_gizmo.get_geom().set_shader_input( "gizmo_pos", helper.np_vec3_to_panda(gizmo_pos))
def raycast(self, ray_origin, ray_dir): # Perform raycast result = self.physics_world.rayTestClosest( helper.np_vec3_to_panda(ray_origin), helper.np_vec3_to_panda(ray_origin + ray_dir * 9999)) if not result.hasHit(): return None # Return a RaycastResult object raycast_obj = RaycastResult() raycast_obj.hit_node_id = result.getNode().name[:-11] raycast_obj.hit_point = helper.panda_vec3_to_np(result.getHitPos()) return raycast_obj
def start(self): self.kinematic = self.property_vals["kinematic"].lower() == "true" body_node = BulletRigidBodyNode(self.node.id + "_rigid_body") body_node.set_mass(float(self.property_vals["mass"])) self.body_path = EComponent.panda_root_node.attach_new_node(body_node) self.body_path.setPos( helper.np_vec3_to_panda( self.node.transform.get_world_translation())) rot = np.degrees(self.node.transform.get_world_rotation()) self.body_path.setHpr(LVector3f(rot[1], rot[0], rot[2])) self.body_path.setScale( helper.np_vec3_to_panda(self.node.transform.get_world_scale())) EComponent.physics_world.attachRigidBody(body_node)
def handle_update(self): if self.body_path is not None: if self.kinematic: self.body_path.setPos( helper.np_vec3_to_panda( self.node.transform.get_world_translation())) rot = np.degrees(self.node.transform.get_world_rotation()) self.body_path.setHpr(LVector3f(rot[1], rot[0], rot[2])) self.body_path.setScale( helper.np_vec3_to_panda( self.node.transform.get_world_scale())) else: self.node.transform.set_world_matrix( helper.panda_mat4_to_np(self.body_path.getMat()))
def cam_ctrl_task(self, task): # Update delta mouse curr_mouse = self.last_mouse if self.base.mouseWatcherNode.hasMouse(): curr_mouse = (self.base.mouseWatcherNode.getMouseX(), self.base.mouseWatcherNode.getMouseY()) delta_mouse = (curr_mouse[0] - self.last_mouse[0], curr_mouse[1] - self.last_mouse[1]) self.last_mouse = curr_mouse if self.scroll_down: # Translate camera if shift is down if self.shift_down: start_point = self.get_ray_plane_intersection( self.start_mouse, np.array([0, 0, 0]), self.plane_normal) plane_point = self.get_ray_plane_intersection( curr_mouse, np.array([0, 0, 0]), self.plane_normal) self.orbit_center = self.start_orbit - helper.np_vec3_to_panda( plane_point - start_point) # Recalculate camera position and direction self.update_cam() # Otherwise, just rotate else: # Adjust angles self.left_angle += -delta_mouse[0] self.up_angle += -delta_mouse[1] if self.left_angle > math.pi * 2: self.left_angle -= math.pi * 2 elif self.left_angle < 0: self.left_angle += math.pi * 2 if self.up_angle > math.pi * 2: self.up_angle -= math.pi * 2 elif self.up_angle < 0: self.up_angle += math.pi * 2 # Recalculate camera position and direction self.update_cam() return Task.cont