motorollin
Member
- Joined
- Jul 31, 2007
- Messages
- 163
The following code should make the enemy home in on the player:
CODE
void Seeker::move(int index)
{
//Find the angle required to move towards the player
int distx = playerx-x;
int disty = playery-y;
int angle = atan2( disty, distx) * 57.29578;
//Set the velocities
dx = seekerspeed * cos(angle);
dy = seekerspeed * sin(angle);
//Move
x+=dx;
y+=dy;
//Set collision rect
col_l = x;
col_r = col_l + SEEKER_WIDTH;
col_t = y;
col_b = col_t + SEEKER_HEIGHT;
}
However, it has a very strange effect. The enemy moves towards the player a certain distance then stops and just wobbles. Also if the player moves away from the enemy, the player seems to reverse its direction!
Quicktime Video showing what happens
CODE
void Seeker::move(int index)
{
//Find the angle required to move towards the player
int distx = playerx-x;
int disty = playery-y;
int angle = atan2( disty, distx) * 57.29578;
//Set the velocities
dx = seekerspeed * cos(angle);
dy = seekerspeed * sin(angle);
//Move
x+=dx;
y+=dy;
//Set collision rect
col_l = x;
col_r = col_l + SEEKER_WIDTH;
col_t = y;
col_b = col_t + SEEKER_HEIGHT;
}
However, it has a very strange effect. The enemy moves towards the player a certain distance then stops and just wobbles. Also if the player moves away from the enemy, the player seems to reverse its direction!
Quicktime Video showing what happens