Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find closest position of asteroids c++

Tags:

c++

visual-c++

enter image description here

In the above image i would like to know how to find the smallest possible way to get to the asteroid. the ship can wrap around so the closest way is going through the top corner instead of turning around and going back. I am not looking for code, just the pseudo code of how to get to it.

Any help is appreciated

The game asteroid is played on the surface of a torus.

like image 848
Alan Fletcher Avatar asked Dec 09 '22 18:12

Alan Fletcher


1 Answers

Well, since you can wrap around any edge of the screen, there are always 4 straight lines between the asteroid and the ship (up and left, up and right, down and left, and down and right). I would just calculate the length of each and take the smallest result.

int dx1 = abs(ship_x - asteroid_x);
int dx2 = screen_width - dx1;

int dy1 = abs(ship_y - asertoid_y);
int dy2 = screen_height - dy1;

// Now calculate the psuedo-distances as Pete suggests:
int psuedo1 = (dx1 * dx1) + (dy1 * dy1);
int psuedo2 = (dx2 * dx2) + (dy1 * dy1);
int psuedo3 = (dx1 * dx1) + (dy2 * dy2);
int psuedo4 = (dx2 * dx2) + (dy2 * dy2);

This shows how to calculate the various distances involved. There is a little complication around mapping each one to the appropriate direction.

like image 191
Peter Ruderman Avatar answered Dec 11 '22 11:12

Peter Ruderman