Jump to content
  • Advertisement
Sign in to follow this  
Salvinger

How to make 1 object point to another in 2d

This topic is 4823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!