I have followed a tutorial Here: https://www.youtube.com/watch?v=M_Hx0g5vFko i followed his tutorial and have finished it (except the part of making the axes shown on the screen). i have reached a problem where i have free movement and rotation on the y axis (left and right) seems to work. but for some reason when i rotate the x axis (up and down) it seems to rotate the global x axis instead of the cameras x axis.
Here is the full code for the camera:
import pygame as pg
from matrix_functions import *
class Camera:
def __init__(self, render, position):
self.render = render
self.position = np.array([*position, 1.0])
self.right = np.array([1, 0, 0, 1])
self.up = np.array([0, 1, 0, 1])
self.forward = np.array([0, 0, 1, 1])
self.h_fov = math.pi / 3
self.v_fov = self.h_fov * (render.HEIGHT / render.WIDTH)
self.near_plane = 0.1
self.far_plane = 100
self.move_speed = 0.02
self.rotation_speed = 0.02
def camera_yaw(self, angle):
rotate = rotate_y(angle)
self.forward = self.forward @ rotate
self.right = self.right @ rotate
self.up = self.up @ rotate
def camera_pitch(self, angle):
rotate = rotate_x(angle)
self.forward = self.forward @ rotate
self.right = self.right @ rotate
self.up = self.up @ rotate
def control(self):
key = pg.key.get_pressed()
if key[pg.K_d]:
self.position += self.right * self.move_speed
if key[pg.K_a]:
self.position -= self.right * self.move_speed
if key[pg.K_w]:
self.position += self.forward * self.move_speed
if key[pg.K_s]:
self.position -= self.forward * self.move_speed
if key[pg.K_q]:
self.position += self.up * self.move_speed
if key[pg.K_e]:
self.position -= self.up * self.move_speed
if key[pg.K_LEFT]:
self.camera_yaw(-self.rotation_speed)
if key[pg.K_RIGHT]:
self.camera_yaw(self.rotation_speed)
if key[pg.K_UP]:
self.camera_pitch(-self.rotation_speed)
if key[pg.K_DOWN]:
self.camera_pitch(self.rotation_speed)
def translate_matrix(self):
x, y, z, w = self.position
return np.array([
[1, 0, 0, 0],
[0, 1, 0, 1],
[0, 0, 1, 0],
[-x, -y, -z, 1]
])
def rotate_matrix(self):
rx, ry, rz, w = self.right
fx, fy, fz, w = self.forward
ux, uy, uz, w = self.up
return np.array([
[rx, ux, fx, 0],
[ry, uy, fy, 0],
[rz, uz, fz, 0],
[0, 0, 0, 1]
])
def camera_matrix(self):
return self.translate_matrix() @ self.rotate_matrix()
is there any way i can fix this?