I am making a FPS game, and I have a for loop where I place out "ground" objects.

(pseudo code)

for (int x = 0; x < 16; x++)
    for (int z = 0; z < 24; z++)
        // creating a game object
        actor_T* a = actor_constructor(
            (-x * 32) + ((16 * 32) / 2), // x
            0.0f, // y
            -z * 32,  // z

        // add rotation around the X-axis
        a->rx = 90.0f;

        // set the texture
        a->texture = get_texture("res/grass.png", GL_RGB)->renderable_texture;

        // add the object to the render list
        dynamic_list_append(((state_T*)s)->actors, a);

And just to be clear, the "actor" is just a struct that looks like this:

typedef struct ACTOR_STRUCT
    float x;
    float y;
    float z;
    float dx;
    float dy;
    float dz;
    float rx; // rotation
    float ry; // rotation
    float rz; // rotation
    float friction;
    int width;
    int height;
    int depth;
    unsigned int VBO;
    unsigned int EBO;
    unsigned int shader_program;
    unsigned int texture;
    int loaded;
    mat4 model;
    void (*tick)(void* self);
    void (*draw)(void* self);
    void (*load)(void* self);
} actor_T;

After they have been added to the render list, I have some code that actually rotates them using their xr rotation that I added.

vec4 qx, qy, qz;
mat4 mx, my, mz;

// creating a quaternion rotation for every axis I want to rotate
glm_quat(qx, to_radians(actor->rx), 1.0f, 0.0f, 0.0f);
glm_quat(qy, to_radians(actor->ry), 0.0f, 1.0f, 0.0f);
glm_quat(qz, to_radians(actor->rz), 0.0f, 0.0f, 1.0f);

// turning the quaternions into 4x4 matrices.
glm_quat_mat4(qx, mx);
glm_quat_mat4(qy, my);
glm_quat_mat4(qz, mz);

mat4 final;

// calculating the final 4x4 rotation matrice by combining them.
glm_mat4_mulN((mat4* []){&mx, &my, &mz}, 3, final);

// finally, I am translating to the objects position.
glm_translate(final, (vec3){ actor->x, actor->y, actor->z });

Now, the problem is that after I have applied the rotations, it seems to me that the X-axis has now become the Y-axis, because if I decrease the value of the X on these actors, they raise higher up in the air.

If I dont rotate them, they are behaving as I would expect.

What is the problem here? How do I solve it?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.