I'm creating a game in c++ and OpenGL and want an enemy to move towards the player.
What is the best method of making game objects move towards other game objects, that works in both 2D and 3D game environments?
UPDATE:
wow thanks everyone for the quick replies!
strangely enough I managed to get this to work just as I posted it
although for some reason i have to multiply the x values by more to get them to move as fast as the y direction.
anyone have any ideas why? or if what I'm doing is wrong/bad
float playerX = player.getXPos();
float playerY = player.getYPos();
float enemyX = XPos-*xscroll;
float enemyY = YPos-*yscroll;
glPushMatrix();
glTranslatef(enemyX, enemyY, 0.0);
glColor3f(1.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(-40,40);
glVertex2f(-40,-40);
glVertex2f(40,-40);
glVertex2f(40,40);
glEnd();
glPopMatrix();
float xDistance = abs(playerX-enemyX);
float yDistance = abs(playerY-enemyY);
if((playerX - enemyX)*(playerX - enemyX)+(playerY - enemyY)*(playerY - enemyY) < 400*400){
float heading = asin(xDistance/yDistance);
if(playerY > enemyY){
YPos += timeFactor*(200*(sin((90+heading)*(PI/180.0f))));
}else{
YPos += -(timeFactor*(200*(sin((90+heading)*(PI/180.0f)))));
}
if(playerX > enemyX){
XPos += -(timeFactor*(10000*(cos((90+heading)*(PI/180.0f)))));
}else{
XPos += timeFactor*(10000*(cos((90+heading)*(PI/180.0f))));
}
}