How to make 1 object point to another in 2d

Recommended Posts

Salvinger    122
I am trying to make a 2d space game(if you can call it that) where you have to avoid being shot by a ship that follows you. My problem is that I have no clue how to go about making it so the ship that follows you, actually faces toward your ship. If someone can post here how to do it, or even give me a tip on how to do it(I would like that much better, it's no fun having things done completely for you).

Share on other sites
markr    1692
1. Compute the vector from the enemy to the player's ship

- To do this just subtract the enemy's position vector from the player's position vector

2. Work out the angle of that vector

- Your vector class probably has a method for doing this - if not, make one, it's dead easy. Just use atan2(y,x) and it'll give you the right result. atan2(y,x) is similar atan(y/x), but it gets the right answer for any angle, not just those between 0 and 180 - it takes into account the sign of y and x to do this.

3. Turn your enemy around until they reach the right direction - if they're facing the wrong way - you'd need a function like "normaliseAngle" which takes an angle, and makes it between -180 and 180 degrees.
- Specifically, just take the enemy's existing heading from its desired heading (i.e. the angle to the player), and determine whether this is positive or negative, and turn around accordingly.

Mark

Share on other sites
Guest Anonymous Poster
Quote:
 Original post by markr1. Compute the vector from the enemy to the player's ship- To do this just subtract the enemy's position vector from the player's position vector 2. Work out the angle of that vector- Your vector class probably has a method for doing this - if not, make one, it's dead easy. Just use atan2(y,x) and it'll give you the right result. atan2(y,x) is similar atan(y/x), but it gets the right answer for any angle, not just those between 0 and 180 - it takes into account the sign of y and x to do this.3. Turn your enemy around until they reach the right direction - if they're facing the wrong way - you'd need a function like "normaliseAngle" which takes an angle, and makes it between -180 and 180 degrees. - Specifically, just take the enemy's existing heading from its desired heading (i.e. the angle to the player), and determine whether this is positive or negative, and turn around accordingly.Mark

You can also only allow the persuing ship to change its heading a certain max number of degrees each 'game turn'. Thus the persuing ship slowly turns toward its target as it moves (and moves along the new headings direction).

Share on other sites
Salvinger    122
I am having trouble getting what you said to do to work. Obviously it's just a mistake I made so here is the code I have currently. It's crude, but I figure I'll clean it up later when I can get it to work.

int xDist = ship.getXPos() - lasers[currentlaser].getXPos();
int yDist = ship.getYPos() - lasers[currentlaser].getYPos();

Btw, can someone tell me what the code tags are?

Share on other sites
Guest Anonymous Poster

int deltaX = ship.x - laser.x;int deltaY = ship.y - laser.y;double angleToShoot = atan2(deltaY, deltaX); // angle between [-Pi, +Pi]int intAngle = (int) RadiansToDegrees(angleToShoot + M_PI);