# Acos() not giving angles between PI/2-3PI/2

I am making a tank game, using the following to determine the angle from the tank to the cursor
`rotate(atan(((mouseY-height/2)*scalar)/((mouseX-width/2)*scalar)));`
then drawing my cannon within it (for shortening purpouses its just a rect)
`rect(0,-5,30,10);`
Then undoing the rotation with the negative of the previously mentioned rotate

Problem is, whenever the angle is from pi/2 to 3pi/2 (quadrants 2 and 3), it gives me the inverse.
To better show this I have made a pointer from where the tank is, to where the cursor is with a line:
`ellipse((mouseX-width/2)*(scalar),(mouseY-height/2)*(scalar), 5,5);`
`line(0,0,(mouseX-width/2)*(scalar),(mouseY-height/2)*(scalar));`
When the line is intersecting the rect, it is working correctly

here is a working copypasta that shows my problem

``````void setup(){
fullScreen();
}

void draw(){
background(255);
translate(width/2, height/2);
scale(height/540.0);

ellipse(0,0,20,20);
rotate(atan(((mouseY-height/2)*(540.0/height))/((mouseX-width/2)*(540.0/height))));
rect(0,-5,30,10);
rotate(-atan(((mouseY-height/2)*(540.0/height))/((mouseX-width/2)*(540.0/height))));
ellipse((mouseX-width/2)*(540.0/height),(mouseY-height/2)*(540.0/height), 5,5);
line(0,0,(mouseX-width/2)*(540.0/height),(mouseY-height/2)*(540.0/height));
}
``````
1 Like

I suggest you try using `atan2` rather than `atan` to get the angle for rotation. The code below seems to work

``````void setup(){
fullScreen();
}

void draw(){
background(255);
translate(width/2, height/2);
scale(height/540.0);

ellipse(0,0,20,20);
rotate(atan2(((mouseY-height/2)*(540.0/height)), ((mouseX-width/2)*(540.0/height))));
rect(0,-5,30,10);
rotate(-atan2(((mouseY-height/2)*(540.0/height)), ((mouseX-width/2)*(540.0/height))));
ellipse((mouseX-width/2)*(540.0/height),(mouseY-height/2)*(540.0/height), 5,5);
line(0,0,(mouseX-width/2)*(540.0/height),(mouseY-height/2)*(540.0/height));
}
``````
4 Likes

In computing we donâ€™t use tan to calculate the angle because it can produce illegal resultsâ€¦ Consider your line
`rotate(atan(((mouseY-height/2)*(540.0/height))/((mouseX-width/2)*(540.0/height))));`

If mouseX == width/2 then we have
`rotate(atan(((mouseY-height/2)*(540.0/height))/ 0 ));`

i.e. division by zero.

this is not a problem with `atan2` because we are not doing the division, we have
`rotate(atan2(((mouseY-height/2)*(540.0/height)) , 0 ));`

4 Likes