Current Mouse-Image Relation Calculation Code (theta is a volatile double value being modified by separate threads)
1 2 3 4 5 6 7
| private void calculatePlayerRotation(int x, int y) { double dx = x - this.x; double dy = y - this.y; theta = atan2(dy, dx); theta = toDegrees(theta); theta+=180.0; } |
Wowowowow! Modifying theta like that is dangerous as hell, even if it is volatile! You overwrite theta on every call to calculatePlayerRotation, but the value may be overwritten by another thread in between the 3 manipulations to theta, meaning that 180 may be added to a completely different value if it was overwritten after atan2() but before theta+=180.0; You need to use a synchronized block (with a non-volatile double) or set theta with a single call so the calculation can't be modified in between the last 3 lines.
For the second solution:
1 2 3 4 5 6 7 8
| private void calculatePlayerRotation(int x, int y) { double dx = x - this.x; double dy = y - this.y; double th = atan2(dy, dx); th = toDegrees(th); th+=180.0; theta = th; } |
This would work, but there are other solutions too.
Sadly, I doubt that this is the problem with your inaccurate angle, as the theta bug was a race condition. The code you posted looks fine otherwise, so it's hard to determine where things go wrong. It's your code, you know how it works (well, hopefully

), so it's easier for you to debug it yourself rather than explaining everything. Add lots of System.out.println("Variable: " + variable); to your code to make sure that variables have the right values or even better, use a debugger so you can inspect all variables while it's running. That should be enough to figure out where things are going wrong. In a wild guess, I'd suspect you treat the angle as degrees sometimes and as radians sometimes...