Shooting at the mouse

I am making a top down shooter. The player needs to shoot where the mouse is. This is what I got, but it isn’t working very well.


double opp = player.gety() - mousePoint.getY();
double adj = player.getx() - mousePoint.getX();
		
double rad = Math.atan2(opp, adj);
		
player.shootAtRad(rad);

I do realize I am doing something terribly wrong. Any helps ?

I just want to know how to get the angle between the player and the mouse.

Try this:

Math.atan2(opp, -adj);

Why do you have a shootAtRad method? Isn’t it just going to do a sin and cos to get back to an (x, y) vector?

Yes, the bullet takes the rad and then vector x is cos of rad and vector y is sin of rad. Then in the update method, the vectors are added to the position. That is how the bullets move. I didn’t understand what you find wrong.

You have x and y. You convert them to an angle. Then you convert back to x and y. It’s a lot simpler to just normalise the vector and not do any trigonometry at all. In fact, most 2D games have little or no need for trig.