Пример #1
0
def world_to_screen_point(point, camera):
    """Get view of the point on screen"""
    tr = camera.transform.get_global_transform()

    screen_plane = Plane.From_normal(tr.forward, point)
    screen_middle_point = Ray(tr.position,
                              tr.forward).intersect_plane(screen_plane)

    screen_width = math.tan(math.radians(camera.FOV / 2)) * vector3.magnitude(
        vector3.subtract(screen_middle_point, tr.position)) * 2
    screen_height = screen_width / camera.aspect_ratio

    point_in_camera_space = convert_to_different_space(
        vector3.subtract(point, screen_middle_point), tr.right, tr.down,
        tr.forward)

    point_right_vector = vector3.add(
        vector3.scale(tr.right, screen_width / 2),
        vector3.scale(tr.right, point_in_camera_space[0]))
    point_down_vector = vector3.add(
        vector3.scale(tr.down, screen_height / 2),
        vector3.scale(tr.down, point_in_camera_space[1]))
    point_right = vector3.magnitude(point_right_vector) / screen_width
    point_down = vector3.magnitude(point_down_vector) / screen_height
    scale_right = vector3.scale_div(point_right_vector, tr.right)
    if scale_right < 0:
        point_right = -point_right
    scale_down = vector3.scale_div(point_down_vector, tr.down)
    if scale_down < 0:
        point_down = -point_down
    return (point_right * camera.resolution[0],
            point_down * camera.resolution[1])
Пример #2
0
def Ray_on_pixel(point2d):
    cam = camera.main_camera
    tr = cam.transform.get_global_transform()
    dist_from_mid = vector2.subtract(point2d, cam.middle_pixel)
    tg_alfa = math.tan(math.radians(
        cam.FOV / 2)) * dist_from_mid[0] / (cam.resolution[0] / 2)
    tg_beta = math.tan(math.radians(
        cam.FOV_vertical / 2)) * dist_from_mid[1] / (cam.resolution[1] / 2)
    direction = vector3.add(
        vector3.add(tr.forward, vector3.scale(tr.right, tg_alfa)),
        vector3.scale(tr.down, tg_beta))
    direction = vector3.normalize(direction)
    ray = Ray(tr.position, direction)
    return ray
Пример #3
0
 def normalize(self):
     normal = self.normal()
     if vector3.magnitude(normal) != 0:
         scale = 1 / vector3.magnitude(normal)
         normal = vector3.scale(normal, scale)
         d = self.factors[3] * scale
         self.factors = (normal[0], normal[1], normal[2], d)
Пример #4
0
 def Calculate_normal(self, inverted=False):
     plane_vector1 = vector3.subtract(self.vertex[0], self.vertex[1])
     plane_vector2 = vector3.subtract(self.vertex[1], self.vertex[2])
     normal = vector3.cross(plane_vector1, plane_vector2)
     normal = vector3.normalize(normal)
     if not inverted:
         return normal
     else:
         return vector3.scale(normal, -1)
Пример #5
0
    def recalculate(self):
        tr = self.camera.transform.get_global_transform()
        normal = tr.forward
        point = vector3.add(tr.position, vector3.scale(tr.forward, self.camera.clip_min))
        self.clipping_plane = Plane.From_normal(normal, point)

        self.previous_position = tr.position
        self.previous_rotation = tr.rotation
        self.previous_scale = tr.scale
Пример #6
0
def _sort_faces_():
    #TODO: FIX THIS
    switch = True
    while switch:
        switch = False
        for i in range(0, len(all_faces) - 1):
            if vector3.dot(all_faces[i].normal,
                           Screen.main_camera.global_transform.forward) < 0:
                vc = all_faces[i].normal
            else:
                vc = vector3.scale(all_faces[i].normal, -1)
            above = True
            below = True
            for vertex in all_faces[i + 1].vertex:
                if Geometry.Axis_view(
                        vc, vector3.subtract(vertex,
                                             all_faces[i].vertex[0])) >= 0:
                    below = False
                else:
                    above = False
            if not above and not below:
                if vector3.dot(
                        all_faces[i + 1].normal,
                        Screen.main_camera.global_transform.forward) < 0:
                    vc = all_faces[i + 1].normal
                else:
                    vc = vector3.scale(all_faces[i + 1].normal, -1)
                above = True
                below = True
                for vertex in all_faces[i].vertex:
                    if Geometry.Axis_view(
                            vc,
                            vector3.subtract(vertex,
                                             all_faces[i + 1].vertex[0])) >= 0:
                        below = False
                    else:
                        above = False
                if above:
                    all_faces[i], all_faces[i + 1] = all_faces[i +
                                                               1], all_faces[i]
                    switch = True
            elif below:
                all_faces[i], all_faces[i + 1] = all_faces[i + 1], all_faces[i]
                switch = True
