diff --git a/.gitignore b/.gitignore index c6439d6..72371a1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ cmake-build-debug/ cmake-build-debug-mingw/ -.idea/ +.idea/ \ No newline at end of file diff --git a/.idea - Kopie/.gitignore b/.idea - Kopie/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea - Kopie/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea - Kopie/.name b/.idea - Kopie/.name deleted file mode 100644 index ed8527b..0000000 --- a/.idea - Kopie/.name +++ /dev/null @@ -1 +0,0 @@ -Prog3B \ No newline at end of file diff --git a/.idea - Kopie/Programmieren 3B.iml b/.idea - Kopie/Programmieren 3B.iml deleted file mode 100644 index f08604b..0000000 --- a/.idea - Kopie/Programmieren 3B.iml +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/.idea - Kopie/Stone_Development.iml b/.idea - Kopie/Stone_Development.iml deleted file mode 100644 index f08604b..0000000 --- a/.idea - Kopie/Stone_Development.iml +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/.idea - Kopie/editor.xml b/.idea - Kopie/editor.xml deleted file mode 100644 index b0d69ef..0000000 --- a/.idea - Kopie/editor.xml +++ /dev/null @@ -1,483 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea - Kopie/misc.xml b/.idea - Kopie/misc.xml deleted file mode 100644 index 0b76fe5..0000000 --- a/.idea - Kopie/misc.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea - Kopie/modules.xml b/.idea - Kopie/modules.xml deleted file mode 100644 index 3795d21..0000000 --- a/.idea - Kopie/modules.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea - Kopie/vcs.xml b/.idea - Kopie/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea - Kopie/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea - Kopie/workspace.xml b/.idea - Kopie/workspace.xml deleted file mode 100644 index 373a5cd..0000000 --- a/.idea - Kopie/workspace.xml +++ /dev/null @@ -1,180 +0,0 @@ - - - - - - - { - "useNewFormat": true -} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - { - "customColor": "", - "associatedIndex": 0 -} - - - - { - "keyToString": { - "CMake Application.Prog3B.executor": "Run", - "CMake Application.Prog3B_tests.executor": "Run", - "RunOnceActivity.RadMigrateCodeStyle": "true", - "RunOnceActivity.ShowReadmeOnStart": "true", - "RunOnceActivity.cidr.known.project.marker": "true", - "RunOnceActivity.git.unshallow": "true", - "RunOnceActivity.readMode.enableVisualFormatting": "true", - "RunOnceActivity.west.config.association.type.startup.service": "true", - "cf.first.check.clang-format": "false", - "cidr.known.project.marker": "true", - "git-widget-placeholder": "main", - "last_opened_file_path": "C:/Users/marce/Desktop/Prog Praktikum C++/Vorlesung/Lambda", - "node.js.detected.package.eslint": "true", - "node.js.detected.package.tslint": "true", - "node.js.selected.package.eslint": "(autodetect)", - "node.js.selected.package.tslint": "(autodetect)", - "nodejs_package_manager_path": "npm", - "settings.editor.selected.configurable": "CMakeSettings", - "vue.rearranger.settings.migration": "true" - } -} - - - - - - - - - - - - - - - - - - - - - - - 1762176441912 - - - - - - - - - - - - - \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index fa4528c..5e31108 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ endif() set(SRC_FILES ${CMAKE_CURRENT_LIST_DIR}/main.cpp ${CMAKE_CURRENT_LIST_DIR}/gamecube.cpp + ${CMAKE_CURRENT_LIST_DIR}/gamematrix.cpp ) set(INCLUDE_DIRS @@ -50,7 +51,6 @@ target_include_directories(${EXECUTABLE_NAME} PRIVATE ${INCLUDE_DIRS}) # === Bibliotheken verlinken === target_link_libraries(${EXECUTABLE_NAME} PRIVATE - ${CMAKE_CURRENT_LIST_DIR}/${OS_DIR}/libgamematrix.a ${CMAKE_CURRENT_LIST_DIR}/${OS_DIR}/libraylib.a ) diff --git a/gamecube.cpp b/gamecube.cpp index 38573ed..2984581 100644 --- a/gamecube.cpp +++ b/gamecube.cpp @@ -44,11 +44,11 @@ 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'); + auto matrix_a = Matrix3D::gameMatrix::translate({ position.x, position.y, position.z}); + auto matrix_b = Matrix3D::gameMatrix::rot3D(rotation, 'y'); // Matrizen multiplizieren (Translation * Rotation) - auto model = gameMatrix::matmul(matrix_a, matrix_b); + auto model = Matrix3D::gameMatrix::matmul(matrix_a, matrix_b); // transform for raylib matrix float f[16]; diff --git a/gamematrix.cpp b/gamematrix.cpp new file mode 100644 index 0000000..ba1ba4a --- /dev/null +++ b/gamematrix.cpp @@ -0,0 +1,87 @@ +#include "gamematrix.h" + + +using namespace Matrix3D; + +/// +/// @param matrix1 - First matrix +/// @param matrix2 - Second matrix +/// @return Returns the multiplication of matrix 1 and matrix 2. +std::array,4> gameMatrix::matmul(const std::array,4>& matrix1, + const std::array,4>& matrix2) { + + std::array,4> matrix3 = {}; + if (matrix1[0].size() != matrix2.size()) { + throw std::invalid_argument("Number of columns of matrix 1 must be equal to number of rows of matrix 2"); + }; + for (int i = 0; i < matrix1.size(); i++) { + std::array temp_array = {}; + for (int j = 0; j < matrix2[i].size(); j++) { + double temp = 0; + for (int k = 0; k < matrix2.size(); k++) { + temp += matrix1[i][k] * matrix2[k][j]; + } + temp_array[j] = temp; + } + matrix3[i] = temp_array; + } + return matrix3; +} + +/// +/// @param angle_deg Rotation angle +/// @param axis Rotation axis (x, y, z) +/// @return Returns a rotation matrix based on a given angle and axis. +std::array,4> gameMatrix::rot3D(double angle_deg, char axis) { + if (axis != 'x' && axis != 'y' && axis != 'z') { + throw std::invalid_argument("Invalid axis"); + }; + const auto angle_rad = angle_deg * M_PI / 180; + std::array,4> rotationMatrix = {}; + switch (axis) { + case 'x': { + std::array,4> rMx = {{ + {1, 0, 0, 0}, + {0, cos(angle_rad), -sin(angle_rad), 0}, + {0, sin(angle_rad), cos(angle_rad), 0}, + {0, 0, 0, 1} + }}; + rotationMatrix = rMx; + } + break; + case 'y': { + std::array,4> rMy = {{ + {cos(angle_rad), 0, sin(angle_rad), 0}, + {0, 1, 0, 0}, + {-sin(angle_rad), 0, cos(angle_rad), 0}, + {0, 0, 0, 1} + }}; + rotationMatrix = rMy; + } + break; + + case 'z': { + std::array,4> rMz = {{ + {cos(angle_rad), -sin(angle_rad), 0, 0}, + {sin(angle_rad), cos(angle_rad), 0, 0}, + {0, 0, 1, 0}, + {0, 0, 0, 1} + }}; + rotationMatrix = rMz; + } + break; + } + return rotationMatrix; +}; + + +/// +/// @param pos 3 point vector (x, y, z) +/// @return Returns a 4x4 translation matrix base on a given vector. +std::array,4> gameMatrix::translate(const std::array& pos) { + std::array,4> transMatrix = {{{1, 0, 0, pos[0]}, {0, 1, 0, pos[1]}, {0, 0, 1, pos[2]}, {0, 0, 0, 1}}}; + return transMatrix; +}; + + + diff --git a/gamematrix.h b/gamematrix.h index 7a644d6..db92914 100644 --- a/gamematrix.h +++ b/gamematrix.h @@ -4,16 +4,23 @@ #include #include -class gameMatrix -{ -public: - // Matrix Multiplikation - static std::array,4> matmul(const std::array,4>& A, - const std::array,4>& B); +namespace Matrix3D { + class gameMatrix + { + public: + // Matrix Multiplikation + static std::array,4> matmul(const std::array,4>& matrix1, + const std::array,4>& matrix2); - // Rotationsmatrix um Achse x/y/z - static std::array,4> rot3D(double angle_deg, char axis); + // Rotationsmatrix um Achse x/y/z + static std::array,4> rot3D(double angle_deg, char axis); - // Verschiebung - static std::array,4> translate(const std::array& pos); -}; + // Verschiebung + static std::array,4> translate(const std::array& pos); + + private: + //TODO: Was für eine Rolle spielen Vec3 und Mat4??? + struct Vec3 {double x,y,z;}; + std::array,4> Mat4 {}; + }; +} \ No newline at end of file