From 9de522990bcc87818cfd6bead356264209ef1ec7 Mon Sep 17 00:00:00 2001 From: MegaMech <7255464+MegaMech@users.noreply.github.com> Date: Sat, 29 Mar 2025 12:30:13 -0600 Subject: [PATCH] Finish new matrix translation code --- src/engine/Actor.cpp | 29 ++++++++++++++++++++++-- src/engine/Actor.h | 2 ++ src/engine/CoreMath.h | 2 +- src/engine/Matrix.cpp | 23 +++++++++++++++++++ src/engine/Matrix.h | 1 + src/engine/actors/Ship.cpp | 13 +---------- src/engine/actors/Ship.h | 1 - src/engine/actors/SpaghettiShip.cpp | 33 ++++++++++++++------------- src/engine/actors/SpaghettiShip.h | 4 ++-- src/engine/actors/Starship.cpp | 30 +++++++++---------------- src/engine/actors/Starship.h | 3 --- src/engine/editor/Gizmo.cpp | 35 ++++++++--------------------- src/engine/editor/Gizmo.h | 1 + 13 files changed, 95 insertions(+), 82 deletions(-) diff --git a/src/engine/Actor.cpp b/src/engine/Actor.cpp index be86d2a5f..f3254fed0 100644 --- a/src/engine/Actor.cpp +++ b/src/engine/Actor.cpp @@ -1,16 +1,41 @@ #include +#include "Matrix.h" #include "Actor.h" +extern "C" { +#include "math_util.h" +} + AActor::AActor() {} // Virtual functions to be overridden by derived classes void AActor::Tick() { } -void AActor::Draw(Camera *camera) { } +void AActor::Draw(Camera *camera) { + if (Model) { + Mat4 mtx; + + gSPSetGeometryMode(gDisplayListHead++, G_SHADING_SMOOTH); + gSPClearGeometryMode(gDisplayListHead++, G_LIGHTING); + + ApplyMatrixTransformations(mtx, *(FVector*)Pos, *(IRotator*)Rot, Scale); + if (render_set_position(mtx, 0) != 0) { + gSPDisplayList(gDisplayListHead++, Model); + } + } +} void AActor::Collision(Player* player, AActor* actor) {} void AActor::VehicleCollision(s32 playerId, Player* player){} void AActor::Destroy() { // Set uuid to zero. memset(uuid, 0, sizeof(uuid)); } -bool AActor::IsMod() { return false; } \ No newline at end of file +bool AActor::IsMod() { return false; } +void AActor::SetLocation(FVector pos) { + Pos[0] = pos.x; + Pos[1] = pos.y; + Pos[2] = pos.z; +} +FVector AActor::GetLocation() const { + return FVector(Pos[0], Pos[1], Pos[2]); +} diff --git a/src/engine/Actor.h b/src/engine/Actor.h index 63585d97d..fb0a9f89e 100644 --- a/src/engine/Actor.h +++ b/src/engine/Actor.h @@ -38,6 +38,8 @@ public: virtual void Draw(Camera*); virtual void Collision(Player* player, AActor* actor); virtual void VehicleCollision(s32 playerId, Player* player); + void SetLocation(FVector pos); + FVector GetLocation() const; virtual void Destroy(); virtual bool IsMod(); diff --git a/src/engine/CoreMath.h b/src/engine/CoreMath.h index f3dcab046..8281cd58d 100644 --- a/src/engine/CoreMath.h +++ b/src/engine/CoreMath.h @@ -128,7 +128,7 @@ struct IRotator { return *this; } - [[nodiscard]] IRotator Set(uint16_t p, uint16_t y, uint16_t r) { + [[nodiscard]] void Set(uint16_t p, uint16_t y, uint16_t r) { pitch = p; yaw = y; roll = r; diff --git a/src/engine/Matrix.cpp b/src/engine/Matrix.cpp index 2ecf8e538..1f2301258 100644 --- a/src/engine/Matrix.cpp +++ b/src/engine/Matrix.cpp @@ -107,6 +107,29 @@ void ApplyMatrixTransformations(Mat4 mtx, FVector pos, IRotator rot, FVector sca mtx[3][3] = 1.0f; } +void AddLocalRotation(Mat4 mat, IRotator rot) { + f32 sin_pitch = sins(rot.pitch); + f32 cos_pitch = coss(rot.pitch); + f32 sin_yaw = sins(rot.yaw); + f32 cos_yaw = coss(rot.yaw); + f32 sin_roll = sins(rot.roll); + f32 cos_roll = coss(rot.roll); + + // Modify only the rotation part (keep translation intact) + mat[0][0] = (cos_yaw * cos_roll) + (sin_pitch * sin_yaw * sin_roll); + mat[0][1] = (cos_pitch * sin_roll); + mat[0][2] = (-sin_yaw * cos_roll) + (sin_pitch * cos_yaw * sin_roll); + + mat[1][0] = (-cos_yaw * sin_roll) + (sin_pitch * sin_yaw * cos_roll); + mat[1][1] = (cos_pitch * cos_roll); + mat[1][2] = (sin_yaw * sin_roll) + (sin_pitch * cos_yaw * cos_roll); + + mat[2][0] = (cos_pitch * sin_yaw); + mat[2][1] = -sin_pitch; + mat[2][2] = (cos_pitch * cos_yaw); +} + + // API extern "C" { diff --git a/src/engine/Matrix.h b/src/engine/Matrix.h index 31c37b52b..180ebfb16 100644 --- a/src/engine/Matrix.h +++ b/src/engine/Matrix.h @@ -9,6 +9,7 @@ #ifdef __cplusplus extern "C" { void ApplyMatrixTransformations(Mat4 mtx, FVector pos, IRotator rot, FVector scale); +void AddLocalRotation(Mat4 mat, IRotator rot); #endif void ClearMatrixPools(void); void AddHudMatrix(Mat4 mtx, s32 flags); diff --git a/src/engine/actors/Ship.cpp b/src/engine/actors/Ship.cpp index e27e33bbd..5af3553e2 100644 --- a/src/engine/actors/Ship.cpp +++ b/src/engine/actors/Ship.cpp @@ -35,6 +35,7 @@ AShip::AShip(FVector pos, AShip::Skin skin) { _skin = ship3_2Ship_mesh; break; } + Model = _skin; } void AShip::Tick() { @@ -52,16 +53,4 @@ void AShip::Tick() { // Rot.yaw = -static_cast(angle * (32768.0f / M_PI / 2.0f)); } -void AShip::Draw(Camera *camera) { - Mat4 shipMtx; - - gSPSetGeometryMode(gDisplayListHead++, G_SHADING_SMOOTH); - gSPClearGeometryMode(gDisplayListHead++, G_LIGHTING); - - ApplyMatrixTransformations(shipMtx, *(FVector*)Pos, *(IRotator*)Rot, Scale); - if (render_set_position(shipMtx, 0) != 0) { - gSPDisplayList(gDisplayListHead++, _skin); - } -} - bool AShip::IsMod() { return true; } diff --git a/src/engine/actors/Ship.h b/src/engine/actors/Ship.h index d81fc630d..fe5743922 100644 --- a/src/engine/actors/Ship.h +++ b/src/engine/actors/Ship.h @@ -23,7 +23,6 @@ public: virtual ~AShip() = default; virtual void Tick() override; - virtual void Draw(Camera*) override; virtual bool IsMod() override; FVector Spawn; diff --git a/src/engine/actors/SpaghettiShip.cpp b/src/engine/actors/SpaghettiShip.cpp index 322268793..b09ada319 100644 --- a/src/engine/actors/SpaghettiShip.cpp +++ b/src/engine/actors/SpaghettiShip.cpp @@ -1,6 +1,7 @@ #include "SpaghettiShip.h" #include +#include "Matrix.h" extern "C" { #include "common_structs.h" @@ -11,8 +12,12 @@ extern "C" { ASpaghettiShip::ASpaghettiShip(FVector pos) { Name = "Spaghetti Ship"; + Pos[0] = pos.x; + Pos[1] = pos.y; + Pos[2] = pos.z; Spawn = pos; Spawn.y += 10; + Scale = {0.4, 0.4, 0.4}; } void ASpaghettiShip::Tick() { @@ -23,12 +28,11 @@ void ASpaghettiShip::Tick() { angle += speed; // Increment the angle to move in a circle // Update the position based on a circular path - Pos.x = Spawn.x + radius * cosf(angle); - Pos.z = Spawn.z + radius * sinf(angle); + Pos[0] = Spawn.x + radius * cosf(angle); + Pos[2] = Spawn.z + radius * sinf(angle); // Rotate to face forward along the circle - Rot.yaw = -static_cast(angle * (32768.0f / M_PI / 2.0f)); - + Rot[1] = -static_cast(angle * (32768.0f / M_PI / 2.0f)); WheelRot.pitch += 500; } @@ -37,29 +41,27 @@ void ASpaghettiShip::Draw(Camera *camera) { Mat4 shipMtx; Mat4 objectMtx; Mat4 resultMtx; - Vec3f hullPos = {Pos.x, Pos.y, Pos.z}; - Vec3s hullRot = {Rot.pitch, Rot.yaw, Rot.roll}; + Vec3f hullPos = {Pos[0], Pos[1], Pos[2]}; + Vec3s hullRot = {Rot[0], Rot[1], Rot[2]}; Vec3s rot = {WheelRot.pitch, WheelRot.yaw, WheelRot.roll}; gSPSetGeometryMode(gDisplayListHead++, G_SHADING_SMOOTH); gSPClearGeometryMode(gDisplayListHead++, G_LIGHTING); - mtxf_pos_rotation_xyz(shipMtx, hullPos, *(Vec3s*)&Rot); - mtxf_scale(shipMtx, 0.4); + ApplyMatrixTransformations(shipMtx, *(FVector*)Pos, *(IRotator*)Rot, Scale); if (render_set_position(shipMtx, 0) != 0) {} // Render the ships hull - Vec3f hullPos2 = {0, 0, 0}; - mtxf_translate(objectMtx, hullPos2); + ApplyMatrixTransformations(objectMtx, {0, 0, 0}, {0, 0, 0}, {1, 1, 1}); mtxf_multiplication(resultMtx, shipMtx, objectMtx); if (render_set_position(resultMtx, 3) != 0) { gSPDisplayList(gDisplayListHead++, ship1_spag1_mesh); } // Front tyre - Vec3f pos = {0, 0, 110}; - mtxf_rotate_x(shipMtx, WheelRot.pitch); - mtxf_translate(objectMtx, pos); + ApplyMatrixTransformations(objectMtx, FVector(0, 0, 110), IRotator(0, 0, 0), FVector(1, 1, 1)); + mtxf_identity(shipMtx); + AddLocalRotation(shipMtx, WheelRot); mtxf_multiplication(resultMtx, shipMtx, objectMtx); if (render_set_position(resultMtx, 3) != 0) { gSPDisplayList(gDisplayListHead++, wheels_Spaghetti_002_mesh); @@ -67,9 +69,8 @@ void ASpaghettiShip::Draw(Camera *camera) { } // Back tyre - Vec3f pos2 = {0, 0, -165}; - mtxf_rotate_x(shipMtx, WheelRot.pitch); - mtxf_translate(objectMtx, pos2); + AddLocalRotation(shipMtx, WheelRot); + ApplyMatrixTransformations(objectMtx, FVector(0, 0, -165), {0, 0, 0}, {1, 1, 1}); mtxf_multiplication(resultMtx, shipMtx, objectMtx); if (render_set_position(resultMtx, 3) != 0) { gSPDisplayList(gDisplayListHead++, wheels_Spaghetti_002_mesh); diff --git a/src/engine/actors/SpaghettiShip.h b/src/engine/actors/SpaghettiShip.h index 3e5b4d382..f5a2e2fd5 100644 --- a/src/engine/actors/SpaghettiShip.h +++ b/src/engine/actors/SpaghettiShip.h @@ -20,8 +20,8 @@ public: virtual bool IsMod() override; FVector Spawn; - FVector Pos; - IRotator Rot = {0, 0, 0}; + //FVector Pos; + //IRotator Rot = {0, 0, 0}; IRotator WheelRot = {0, 0, 0}; private: f32 scale; diff --git a/src/engine/actors/Starship.cpp b/src/engine/actors/Starship.cpp index 377af20a0..e104d0eb6 100644 --- a/src/engine/actors/Starship.cpp +++ b/src/engine/actors/Starship.cpp @@ -1,6 +1,7 @@ #include "Starship.h" #include +#include "Matrix.h" extern "C" { #include "common_structs.h" @@ -12,6 +13,9 @@ extern "C" { AStarship::AStarship(FVector pos) { Name = "Starship"; Spawn = pos; + SetLocation(pos); + Scale = FVector(1.5, 1.5, 1.5); + Model = starship_Cube_mesh; } void AStarship::Tick() { @@ -22,29 +26,17 @@ void AStarship::Tick() { angle += speed; // Move relative to the initial position - Pos.x = Spawn.x + radius * cosf(angle); - Pos.z = Spawn.z + radius * sinf(angle); + + FVector pos = GetLocation(); + pos.x = Spawn.x + radius * cosf(angle); + pos.z = Spawn.z + radius * sinf(angle); + SetLocation(pos); // Keep y from changing (or adjust it if necessary) - Pos.y = Spawn.y; + //Pos.y = Spawn.y; // Rotate to face forward along the circle - Rot.yaw = angle * (180.0f / M_PI) + 90.0f; -} - -void AStarship::Draw(Camera *camera) { - Mat4 shipMtx; - Vec3f hullPos = {Pos.x, Pos.y, Pos.z}; - Vec3s hullRot = {Rot.pitch, Rot.yaw, Rot.roll}; - - gSPSetGeometryMode(gDisplayListHead++, G_SHADING_SMOOTH); - gSPClearGeometryMode(gDisplayListHead++, G_LIGHTING); - - mtxf_pos_rotation_xyz(shipMtx, hullPos, hullRot); - mtxf_scale(shipMtx, 1.5); - if (render_set_position(shipMtx, 0) != 0) { - gSPDisplayList(gDisplayListHead++, starship_Cube_mesh); - } + Rot[1] = angle * (180.0f / M_PI) + 90.0f; } bool AStarship::IsMod() { return true; } diff --git a/src/engine/actors/Starship.h b/src/engine/actors/Starship.h index 3599f0304..71b898358 100644 --- a/src/engine/actors/Starship.h +++ b/src/engine/actors/Starship.h @@ -16,10 +16,7 @@ public: virtual ~AStarship() = default; virtual void Tick() override; - virtual void Draw(Camera*) override; virtual bool IsMod() override; FVector Spawn; - FVector Pos; - IRotator Rot = {0, 0, 0}; }; diff --git a/src/engine/editor/Gizmo.cpp b/src/engine/editor/Gizmo.cpp index 8a7ae6886..774c1ab4c 100644 --- a/src/engine/editor/Gizmo.cpp +++ b/src/engine/editor/Gizmo.cpp @@ -206,15 +206,8 @@ void Gizmo::DrawHandles() { Mat4 mainMtx; Vec3s rot = {0, 0, 0}; - mtxf_pos_rotation_xyz(mainMtx, &Pos.x, rot); - //mtxf_scale(mainMtx, 0.05f); - - + ApplyMatrixTransformations(mainMtx, Pos, Rot, {1, 1, 1}); Editor_AddMatrix(mainMtx, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_MODELVIEW); - //if (render_set_position(mainMtx, 0) != 0) { - //gSPDisplayList(gDisplayListHead++, wheels_Spaghetti_002_mesh); - //gSPDisplayList(gDisplayListHead++, handle_Cylinder_mesh); - //} handle_f3dlite_material_lights = gdSPDefLights1( 0x7F, 0x7F, 0x7F, @@ -222,28 +215,20 @@ void Gizmo::DrawHandles() { Mat4 RedXMtx; Vec3s rot1 = {0, 0, 0}; - Vec3f pos1 = {Pos.x, Pos.y, Pos.z - _gizmoOffset}; - mtxf_pos_rotation_xyz(RedXMtx, pos1, rot1); - mtxf_scale(RedXMtx, 0.05); + FVector pos1 = {Pos.x, Pos.y, Pos.z - _gizmoOffset}; + ApplyMatrixTransformations(RedXMtx, pos1, Rot, {0.05f, 0.05f, 0.05f}); Editor_AddMatrix(RedXMtx, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_MODELVIEW); gSPDisplayList(gDisplayListHead++, handle_Cylinder_mesh); - //if (render_set_position(RedXMtx, 0) != 0) { - //} - - Vec3s rot2 = {0, 0x4000, 0}; - handle_f3dlite_material_lights = gdSPDefLights1( 0x7F, 0x7F, 0x7F, 0, 0xFF, 0, 0x49, 0x49, 0x49); Mat4 GreenYMtx; - Vec3f pos2 = {Pos.x - _gizmoOffset, Pos.y, Pos.z}; - mtxf_pos_rotation_xyz(GreenYMtx, pos2, rot2); - mtxf_scale(GreenYMtx, 0.05); + FVector pos2 = {Pos.x - _gizmoOffset, Pos.y, Pos.z}; + ApplyMatrixTransformations(GreenYMtx, pos2, IRotator(0, 90, 0), {0.05f, 0.05f, 0.05f}); + Editor_AddMatrix(GreenYMtx, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_MODELVIEW); gSPDisplayList(gDisplayListHead++, handle_Cylinder_mesh); - //if (render_set_position(GreenYMtx, 0) != 0) { - //} Vec3s rot3 = {0x4000, 0, 0}; @@ -252,12 +237,10 @@ void Gizmo::DrawHandles() { 0, 0, 0xFF, 0x49, 0x49, 0x49); Mat4 BlueZMtx; - Vec3f pos3 = {Pos.x, Pos.y + _gizmoOffset, Pos.z}; - mtxf_pos_rotation_xyz(BlueZMtx, pos3, rot3); - mtxf_scale(BlueZMtx, 0.05); + FVector pos3 = {Pos.x, Pos.y + _gizmoOffset, Pos.z}; + ApplyMatrixTransformations(BlueZMtx, pos3, IRotator(90, 0, 0), {0.05f, 0.05f, 0.05f}); + Editor_AddMatrix(BlueZMtx, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_MODELVIEW); gSPDisplayList(gDisplayListHead++, handle_Cylinder_mesh); - //if (render_set_position(BlueZMtx, 0) != 0) { - //} } } diff --git a/src/engine/editor/Gizmo.h b/src/engine/editor/Gizmo.h index b9c88254a..ad8aec724 100644 --- a/src/engine/editor/Gizmo.h +++ b/src/engine/editor/Gizmo.h @@ -54,6 +54,7 @@ public: GameObject BlueCollision; FVector Pos; // Global scene view + IRotator Rot = {0, 0, 0}; float _gizmoOffset = 8.0f; float AllAxisRadius = 4.0f; // Free move selection radius float PickDistance;