diff --git a/includes/gamecube.h b/includes/gamecube.h index bbe0028..235cb4e 100644 --- a/includes/gamecube.h +++ b/includes/gamecube.h @@ -2,6 +2,9 @@ #include "gamematrix.h" #include "raylib.h" #include +#include + +using Mat4 = std::array, 4>; struct Vec3 { diff --git a/includes/gamematrix.h b/includes/gamematrix.h index 7a644d6..6e490d6 100644 --- a/includes/gamematrix.h +++ b/includes/gamematrix.h @@ -3,6 +3,7 @@ #include #include #include +#include "gamecube.h" class gameMatrix { diff --git a/src/gamecube.cpp b/src/gamecube.cpp index e7ed188..2a9b7ff 100644 --- a/src/gamecube.cpp +++ b/src/gamecube.cpp @@ -1,70 +1,70 @@ -#include "gamecube.h" + #include "gamecube.h" -gamecube::gamecube(const Vec3 &pos, Color col) - : position(pos), color(col) {} + gamecube::gamecube(const Vec3 &pos, Color col) + : position(pos), color(col) {} -void gamecube::Update(float flipSpeed) -{ - //Tom: Added vars for clarity; replaced old 180.0f, 0.0f - const float MaxRotationAngle = 180.0f; - const float NoRotationAngle = 0.0f; - - if (flippingForward) + void gamecube::Update(float flipSpeed) { - rotation += flipSpeed; - if (rotation >= MaxRotationAngle) + //Tom: Added vars for clarity; replaced old 180.0f, 0.0f + const float MaxRotationAngle = 180.0f; + const float NoRotationAngle = 0.0f; + + if (flippingForward) { - rotation = MaxRotationAngle; - flippingForward = false; - flipped = true; + rotation += flipSpeed; + if (rotation >= MaxRotationAngle) + { + rotation = MaxRotationAngle; + flippingForward = false; + flipped = true; + } + } + else if (flippingBackward) + { + rotation -= flipSpeed; + if (rotation <= NoRotationAngle) + { + rotation = NoRotationAngle; + flippingBackward = false; + flipped = false; + } } } - else if (flippingBackward) + + void gamecube::FlipForward() { flippingForward = true; } + void gamecube::FlipBackward() { flippingBackward = true; } + + bool gamecube::IsFlipped() const { return flipped; } + bool gamecube::IsMatched() const { return matched; } + void gamecube::SetMatched(bool m) { matched = m; } + + void gamecube::Draw() const { - rotation -= flipSpeed; - if (rotation <= NoRotationAngle) - { - rotation = NoRotationAngle; - flippingBackward = false; - flipped = false; - } + rlPushMatrix(); + + // Matrizen für Rotation und Translation erzeugen + auto matrix_a = gameMatrix::translate({ position.x, position.y, position.z}); + auto matrix_b = gameMatrix::rot3D(rotation, 'y'); + + // Matrizen multiplizieren (Translation * Rotation) + auto model = gameMatrix::matmul(matrix_a, matrix_b); + + // transform for raylib matrix + float f[16]; + for (int i = 0; i < 4; i++) + for (int j = 0; j < 4; j++) + f[j * 4 + i] = model[i][j]; + rlMultMatrixf(f); + + if (rotation < 90.0f) + DrawCube({0,0,0}, 1,1,1, GRAY); + else + DrawCube({0,0,0}, 1,1,1, color); + + DrawCubeWires({0,0,0}, 1,1,1, BLACK); + + rlPopMatrix(); } -} -void gamecube::FlipForward() { flippingForward = true; } -void gamecube::FlipBackward() { flippingBackward = true; } - -bool gamecube::IsFlipped() const { return flipped; } -bool gamecube::IsMatched() const { return matched; } -void gamecube::SetMatched(bool m) { matched = m; } - -void gamecube::Draw() const -{ - rlPushMatrix(); - - // Matrizen für Rotation und Translation erzeugen - auto matrix_a = gameMatrix::translate({ position.x, position.y, position.z}); - auto matrix_b = gameMatrix::rot3D(rotation, 'y'); - - // Matrizen multiplizieren (Translation * Rotation) - auto model = gameMatrix::matmul(matrix_a, matrix_b); - - // transform for raylib matrix - float f[16]; - for (int i = 0; i < 4; i++) - for (int j = 0; j < 4; j++) - f[j * 4 + i] = model[i][j]; - rlMultMatrixf(f); - - if (rotation < 90.0f) - DrawCube({0,0,0}, 1,1,1, GRAY); - else - DrawCube({0,0,0}, 1,1,1, color); - - DrawCubeWires({0,0,0}, 1,1,1, BLACK); - - rlPopMatrix(); -} - -Vec3 gamecube::GetPosition() const { return position; } -float gamecube::GetRotationY() const { return rotation; } \ No newline at end of file + Vec3 gamecube::GetPosition() const { return position; } + float gamecube::GetRotationY() const { return rotation; } \ No newline at end of file diff --git a/src/gamematrix.cpp b/src/gamematrix.cpp index 5fccbe3..b2bfcf1 100644 --- a/src/gamematrix.cpp +++ b/src/gamematrix.cpp @@ -1,76 +1,75 @@ #include "gamematrix.h" #include -namespace Matrix3D { +// Entfernt: namespace Matrix3D { ... } <-- GEÄNDERT - Mat4 identity() { - Mat4 m{}; - for(int i = 0; i < 4; i++) { - for(int j = 0; j < 4; j++) { - m[i][j] = (i == j) ? 1.0 : 0.0; - } +// Implementierungen jetzt als Klassenmethoden von gameMatrix <-- GEÄNDERT + +// Optional: identity() als Hilfsmethode hinzugefügt +static std::array,4> identity() { // <-- NEU + std::array,4> m{}; + for(int i = 0; i < 4; i++) { + for(int j = 0; j < 4; j++) { + m[i][j] = (i == j) ? 1.0 : 0.0; } - return m; } + return m; +} - Mat4 matmul(const Mat4& A, const Mat4& B) { - Mat4 R{}; - for(int i = 0; i < 4; i++) { - for(int j = 0; j < 4; j++) { - double sum = 0.0; - for(int k = 0; k < 4; k++) { - sum += A[i][k] * B[k][j]; - } - R[i][j] = sum; +// -------------------- matmul -------------------- +std::array,4> gameMatrix::matmul( // <-- GEÄNDERT + const std::array,4>& A, + const std::array,4>& B) +{ + std::array,4> R{}; + for(int i = 0; i < 4; i++) { + for(int j = 0; j < 4; j++) { + double sum = 0.0; + for(int k = 0; k < 4; k++) { + sum += A[i][k] * B[k][j]; } + R[i][j] = sum; } - return R; } + return R; +} - Mat4 translate(const Vec3& pos) { - Mat4 t = identity(); - t[0][3] = pos.x; - t[1][3] = pos.y; - t[2][3] = pos.z; - return t; - } +// -------------------- translate -------------------- +std::array,4> gameMatrix::translate( // <-- GEÄNDERT + const std::array& pos) +{ + auto t = identity(); // <-- NEU: nutzt die Hilfsmethode + t[0][3] = pos[0]; + t[1][3] = pos[1]; + t[2][3] = pos[2]; + return t; +} - Mat4 rot3D(double angle_deg, char axis) { - Mat4 r = identity(); +// -------------------- rot3D -------------------- +std::array,4> gameMatrix::rot3D( // <-- GEÄNDERT + double angle_deg, char axis) +{ + auto r = identity(); // <-- NEU: nutzt die Hilfsmethode - double rad = angle_deg * M_PI / 180.0; - double c = std::cos(rad); - double s = std::sin(rad); + double rad = angle_deg * M_PI / 180.0; + double c = std::cos(rad); + double s = std::sin(rad); - switch(axis) { - case 'x': case 'X': - r[1][1] = c; r[1][2] = -s; + 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; + 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; + case 'z': case 'Z': + r[0][0] = c; r[0][1] = -s; r[1][0] = s; r[1][1] = c; break; - } - return r; } - - Vec3 operator*(const Mat4& M, const Vec3& v) { - Vec3 out; - out.x = M[0][0]*v.x + M[0][1]*v.y + M[0][2]*v.z + M[0][3]; - out.y = M[1][0]*v.x + M[1][1]*v.y + M[1][2]*v.z + M[1][3]; - out.z = M[2][0]*v.x + M[2][1]*v.y + M[2][2]*v.z + M[2][3]; - return out; - } - - Mat4 operator*(const Mat4& A, const Mat4& B) { - return matmul(A, B); - } - + return r; } \ No newline at end of file