Question about a game [Archive] - C Board

PDA

View Full Version : Question about a game


TheGr8one
09-15-2001, 06:16 PM
Hello,
Im trying to make a top view space ship game in OpenGL that when you press left and right the ship rotates and when you press up the ship goes in the direction that it is pointing. The problem im have is figuring out a way to rotate the ship but also figure out which direction the ship should go when the player presses up to go where its pointing. I was going to just use glRotatef but that just rotates the ship and doesnt keep track of where the ship is pointing. Anyone know a way to do this?

Thanks
Chris

no-one
09-15-2001, 10:44 PM
im not sure this will work but it should.
I asume a 2D space and rotation is preformed on the y axis.

rotate the vector and then translate it like the following
NOTE: this permanently rotates the vector.


#include <math.h>
#include <string.h>
// rotation matrix
roty [4][4];
// translation matrix
tran [4][4];

// this will rotate vector and replace the values in vector with the new rotated ones

void rotate(float vector[4], float angle)
{
floar rotvect[4] = { 0.0,0.0,0.0,0.0 };

memset(roty[0],0,4);
memset(roty[1],0,4);
memset(roty[2],0,4);
memset(roty[3],0,4);


// setup the matrix for y rotation
roty[0][0] = cos(angle);
roty[0][2] = sin(angle);
roty[1][1] = 1;
roty[2][0] = -sin(angle);
roty[2][1] = cos(angle);
roty[3][3] = 1;


// rotate the vector
rotvect[0] = (vector[0] * roty[0][0]) + (vector[1] * roty[0][1]) + (vector[2] * roty[0][2]) + (vector[3] * roty[0][3]);
rotvect[1] = (vector[0] * roty[1][0]) + (vector[1] * roty[1][1]) + (vector[2] * roty[1][2]) + (vector[3] * roty[1][3]);
rotvect[2] = (vector[0] * roty[2][0]) + (vector[1] * roty[2][1]) + (vector[2] * roty[2][2]) + (vector[3] * roty[2][3]);
rotvect[3] = (vector[0] * roty[3][0]) + (vector[1] * roty[3][1]) + (vector[2] * roty[3][2]) + (vector[3] * roty[3][3]);

memcpy(vector,rotvect,4);

}

void translate(float vector[4],float dx,float dy,float dz)
{

floar tvect[4] = { 0.0,0.0,0.0,0.0 };


memset(tran[0],0,4);
memset(tran[1],0,4);
memset(tran[2],0,4);
memset(tran[3],0,4);

tran[0][0] = 1;
tran[0][3] = dx;
tran[1][1] = 1;
tran[1][3] = dy;
tran[2][2] = 1;
tran[2][3] = dz;
tran[3][3] = 1;

// translate the vector
tvect[0] = (vector[0] * tran[0][0]) + (vector[1] * tran[0][1]) + (vector[2] * tran[0][2]) + (vector[3] * tran[0][3]);
tvect[1] = (vector[0] * tran[1][0]) + (vector[1] * tran[1][1]) + (vector[2] * tran[1][2]) + (vector[3] * tran[1][3]);
tvect[2] = (vector[0] * tran[2][0]) + (vector[1] * tran[2][1]) + (vector[2] * tran[2][2]) + (vector[3] * tran[2][3]);
tvect[3] = (vector[0] * tran[3][0]) + (vector[1] * tran[3][1]) + (vector[2] * tran[3][2]) + (vector[3] * tran[3][3]);

memcpy(vector,tvect,4);

}

draw()
{
float vert[4] = { 1.0,0.0,2.0,1.0 };
// rotate vert 60 degrees
rotate(vert,60);
// translate it .1 x and .2 unit z unit
translate(vert,.1,0.0,.2);
// draw it.
}