Finish new matrix translation code

This commit is contained in:
MegaMech
2025-03-29 12:30:13 -06:00
parent a6d19eee1b
commit 9de522990b
13 changed files with 95 additions and 82 deletions
+27 -2
View File
@@ -1,16 +1,41 @@
#include <libultraship.h>
#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; }
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]);
}
+2
View File
@@ -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();
+1 -1
View File
@@ -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;
+23
View File
@@ -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" {
+1
View File
@@ -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);
+1 -12
View File
@@ -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<int16_t>(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; }
-1
View File
@@ -23,7 +23,6 @@ public:
virtual ~AShip() = default;
virtual void Tick() override;
virtual void Draw(Camera*) override;
virtual bool IsMod() override;
FVector Spawn;
+17 -16
View File
@@ -1,6 +1,7 @@
#include "SpaghettiShip.h"
#include <libultra/gbi.h>
#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<int16_t>(angle * (32768.0f / M_PI / 2.0f));
Rot[1] = -static_cast<int16_t>(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);
+2 -2
View File
@@ -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;
+11 -19
View File
@@ -1,6 +1,7 @@
#include "Starship.h"
#include <libultra/gbi.h>
#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; }
-3
View File
@@ -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};
};
+9 -26
View File
@@ -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) {
//}
}
}
+1
View File
@@ -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;