Bullet Angles

Well, geometry has finally become of use in my life, too bad I rarely payed attention in that class ;D

So I have a character, lets call him bob. Bob is inside of a 100 x 100 window, the center of bobs body is at (50, 50), hence he is in the middle of the screen.

The mouse position can be put anywhere around bob in this window, and we want to calculate the angle at which the mouse is in relationship 0 - 360 degrees.

Just like a unit circle (yay calc!)

I have been using Math.atan, but it only gives angles -90 - 90, is there a way to calculate these angles in relationship to a horizontal-radian line pointing to the right?

This may expain better:

http://www4.ncsu.edu/~bdburns/MA111_UnitCircle001.jpg

Math.atan2

Rummaging around in my libraries I found;

public float getBearing(float x, float y, float x1, float y1)
    {
        float dx=x1-x;
        float dy=y1-y;
        double d=Math.atan2(dy,dx);
        if (d<0)
        {
            d=Math.PI*2+d;
        }
        return (float)d;
    }

which gives a bearing where east (1,0) is 0’

I first read Bullet Angels and thought: that would make a nice shooter name. :slight_smile:

I have the code to do this on my laptop. I’ll come back later and comment it out and post it for you.

Hi!
I have implemented the method that indicates the full polar coordinate (with angle in [0;2*PI]) of an input coordinate, which could be mouse-bob in your case.
Search for Coord2d.fullPolar() here: http://code.google.com/p/jzy3d/source/browse/branches/0.8.1/src/api/org/jzy3d/maths/Coord2d.java

I use it a lot and it works as expected.
Cheers,
Martin

FWIW there is a game called Bullet Witch: