44
55public class MathUtils {
66
7- private static MathUtils instance ;
8- private double [] cos = new double [360 ];
9- private double [] sin = new double [360 ];
10-
11- public MathUtils (){
12- instance = this ;
7+ private static double [] cos = new double [360 ];
8+ private static double [] sin = new double [360 ];
9+
10+ static {
1311 for (int deg = 0 ; deg < 360 ; deg ++) {
14- cos [deg ] = Math .cos (Math .toRadians (deg ));
15- sin [deg ] = Math .sin (Math .toRadians (deg ));
12+ cos [deg ] = Math .cos (Math .toRadians (deg ));
13+ sin [deg ] = Math .sin (Math .toRadians (deg ));
1614 }
1715 }
1816
1917 private static double [] getCos (){
20- return MathUtils . instance . cos ;
18+ return cos ;
2119 }
2220
2321 private static double [] getSin (){
24- return MathUtils . instance . sin ;
22+ return sin ;
2523 }
2624
2725 public static Location stereoSourceLeft (Location location , float distance ) {
28- float yaw = location .getYaw ();
29- return location .clone ().add (-getCos ()[( int ) ( yaw + 360 ) % 360 ] * distance , 0 , -getSin ()[( int ) ( yaw + 360 ) % 360 ] * distance );
26+ int angle = getAngle ( location .getYaw () );
27+ return location .clone ().add (-getCos ()[angle ] * distance , 0 , -getSin ()[angle ] * distance );
3028 }
3129 public static Location stereoSourceRight (Location location , float distance ) {
32- float yaw = location .getYaw ();
33- return location .clone ().add (getCos ()[( int ) ( yaw + 360 ) % 360 ] * distance , 0 , getSin ()[( int ) ( yaw + 360 ) % 360 ] * distance );
30+ int angle = getAngle ( location .getYaw () );
31+ return location .clone ().add (getCos ()[angle ] * distance , 0 , getSin ()[angle ] * distance );
3432 }
3533
3634 /**
@@ -40,8 +38,14 @@ public static Location stereoSourceRight(Location location, float distance) {
4038 * @return
4139 */
4240 public static Location stereoPan (Location location , float distance ){
43- float yaw = location .getYaw ();
44- return location .clone ().add ( getCos ()[(int ) (yaw + 360 ) % 360 ] * distance , 0 , getSin ()[(int ) (yaw + 360 ) % 360 ] * distance );
41+ int angle = getAngle (location .getYaw ());
42+ return location .clone ().add ( getCos ()[angle ] * distance , 0 , getSin ()[angle ] * distance );
43+ }
44+
45+ private static int getAngle (float yaw ){
46+ int angle = (int ) yaw ;
47+ while (angle < 0 ) angle += 360 ;
48+ return angle % 360 ;
4549 }
4650
4751}
0 commit comments