Here are a couple of utility methods I wrote that work like a charm:
public static vec2 point(vec2 pivot, vec2 point, float rotation) {
float rot = (float)(1f / 180 * rotation * Math.PI);
float x = point.x - pivot.x;
float y = point.y - pivot.y;
float newx = (float)(x * Math.cos(rot) - y * Math.sin(rot));
float newy = (float)(x * Math.sin(rot) + y * Math.cos(rot));
newx += pivot.x;
newy += pivot.y;
return new vec2(newx, newy);
}
public static int angle(vec2 pivot, vec2 point) {
float xdiff = pivot.x - point.x;
float ydiff = pivot.y - point.y;
float angle = (float) ((Math.atan2(xdiff, ydiff)) * 180 / Math.PI);
return -(int)angle;
}
Here is vec2 class.
public class vec2 {
public float x, y;
public vec2(float x, float y) {
this.x = x;
this.y = y;
}
public int getX() {
return (int) x;
}
public int getY() {
return (int) y;
}
}
Now, to get velocities, you do something like this:
float rot = angle(new vec2(player.x, player.y), new vec2(mouse.x, mouse.y));
vec2 velocity = point(new vec2(0, 0), new vec2(0, -speed), rot);
To get x and y velocities, do velocity.x for x velocity and velocity.y for y velocity.