About the Euler 'ZXY' Rotation Matrix

44 Views Asked by At
#include <iostream>
#include <cmath>
#include "SDL.h"
using namespace std;

#define PI 3.141592

struct vertex {
    float x;
    float y;
    float z;
    float m = 1;
}typedef vertex;

struct matrix {
    float x1, x2, x3;
    float y1, y2, y3;
    float z1, z2, z3;
}typedef matrix;

vertex rotate(vertex v, matrix m) {
    return { m.x1 * v.x + m.x2 * v.y + m.x3 * v.z,
             m.y1 * v.x + m.y2 * v.y + m.y3 * v.z,
             m.z1 * v.x + m.z2 * v.y + m.z3 * v.z,
             1 };
}

void drawCube(SDL_Renderer* renderer, vertex points[], int edges[][2], int numEdges, matrix m) {
    for (int i = 0; i < numEdges; i++) {
        vertex p1 = rotate(points[edges[i][0]], m);
        vertex p2 = rotate(points[edges[i][1]], m);

        p1.x += 640; // 1280/2
        p1.y += 360; // 720/2
        p2.x += 640;
        p2.y += 360;

        SDL_RenderDrawLine(renderer, p1.x, p1.y, p2.x, p2.y);
    }
}

int main(int argc, char* argv[]) {
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Window* window = SDL_CreateWindow("title_", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_SHOWN);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);

    vertex points[] = {
    {-50, -50, -50}, // 0
    { 50, -50, -50}, // 1
    { 50,  50, -50}, // 2
    {-50,  50, -50}, // 3
    {-50, -50,  50}, // 4
    { 50, -50,  50}, // 5
    { 50,  50,  50}, // 6
    {-50,  50,  50}  // 7
    };

    int edges[][2] = {
        {0, 1}, {1, 2}, {2, 3}, {3, 0}, // Bottom face
        {4, 5}, {5, 6}, {6, 7}, {7, 4}, // Top face
        {0, 4}, {1, 5}, {2, 6}, {3, 7}  // Connecting edges
    };

    int yaw = 0.f;
    int pitch = 0.f;
    int roll = 0.f;

    bool running = true;
    SDL_Event event;
    while (running) {
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        SDL_RenderClear(renderer);

        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                running = false;
            }
            else if (event.type == SDL_KEYDOWN) {
                switch (event.key.keysym.scancode) {
                    //y
                case SDL_SCANCODE_RIGHT:
                    yaw+= 10;
                    break;
                case SDL_SCANCODE_LEFT:
                    yaw-= 10;
                    break;
                    //x
                case SDL_SCANCODE_UP:
                    pitch+= 10;
                    break;
                case SDL_SCANCODE_DOWN:
                    pitch-= 10;
                    break;
                    //z
                case SDL_SCANCODE_A:
                    roll+= 10;
                    break;
                case SDL_SCANCODE_D:
                    roll-= 10;
                    break;
                }
            }
        }

        yaw %= 180;
        pitch %= 180;
        roll %= 180;

        float a = (float)yaw * PI / 180;
        float b = (float)pitch * PI / 180;
        float r = (float)roll * PI / 180;

        float sa = sin(a), ca = cos(a);
        float sb = sin(b), cb = cos(b);
        float sr = sin(r), cr = cos(r);

        matrix m = { 
            ca * cr + sa * sb * sr, -ca * sr + sa * sb * cr,  sa * sb,

            cb * sr,  cb * cr, -sb,

            -sa * cr + ca * sb * sr,  sa * sr + ca * sb * cr, ca * cb };

        matrix transpose_m = {
            ca * cr + sa * sb * sr, cb * sr, -sa * cr + ca * sb * sr,
            -ca * sr + sa * sb * cr, cb * cr, sa * sr + ca * sb * cr,
            sa * sb, -sb, ca * cb
        };

        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 
        drawCube(renderer, points, edges, 12, transpose_m);

        SDL_RenderPresent(renderer);
    }
    SDL_Quit();
    return 0;
}

This code can rotate the cube on X, Y, and Z axes. In the code, the order of calculation of the Euler angle is Roll, Pitch, and Yaw. However, the cube is distorted in the result of the rotation matrix, 'matrix m;'. And in order to achieve the expected result, the operation must be performed with a transpose matrix of m. Is there a problem with the formula?

I would like to know the problem of matrix m.

0

There are 0 best solutions below