Ok so I'm fairly new to C++ and i have a project to make a tank game with 2 players controlling tanks, I've made two boxes for the tanks and set up my controls as case events pointing to playerArray[0] and playerArray[1] separately. But the controls only rotate one of the boxes, and i can change which box they rotate but i cannot separately rotate each box with the numpad and arrow keys :
{
public:
playerInfo(); // the constructor
bool upPressed; // whether or not they are pressing their "up" key
bool downPressed; // whether or not they are pressing their "down" key
bool leftPressed; // and so on
bool rightPressed;
float rotation; // the rotation of their avatar
float speed; // player tank speed
float xPos; // position in world
float yPos;
float xscale; // Scale
};
// this class holds all of the information about a game state
class gameInfo
{
public:
gameInfo(); // the constructor
~gameInfo(); // the destructor
playerInfo** playerArray;
void updateGame(HDC deviceContext, gameInfo* currentGame)
{
// do game logic here
// this bit updates the rotation of each player if they are holding their key down...
for (int i = 0; i < NUM_PLAYERS; i++)
{
//if (currentGame->playerArray[i]->leftPressed) {currentGame->playerArray[i]->rotation -= ROTATION_SPEED;}
//if (currentGame->playerArray[i]->rightPressed) {currentGame->playerArray [i]->rotation += ROTATION_SPEED;}
}
// now draw the scene...
draw(deviceContext, currentGame);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // clear the screen and the depth buffer
glEnableClientState(GL_VERTEX_ARRAY); // we need enable vertices to draw polygons (obviously)
glEnableClientState(GL_COLOR_ARRAY); // we want to use colors too
glMatrixMode(GL_MODELVIEW); // we are going to affect how models are drawn
// so, for each player...
for (int i = 0; i < NUM_PLAYERS; i++)
{
// in this demo we always use the same piece of geometry (i.e. the triangle)...
glVertexPointer(3, GL_FLOAT, 0, myVertexArray); // choose the array of vertex positions to use, 3 dimensional (XYZ), floats, 0 gap between elements
glColorPointer(4, GL_FLOAT, 0, myColorArray); // choose the array of vertex colors to use, 4 dimensional (RGBA), floats, 0 gap between elements
glPushMatrix(); // save the current matrix state, in case anything important was done before we got here
glLoadIdentity(); // start with the identity matrix (i.e. no transformation)
glTranslatef(currentGame->playerArray[i]->xPos,currentGame->playerArray[i]->yPos,-10.0); // then move across and back
//then rotate to the current player's rotation around z axis
glRotatef(-currentGame->playerArray[i]->rotation, 0.0, 0.0, 1.0);
// then draw the 3 vertices from the array to make the generic triangle.
// because we are using matrices the same triangle can be rotated differently for each player!
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glPopMatrix(); // restore the old matrix state, leaving things as we found them!
}
glDisableClientState(GL_VERTEX_ARRAY); // turn off vertex rendering again, just in case
glDisableClientState(GL_COLOR_ARRAY); // turn off vertex colors again, just in case
SwapBuffers(deviceContext); // put our triangles on the screen!
}
////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
Left out some unnecessary? code here, here is the code for the keycommands and playerinfo which is further down.
///////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// as long as the player hasnt exited the app:
while( !needToQuit )
{
// we need to listen out for OS messages.
// if there is a windows message to process...
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
// if the message was a "quit" message...
if( msg.message == WM_QUIT )
{
needToQuit = true; // we want to quit asap
}
// otherwise, if it was a "key pressed" message...
else if (msg.message == WM_KEYDOWN)
{
switch (msg.wParam)
{
//Up
case VK_UP:
//Set player up movement later
currentGame->playerArray[0]->upPressed = true;
break;
//Down
case VK_DOWN:
//Set player up movement later
currentGame->playerArray[0]->downPressed = true;
break;
// if it was the right arrow key
case VK_RIGHT:
// this sets player 1's right movement in action later
currentGame->playerArray[0]->rightPressed = true;
break;
// if it was the left arrow key
case VK_LEFT:
// this sets player 1's left movement in action later
currentGame->playerArray[0]->leftPressed = true;
break;
//Up
case VK_NUMPAD8:
// sets player 2 up movement later
currentGame->playerArray[1]->upPressed = true;
break;
//Down
case VK_NUMPAD5:
// sets player 2 down movement later
currentGame->playerArray[1]->downPressed = true;
break;
// if it was the up arrow key
case VK_NUMPAD6:
// this sets player 2's right movement in action later
currentGame->playerArray[1]->rightPressed = true;
break;
// if it was the down arrow key
case VK_NUMPAD4:
// this sets player 2's left movement in action later
currentGame->playerArray[1]->leftPressed = true;
break;
default:
break;
}
}
// otherwise, if they let go of a key (you get the idea)
else if (msg.message == WM_KEYUP)
{
switch (msg.wParam)
{
// now update the gameplay
updateGame(myDeviceContext, currentGame);
}
// this sets up the default values that are common to all game instances
gameInfo::gameInfo()
{
// create an array of pointers to players...
playerArray = new playerInfo* [NUM_PLAYERS];
// for each player in the array, allocate memory for that player
for (int i = 0; i < NUM_PLAYERS; i++)
{
playerArray[i] = new playerInfo;
}
// adjust the xpos of each triangle so they dont overlap...
playerArray[0]->xPos = 0.0f;
playerArray[1]->xPos = 3.0f;
}
// this sets up the default values that are common to all players
playerInfo::playerInfo()
{
upPressed = false;
downPressed = false;
leftPressed = false;
rightPressed = false;
rotation = 30.0f;
speed = 0.0001f;
xPos = 0.0f;
yPos = 0.0f;
xscale = 0.5f;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
Somewhere in this code the playerArray{0} and playerArray[1] controls arent working as they should... any ideas? :(
Ah i've changed to glut and redone the whole thing, works now. I wasn't able to have 2 separate player control sets to control two separate Quads. Sorry about the confusion!