www.pudn.com > airhockey.rar > player.cpp


#include "player.h" 
 
// Load() 
// desc: not used by CPlayer 
void CPlayer::Load() 
{} 
 
// Unload() 
// desc: not used by CPlayer 
void CPlayer::Unload() 
{} 
 
// Draw() 
// desc: draws the player at its current position 
void CPlayer::Draw() 
{ 
     glPushMatrix(); 
          glTranslatef(position.x, position.y, position.z); 
          glScalef(radius, radius, radius); 
          glColor3f(0.5, 0.0, 0.0); 
 
          // draw side ring of player 
          glBegin(GL_TRIANGLE_STRIP); 
          for (int i = 0; i < 16; i++) 
          {               
               glVertex3fv(&playerData[i+16][0]); // top     
               glVertex3fv(&playerData[i][0]);    // bottom 
          } 
          glVertex3fv(&playerData[16][0]); 
          glVertex3fv(&playerData[0][0]); 
          glEnd(); 
 
          glColor3f(0.5, 0.5, 0.5); 
          // draw top and bottom of player 
          glBegin(GL_TRIANGLE_FAN); 
               glVertex3f(0.0, playerData[0][1], 0.0); 
               for (i = 0; i < 16; i++) 
                    glVertex3fv(&playerData[i][0]); 
               glVertex3fv(&playerData[0][0]); 
          glEnd(); 
          glBegin(GL_TRIANGLE_FAN); 
               glVertex3f(0.0, playerData[31][1], 0.0); 
               for (i = 16; i < 32; i++) 
                    glVertex3fv(&playerData[i][0]); 
               glVertex3fv(&playerData[16][0]); 
          glEnd(); 
     glPopMatrix(); 
} 
 
// Animate() 
// desc: not used by CPlayer 
void CPlayer::Animate(scalar_t deltaTime) 
{} 
 
// Move() 
// desc: moves the player on the table based on the movements of the mouse 
//       performs collision detection with puck 
void CPlayer::Move(scalar_t deltaTime, int mouseX, int mouseY, CTable *table, CPuck *puck) 
{ 
     int xDiff, yDiff;        // distance mouse has moved in mouse coordinate units 
 
     // calculate distance mouse has moved 
     xDiff = (mouseX - oldMouseX)*4.0; 
     yDiff = (mouseY - oldMouseY)*4.0; 
 
     // save mouse position 
     oldMouseX = mouseX; 
     oldMouseY = mouseY; 
 
     // calculate velocity based on mouse movement 
     velocity = CVector(xDiff, 0.0, yDiff) * 10.0; 
 
     // calculate position 
     position = position + (velocity * deltaTime); 
 
     // collisions with table walls 
     if (position.x - radius < table->tableCorners[0][0]) 
          position.x = table->tableCorners[0][0] + radius; 
     if (position.x + radius > table->tableCorners[3][0]) 
          position.x = table->tableCorners[3][0] - radius; 
     if (position.z - radius < table->tableCorners[1][2]) 
          position.z = table->tableCorners[1][2] + radius; 
     if (position.z + radius > table->tableCorners[3][2]) 
          position.z = table->tableCorners[3][2] - radius; 
 
     // collision with puck 
     if ((puck->position - position).Length() <= (puck->size + radius)) 
     { 
          // stationary collision (simple) 
          if (velocity.Length() == 0) 
          { 
               puck->velocity = -puck->velocity; 
          } 
          else      // moving collision 
          { 
               // new puck velocity is equal to reflection of the cross product of 
               // the player velocity and the puck velocity plus the player velocity 
               puck->velocity = puck->velocity.Reflection(puck->velocity ^ velocity) + velocity; 
          } 
     } 
}