Sign in to follow this  
Salvinger

How to make 1 object point to another in 2d

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by markr
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





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 this post


Link to post
Share on other sites
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();
int angle = (int)atan2(sin((double)yDist * RADIAN), sin((double)xDist * RADIAN)) / RADIAN;

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
How about:


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);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this