def _parse_geom(cls, geom): primitive = geom.get_primitives()[0] vertex_data = geom.get_vertex_data() vertex_reader = GeomVertexReader(vertex_data, 'vertex') triangle_count = primitive.get_num_primitives() triangles = [] vertex_positions = {} # Get triangles and vertex positions for triangle_index in range(triangle_count): start_index = primitive.get_primitive_start(triangle_index) end_index = primitive.get_primitive_end(triangle_index) vertex_indices = [] for i in range(start_index, end_index): vertex_index = primitive.get_vertex(i) vertex_reader.set_row(vertex_index) vertex_position = Vector(vertex_reader.getData3f()) vertex_positions[vertex_index] = vertex_position.freeze() vertex_indices.append(vertex_index) triangles.append(tuple(vertex_indices)) triangles_to_neighbours = cls._build_neighbours(triangles) return cls._build_nodes(triangles_to_neighbours, vertex_positions)
def update(self): """Process new mouse state""" mouse_node = base.mouseWatcherNode if mouse_node.hasMouse(): x = mouse_node.getMouseX() y = mouse_node.getMouseY() position = Vector((x, y)) self._last_position, self.position = self.position, position
def local_angular(self): parent = self._nodepath.getParent() inverse_rotation = parent.getQuat() inverse_rotation.invertInPlace() angular = self._node.getAngularVelocity() inverse_rotation.xform(angular) return Vector(angular)
def local_velocity(self): parent = self._nodepath.getParent() inverse_rotation = parent.getQuat() inverse_rotation.invertInPlace() velocity = self._node.getLinearVelocity() inverse_rotation.xform(velocity) return Vector(velocity)
def ray_test(self, target, source=None, distance=None, ignore_self=True, mask=None): """Perform a ray trace to a target :param target: target to trace towards :param source: optional origin of trace, otherwise object position :param distance: distance to use instead of vector length :rtype: :py:class:`game_system.physics.RayTestResult` """ if source is None: source = Vector(self._nodepath.getPos(base.render)) # Move target to appropriate position, if explicit distance if distance: direction = target - source direction.length = distance target = source + direction if mask is None: collision_mask = BitMask32.all_on() else: collision_mask = BitMask32() collision_mask.set_word(mask) world = self._node.get_python_tag("world") query_result = world.rayTestAll(tuple(source), tuple(target), collision_mask) sorted_hits = sorted(query_result.get_hits(), key=get_hit_fraction) for hit_result in sorted_hits: hit_node = hit_result.get_node() hit_entity = entity_from_nodepath(hit_node) if ignore_self and hit_entity is self._entity: continue hit_position = Vector(hit_result.get_hit_pos()) hit_distance = (hit_position - source).length hit_normal = Vector(hit_result.get_hit_normal()) return RayTestResult(hit_position, hit_normal, hit_entity, hit_distance)
def get_direction_vector(self, axis): """Get the axis vector of this object in world space :param axis: :py:class:`game_system.enums.Axis` value :rtype: :py:class:`game_system.coordinates.Vector` """ direction = Vec3(0, 0, 0) direction[axis] = 1 rotation = self._nodepath.getQuat() direction = rotation.xform(direction) return Vector(direction)
def get_screen_direction(self, x=0.5, y=0.5): """Find direction along screen vector :param x: screen space x coordinate :param y: screen space y coordinate """ mouse_pos = x, y from_point = Point3() to_point = Point3() self._node.get_lens().extrude(mouse_pos, from_point, to_point) relative_direction = to_point - from_point direction = base.render.get_relative_vector(self._nodepath, relative_direction) return Vector(direction)
def process_inputs(self, buttons, ranges): if buttons['debug'] == ButtonState.pressed: self.shoot() #self.debug = not self.debug if self.debug: self.shoot() # print(self.buffer) # print(buttons) y_sign = 0 if buttons['up'] in {ButtonState.pressed, ButtonState.held}: y_sign += 1 if buttons['down'] in {ButtonState.pressed, ButtonState.held}: y_sign -= 1 x_sign = 0 if buttons['right'] in {ButtonState.pressed, ButtonState.held}: x_sign -= 1 if buttons['left'] in {ButtonState.pressed, ButtonState.held}: x_sign += 1 y_speed = y_sign * 2.0 rotation_speed = x_sign pawn = self.pawn if pawn is None: return velocity = Vector((0.0, y_speed, 0.0)) velocity.rotate(pawn.transform.world_orientation) angular = Vector((0.0, 0.0, rotation_speed)) pawn.physics.world_angular = angular pawn.physics.world_velocity = velocity
def __init__(self): self.position = Vector((0.0, 0.0)) self._last_position = Vector((0.0, 0.0))
def shoot(self): cube = TestActor() pawn = self.pawn cube.transform.world_position = pawn.transform.world_position + Vector( (-5, 0, 0))
def world_position(self): return Vector(self._nodepath.getPos(base.render))
def world_angular(self): return Vector(self._node.getAngularVelocity())
def world_velocity(self): return Vector(self._node.getLinearVelocity())