Пример #7
0
    def Rotate(self,
               angle=0,
               axis=(0, 0, 0),
               rotation=Quaternion(1, (0, 0, 0))):
        """Rotation of the object"""
        if axis == (0, 0, 0):
            self.rotation = Quaternion.composite_rotations(
                self.rotation, rotation)
        else:
            rotQuaternion = Quaternion.from_angle_axis(angle, axis)
            self.rotation = Quaternion.composite_rotations(
                self.rotation, rotQuaternion)

        self._update_mesh_()

        self.forward = Quaternion.rotate_vector((1, 0, 0), self.rotation)
        self.backward = vector3.scale(self.forward, -1)
        self.right = Quaternion.rotate_vector((0, -1, 0), self.rotation)
        self.left = vector3.scale(self.right, -1)
        self.up = Quaternion.rotate_vector((0, 0, 1), self.rotation)
        self.down = vector3.scale(self.up, -1)
Пример #8
0
def Gravity():
    for obj in Mesh.objects:
        if obj.rigidbody:
            if obj.rigidbody.use_gravity:
                height = obj.rigidbody.Dist_from_ground()
                if not height or height > 0.05:
                    obj.rigidbody.time_in_air += Other.delta_time
                    obj.rigidbody.velocity = vector3.add(
                        obj.rigidbody.velocity,
                        vector3.scale(g_acc, obj.rigidbody.time_in_air))
                    if height != None and obj.rigidbody.velocity[
                            2] < 0 and obj.rigidbody.velocity[
                                2] * Other.delta_time < -height:
                        obj.rigidbody.velocity = (obj.rigidbody.velocity[0],
                                                  obj.rigidbody.velocity[1],
                                                  -height /
                                                  (2 * Other.delta_time))
                else:
                    obj.rigidbody.time_in_air = 0
                    if obj.rigidbody.velocity[2] < 0:
                        obj.rigidbody.velocity = (obj.rigidbody.velocity[0],
                                                  obj.rigidbody.velocity[1], 0)
            obj.rigidbody.Apply()
Пример #9
0
def Screen_to_world(point2d, distance):
    ray = Ray_on_pixel(point2d)
    point = vector3.add(ray.origin, vector3.scale(ray.direction, distance))
    return point
Пример #10
0
 def Invert_normal(self):
     self.factors = vector3.scale(self.factors, -1)
Пример #11
0
def is_point_behind_camera_clip(point, camera):
    tr = camera.transform.get_global_transform()
    clip_position = vector3.add(tr.position,
                                vector3.scale(tr.forward, camera.clip_min))
    return vector3.dot(vector3.subtract(point, clip_position),
                       tr.forward) < 0.0
Пример #12
0
            crouching = False
        else:
            cam.transform.Translate((0, 0, -0.9))
            crouching = True

    if Input.Get_key("lshift"):
        if crouching:
            crouching = False
            cam.transform.Translate((0, 0, 0.9))
        move_speed = run_speed
    elif not crouching:
        move_speed = walk_speed
    else:
        move_speed = crouch_speed

    right_move_vec = vector3.scale(cam.transform.right, player_move[0])
    forward_move_vec = vector3.scale(cam.transform.forward, player_move[1])
    move_vec = (vector3.add(right_move_vec, forward_move_vec)[0],
                vector3.add(right_move_vec, forward_move_vec)[1], 0)
    move_vec = vector3.normalize(move_vec)
    move_vec = vector3.scale(move_vec, move_speed)

    player.rigidbody.Move(vector3.scale(move_vec, Other.delta_time))

    if vector3.magnitude(move_vec) > 0 and step_timer <= 0:
        if player.rigidbody.Dist_from_ground(
        ) and player.rigidbody.Dist_from_ground() <= 0.05:
            if move_speed == walk_speed:
                step_channel.play(footstep)
                step_timer = walk_step_time
            elif move_speed == run_speed:
Пример #13
0
 def from_angle_axis(cls, angle, axis):
     axis = vector3.normalize(axis)
     v = vector3.scale(axis, math.radians(angle))
     q = quaternion.from_rotation_vector(v)
     return cls.from_np_quaternion(q)
Пример #14
0
 def Apply(self):
     self.Move(vector3.scale(self.velocity, Other.delta_time))