
-----------------------------------
lordroba
Sun Sep 25, 2005 11:47 am

Mouse.Where relative angle question
-----------------------------------
hey

i'm making a game where there is a spaceship in the middle of the screen and you rotate it by using the mouse.   

The spaceship will always stay in the middle, but as you move the mouse around the spaceship, the graphic will rotate depending on where the mouse is.  The problem is, I just can't figure out how to find the angle between the x,y cordinates of the mouse and the x,y coordinates of the spaceship.

Thanks in advance

-----------------------------------
Mazer
Sun Sep 25, 2005 12:31 pm


-----------------------------------
You'll need to use arctand and check a few cases to do this. I wrote a function for this some time ago, I just can't recall what happened to it. But hey, you wouldn't learn as well if I handed you the answer.

Note: I'm using the functions tand() and arctand() because I prefer to deal with degrees.

Let's start from the basics:
What information do you start off with?
- the position of the space ship (middle of the screen): midx, midy
- the position of the mouse: mousex, mousey

What do we (hopefully) know?
tand(angle) = slope
and therefore:
arctand(slope) = angle
since slope is dy/dx:
arctand((mousey - midy)/(mousex - midx)) = angle

And there you have it! But there are problems with this: most notable, if you're mouse is in the exact middle of the screen, you'll crash the program with your attempt to divide by zero. The other problem is that arctand will only return an angle from -90 < angle < 90 (notice the "