I’m making a game where you have a sprite that shoots bullets in the direction of the mouse. So far, it works fine with 1 bullet. I have this method that gets a slope, and then normalizes the vector:
public static Vector2f getSimplifiedSlope(Vector2f v1, Vector2f v2) {
Vector2f result = new Vector2f(v2.x - v1.x, v2.y - v1.y);
float length = (float)Math.sqrt(result.x * result.x + result.y * result.y);
return new Vector2f(result.x / length, result.y / length);
}
However, now I’m making a shotgun that fires several bullets, with a “spread”. My plan is, I’ll take the base slope, convert it to degrees, add or subtract a couple to create a deviation, then convert the degrees back to a slope, and pass it to the bullet.
However, I don’t know how to do this. It’d be great if someone could show me how to convert a 2D slope to degrees, and vice versa.
Thanks in advance!
The simplest way is to use the trigonometry method
Math.atan2to compute the angle in radians, convert it to degrees withMath.toDegrees, perform your adjustment, convert it back to radians withMath.toRadians, then use the trigonometry methodMath.tanto convert a radian value back to a slope. You’ll want to watch out for a potentially infinite slope coming back from thetanmethod.Here’s the Javadocs on the
Mathclass.