Crosswar

04-23-2015, 04:27 PM

Hello, I have recently started working on a project where I trying to simulate a rocket taking off, the problem I am currently experiencing is regarding the rendering of the object, I translate it on it's "local axis" and it moves fine however when I rotate it, it only displays the 1 degree rotation based on

glRotatef(deg, 1, 0, 0);

where if the degree value would be 1 it would rotate on the said axis by 1 and only render the first rotation as in if I were to press the left arrow it rotates it by 1 degree, it shows the rotation and any other rotations done afterwards are not rendered however are still applied.

If I were to press the left key lets say 45 times it and the degree would still be 1 it will render the first rotation of 1 degree then rotate the coord system by another 44 degrees without actually showing the rotation of the object.

Moving coordinate system and allowing translation on said system.

Here is the code I am using for the rotation and translation and how I am calling it in the main function.

Rocket.cpp

#include "Rocket.h"

Rocket::Rocket(float x, float y, float z)

{

Transform[0] = 1.0f;

Transform[5] = 1.0f;

Transform[10] = -1.0f;

Transform[15] = 1.0f;

Transform[12] = x; Transform[13] = y; Transform[14] = z;

Right=&Transform[0];

Up=&Transform[4];

Forward=&Transform[8];

Position=&Transform[12];

}

Rocket::~Rocket(){}

void Rocket::locMove(float x, float y, float z, float distance)

{

float dx=x*Transform[0] + y*Transform[4] + z*Transform[8];

float dy=x*Transform[1] + y*Transform[5] + z*Transform[9];

float dz=x*Transform[2] + y*Transform[6] + z*Transform[10];

Transform[12] += dx * distance;

Transform[13] += dy * distance;

Transform[14] += dz * distance;

}

void Rocket::locRot(float deg, float x, float y, float z)

{

glMatrixMode(GL_MODELVIEW);

glPushMatrix();

glLoadMatrixf(Transform);

glRotatef(deg, x,y,z);

glGetFloatv(GL_MODELVIEW_MATRIX, Transform);

glPopMatrix();

degG = deg;

xG = x;

yG = y;

zG = z;

}

void Rocket::drawRocket()

{

glPushMatrix();

glColor3ub(229,228,226);

glTranslatef(Transform[12], Transform[13], Transform[14]);

glRotatef(degG, xG,yG,zG);

glutSolidCube(15);

glPopMatrix();

}

main.cpp

Rocket rock(rockX, rockY, rockZ);

draw3d()

{

rock.drawRocket();

}

The main class is quite large and the only bits of code related is the instancing of a new rocket and calling the draw rocket function, alongside various rock.locRotate and rock.locMove calls within the keypress function.

My best guess as to how this could be fixed would be an update function for the rocket however I do not know what would be inside said function.

Any help would be appreciated as this has been an issue of mine for a few weeks now, getting into this stage just a few days ago.

Thanks again.

glRotatef(deg, 1, 0, 0);

where if the degree value would be 1 it would rotate on the said axis by 1 and only render the first rotation as in if I were to press the left arrow it rotates it by 1 degree, it shows the rotation and any other rotations done afterwards are not rendered however are still applied.

If I were to press the left key lets say 45 times it and the degree would still be 1 it will render the first rotation of 1 degree then rotate the coord system by another 44 degrees without actually showing the rotation of the object.

Moving coordinate system and allowing translation on said system.

Here is the code I am using for the rotation and translation and how I am calling it in the main function.

Rocket.cpp

#include "Rocket.h"

Rocket::Rocket(float x, float y, float z)

{

Transform[0] = 1.0f;

Transform[5] = 1.0f;

Transform[10] = -1.0f;

Transform[15] = 1.0f;

Transform[12] = x; Transform[13] = y; Transform[14] = z;

Right=&Transform[0];

Up=&Transform[4];

Forward=&Transform[8];

Position=&Transform[12];

}

Rocket::~Rocket(){}

void Rocket::locMove(float x, float y, float z, float distance)

{

float dx=x*Transform[0] + y*Transform[4] + z*Transform[8];

float dy=x*Transform[1] + y*Transform[5] + z*Transform[9];

float dz=x*Transform[2] + y*Transform[6] + z*Transform[10];

Transform[12] += dx * distance;

Transform[13] += dy * distance;

Transform[14] += dz * distance;

}

void Rocket::locRot(float deg, float x, float y, float z)

{

glMatrixMode(GL_MODELVIEW);

glPushMatrix();

glLoadMatrixf(Transform);

glRotatef(deg, x,y,z);

glGetFloatv(GL_MODELVIEW_MATRIX, Transform);

glPopMatrix();

degG = deg;

xG = x;

yG = y;

zG = z;

}

void Rocket::drawRocket()

{

glPushMatrix();

glColor3ub(229,228,226);

glTranslatef(Transform[12], Transform[13], Transform[14]);

glRotatef(degG, xG,yG,zG);

glutSolidCube(15);

glPopMatrix();

}

main.cpp

Rocket rock(rockX, rockY, rockZ);

draw3d()

{

rock.drawRocket();

}

The main class is quite large and the only bits of code related is the instancing of a new rocket and calling the draw rocket function, alongside various rock.locRotate and rock.locMove calls within the keypress function.

My best guess as to how this could be fixed would be an update function for the rocket however I do not know what would be inside said function.

Any help would be appreciated as this has been an issue of mine for a few weeks now, getting into this stage just a few days ago.

Thanks again.