Skip to content

Commit 8c60fcb

Browse files
committed
fix math
1 parent 3eb4234 commit 8c60fcb

File tree

2 files changed

+10
-21
lines changed

2 files changed

+10
-21
lines changed

src/main/java/com/cleanroommc/modularui/utils/MathUtils.java

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,21 @@ public static int wrapDegrees(int angle) {
163163
}
164164

165165
public static float sin(float v) {
166-
return MathHelper.sin(v);
166+
// MathHelper.sin doesn't account for negative numbers
167+
// with the point symmetry property of sin we can easily fix it
168+
// if v is negative, negate the input and then the output
169+
float s = Math.signum(v);
170+
return s * MathHelper.sin(s * v);
167171
}
168172

169173
public static float cos(float v) {
170-
return MathHelper.cos(v);
174+
// MathHelper.cos doesn't account for negative numbers
175+
// with the axis symmetry property of cos we can easily fix it
176+
return MathHelper.cos(Math.abs(v));
171177
}
172178

173179
public static float tan(float v) {
174-
return MathHelper.sin(v) / MathHelper.cos(v);
180+
return sin(v) / cos(v);
175181
}
176182

177183
public static double sqrt(double v) {
@@ -181,21 +187,4 @@ public static double sqrt(double v) {
181187
public static float sqrt(float v) {
182188
return (float) Math.sqrt(v);
183189
}
184-
185-
/**
186-
* Computes 1/sqrt(n) using <a href="https://en.wikipedia.org/wiki/Fast_inverse_square_root">the fast inverse square
187-
* root</a> with a constant of 0x5FE6EB50C7B537AA.
188-
*/
189-
public static double fastInvSqrt(double v) {
190-
double d0 = 0.5D * v;
191-
long i = Double.doubleToRawLongBits(v);
192-
i = 6910469410427058090L - (i >> 1);
193-
v = Double.longBitsToDouble(i);
194-
v = v * (1.5D - d0 * v * v);
195-
return v;
196-
}
197-
198-
public static float fastInvSqrt(float v) {
199-
return (float) fastInvSqrt((double) v);
200-
}
201190
}

src/main/java/com/cleanroommc/modularui/utils/Vector3f.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public Vector3f normalise(Vector3f dest) {
221221
if (dest == null) dest = new Vector3f();
222222
float lsq = lengthSquared();
223223
if (lsq == 1) return dest.set(this);
224-
float f = MathUtils.fastInvSqrt(lsq);
224+
float f = (float) (1f / Math.sqrt(lsq));
225225
dest.set(x * f, y * f, z * f);
226226
return dest;
227227
}

0 commit comments

Comments
 (0)