0

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?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Luca-Fly
  • 15
  • 5
  • see [Understanding 4x4 homogenous transform matrices](https://stackoverflow.com/a/28084380/2521214) ... especially difference between local and global rotation (bullet #5) and also the example links at the bottom ... – Spektre May 13 '23 at 06:30
  • I have looked at the link and those at the bottom but i am afraid i dont quite understand them. This is my first ever 3d project. If i am understanding it correctly i would need to creat a global transform and move and rotate the camera on a local trnasform… right? – Luca-Fly May 13 '23 at 15:39
  • its matter of used notation (row-major/column-major and multiplication order) either you apply the transforms on direct or inverse matrix – Spektre May 14 '23 at 03:17

0 Answers0