#19585 - Cearn - Thu Apr 22, 2004 1:40 pm
This is basic trigonometry.
If we use
(dx, dy) : the differences between the two objects in the x and y directions
dist : the distance between them dist= sqrt(dx^2 + dy^2)
theta : the angle you have in your picture
then
cos(theta)= dy/dist
sin(theta) = dx/dist
tan(theta) = sin(theta)/cos(theta) = dx/dy
These equations are a little different than the standard trig definitions because you chose the angle based on the y-axis instead of the x-axis. To get the actual angle, you need to use the inverses of these functions, arcsin, arccos and arctan. The C library <math.h> contains functions for these. You will have to be careful with minus signs, though, because the range of arccos, arcsin and arctan will only cover half a circle. You would have to check the signs of dx and dy for the full angle.
However, using these routines is bad idea on the GBA. As anyone here can tell you, the GBA doesn't have real floating point support so sqrt or any of the trigonometry functions are going to be dead slow. The same goes for division. You will want to put these in look-up tables instead.
Are you sure need the full angle by the way? If you just need a direction you can use the definitions for sin(theta) and cos(theta) without any need for the actual angle, which would save a good deal of calculation time.
There are also fast ways of calculating the distance that you need for this, but I can't recall it at this time. I'm sure one of the others can help you with that.
#19597 - Miked0801 - Thu Apr 22, 2004 5:37 pm
How exactly do you need the direction represented - as a vector I assume? If so, what are you attempting to do with that angle? Depending on how you are going to use it, you can probably get away without a ArcTan/Sqrt and perhaps without the Div too. :)
#19671 - kinski - Fri Apr 23, 2004 8:34 am
In the image above, at the intersection of the two lines is a kind of "shooting tower" (in french, we call it a DCA) that should rotate in the direction of the main sprite (and then shoot at it).
Finally, I found a solution that is not based on angles but on the 8 main lines that have the tower as intersection. I just check if my main sprite is on one of these eight lines. It works fine.
Anyway, thanks a lot to the both of you for you help.
-> Cearn : it's quite funny, I was printing the content of your site (TONC) the day before starting this post. Impressive work!