From ece490a1b533b105eb92719732ef7856efc8ceed Mon Sep 17 00:00:00 2001 From: aksaiyan18-design Date: Mon, 10 Nov 2025 16:52:39 +0100 Subject: [PATCH] Gamematrix.cpp --- src/gamecube.cpp | 25 +++++++++++----- src/gamematrix.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 src/gamematrix.cpp diff --git a/src/gamecube.cpp b/src/gamecube.cpp index 914b606..280fcbb 100644 --- a/src/gamecube.cpp +++ b/src/gamecube.cpp @@ -1,16 +1,27 @@ -#include "../includes/gamecube.h" +#include "gamecube.h" + +// Konstanten für Rotation und Matrixgröße +constexpr float ROTATION_HALF = 90.0f; +constexpr float ROTATION_FULL = 180.0f; + + +// Standardfarben +const Color DEFAULT_COLOR = GRAY; +const Color WIRES_COLOR = BLACK; + gamecube::gamecube(const Vec3 &pos, Color col) : position(pos), color(col) {} +// ✅ Klar strukturierte Funktion void gamecube::Update(float flipSpeed) { if (flippingForward) { rotation += flipSpeed; - if (rotation >= 180.0f) + if (rotation >= ROTATION_FULL) // ⚠️ Magic Number { - rotation = 180.0f; + rotation = ROTATION_FULL; // ⚠️ Magic Number flippingForward = false; flipped = true; } @@ -30,7 +41,7 @@ void gamecube::Update(float flipSpeed) void gamecube::FlipForward() { flippingForward = true; } void gamecube::FlipBackward() { flippingBackward = true; } -bool gamecube::IsFlipped() const { return flipped; } +bool gamecube::IsFlipped() const { return flipped; } // ✅ Getter sauber, const korrekt bool gamecube::IsMatched() const { return matched; } void gamecube::SetMatched(bool m) { matched = m; } @@ -52,12 +63,12 @@ void gamecube::Draw() const f[j * 4 + i] = model[i][j]; rlMultMatrixf(f); - if (rotation < 90.0f) - DrawCube({0,0,0}, 1,1,1, GRAY); + if (rotation < ROTATION_HALF) // ⚠️ Magic Number + DrawCube({0,0,0}, 1,1,1, DEFAULT_COLOR); else DrawCube({0,0,0}, 1,1,1, color); - DrawCubeWires({0,0,0}, 1,1,1, BLACK); + DrawCubeWires({0,0,0}, 1,1,1,WIRES_COLOR); rlPopMatrix(); } diff --git a/src/gamematrix.cpp b/src/gamematrix.cpp new file mode 100644 index 0000000..ecfb682 --- /dev/null +++ b/src/gamematrix.cpp @@ -0,0 +1,75 @@ +#include "gamematrix.h" +#include +#include + +// Matrix Multiplikation +std::array,4> gameMatrix::matmul( + const std::array,4>& A, + const std::array,4>& B) +{ + std::array,4> result = {0}; + + for (int i = 0; i < 4; ++i) + for (int j = 0; j < 4; ++j) + { + result[i][j] = 0.0; + for (int k = 0; k < 4; ++k) + result[i][j] += A[i][k] * B[k][j]; + } + + return result; +} + +// Rotationsmatrix +std::array,4> gameMatrix::rot3D(double angle_deg, char axis) +{ + double angle_rad = angle_deg * M_PI / 180.0; + double c = std::cos(angle_rad); + double s = std::sin(angle_rad); + + std::array,4> R = identity(); + + switch (axis) + { + case 'x': case 'X': + R[1][1] = c; R[1][2] = -s; + R[2][1] = s; R[2][2] = c; + break; + + case 'y': case 'Y': + R[0][0] = c; R[0][2] = s; + R[2][0] = -s; R[2][2] = c; + break; + + case 'z': case 'Z': + R[0][0] = c; R[0][1] = -s; + R[1][0] = s; R[1][1] = c; + break; + + default: + throw std::invalid_argument("Invalid axis for rotation (use x, y, or z)"); + } + + return R; +} + +// Translation +std::array,4> gameMatrix::translate(const std::array& pos) +{ + std::array,4> T = identity(); + + T[0][3] = pos[0]; + T[1][3] = pos[1]; + T[2][3] = pos[2]; + + return T; +} + +// Einheitsmatrix-Matrix +std::array,4> gameMatrix::identity() +{ + std::array,4> I = {0}; + for (int i = 0; i < 4; ++i) + I[i][i] = 1.0; + return I; +}