Java game development - viewing and shooting at mouse coordinates

I'm going to make a game where you run with the character seen from above. But the problem is how to make the character constantly look at the mouse? And how can I calculate the angle so that I can shoot in the direction of the mouse (where I am looking now). I think you need to play with trigonometry and the like to get angles, but I really don't know how to do this.

I am pretty good at math, so I could figure it out if you guys help me.

And sorry for my bad English, I'm Swedish :)

Thanks Alexandberg

+5
source share
2 answers
double angle = Math.atan2(yMouse-yChar, xMouse-xChar);
+1
source

, trig

Point pl = getCharacterLox();
Point mouse = getMouseLoc();
double cos = (mouse.getX()-pl.getX());
double sin = (mouse.getY()-pl.getY());
cos/=Math.hypot(cos,sin);//normalize 
double angle = Math.copySign(Math.acos(cos),sin);

, : acos 0 PI, sin , , sin 0, +0,0, 0 PI

+1

All Articles