204 lines
4.0 KiB
Java
Executable File
204 lines
4.0 KiB
Java
Executable File
package gl_engine;
|
|
|
|
import gl_engine.vec.Vec2d;
|
|
import gl_engine.vec.Vec3d;
|
|
|
|
public class MathHelpers
|
|
{
|
|
public static final double FallSpeed = 0.00098;
|
|
private static double[] TRIG_SIN_VALUES = null;
|
|
private static double[] TRIG_ASIN_VALUES = null;
|
|
|
|
public static final double PI = 3.1415926535897932384626;
|
|
public static final double TWO_PI = PI * 2;
|
|
public static final double HALF_PI = PI / 2;
|
|
|
|
public static void init()
|
|
{
|
|
// Initialize by default with 8 MB of trig values
|
|
init(1048576);
|
|
}
|
|
|
|
public static void init(int size)
|
|
{
|
|
TRIG_SIN_VALUES = new double[size];
|
|
TRIG_ASIN_VALUES = new double[size];
|
|
|
|
for(int i=0;i<size;i++) {
|
|
TRIG_SIN_VALUES[i] = Math.sin((i / (double)size) * TWO_PI);
|
|
TRIG_ASIN_VALUES[i] = HALF_PI - Math.asin(((2 * i) / (double)size) - 1);
|
|
}
|
|
}
|
|
|
|
public static double sin(double a) {
|
|
return TRIG_SIN_VALUES[(int)((mod(a, TWO_PI) / TWO_PI) * TRIG_SIN_VALUES.length)];
|
|
}
|
|
|
|
public static double cos(double a) {
|
|
return sin(a + HALF_PI);
|
|
}
|
|
|
|
public static double tan(double a) {
|
|
return sin(a) / sin(a + HALF_PI);
|
|
}
|
|
|
|
public static double asin(double a) {
|
|
if(a < -1 || a > 1) return Double.NaN;
|
|
return TRIG_ASIN_VALUES[(int)((a + 1) / (2 * TRIG_ASIN_VALUES.length))];
|
|
}
|
|
|
|
public static double acos(double a) {
|
|
return HALF_PI - asin(a);
|
|
}
|
|
|
|
public static double atan(double a) {
|
|
return Math.atan(a);
|
|
}
|
|
|
|
public static double atan2(double y, double x)
|
|
{
|
|
if(x > 0) return tan(y / x);
|
|
|
|
if(x < 0) {
|
|
if(y >= 0) return tan(y / x) + PI;
|
|
if(y < 0) return tan(y / x) - PI;
|
|
};
|
|
|
|
if(x == 0) {
|
|
if(y > 0) return HALF_PI;
|
|
if(y < 0) return -HALF_PI;
|
|
}
|
|
|
|
return Double.NaN;
|
|
}
|
|
|
|
public static double sqrt(double a) {
|
|
return Math.sqrt(a);
|
|
}
|
|
|
|
public static double nextPowerOf(double x, double n)
|
|
{
|
|
double i = 1;
|
|
|
|
while(i < x) {
|
|
i *= n;
|
|
}
|
|
|
|
return i;
|
|
}
|
|
|
|
public static int nextPowerOf(double x, int n)
|
|
{
|
|
int i = 1;
|
|
|
|
while(i < x) {
|
|
i *= n;
|
|
}
|
|
|
|
return i;
|
|
}
|
|
|
|
public static double squared(double x) {
|
|
return x*x;
|
|
}
|
|
|
|
public static double pow(int c, double x)
|
|
{
|
|
double res = 1;
|
|
|
|
for(int i=0;i<c;i++) {
|
|
res *= x;
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
public static double distance3d(double x1, double y1, double z1, double x2, double y2, double z2) {
|
|
return MathHelpers.sqrt( squared(x2 - x1) + squared(y2 - y1) + squared(z2 - z1) );
|
|
}
|
|
|
|
public static double distance2d(double x1, double y1, double x2, double y2) {
|
|
return MathHelpers.sqrt( squared(x2 - x1) + squared(y2 - y1) );
|
|
}
|
|
|
|
public static double map(double x, double in_min, double in_max, double out_min, double out_max) {
|
|
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
|
|
}
|
|
|
|
public static Vec2d moveTowards2(double amount, double angle) {
|
|
return new Vec2d(sin(angle) * amount, cos(angle) * amount);
|
|
}
|
|
|
|
public static Vec3d moveTowards3(double amount, Vec2d angles)
|
|
{
|
|
// Create some varibles
|
|
Vec3d points = new Vec3d(0, 0, 0);
|
|
double m;
|
|
|
|
points.z = sin(angles.y) * amount;
|
|
m = cos(angles.y);
|
|
points.x = sin(angles.x) * m * amount;
|
|
m = cos(angles.x) * m;
|
|
points.y = m * amount;
|
|
|
|
return points;
|
|
}
|
|
|
|
public static double mod(double a, double b) {
|
|
return (((a % b) + b) % b);
|
|
}
|
|
|
|
public static int mod(int a, int b) {
|
|
return (((a % b) + b) % b);
|
|
}
|
|
|
|
public static int floor(double a)
|
|
{
|
|
if((int)a == a) {
|
|
return (int) a;
|
|
}
|
|
|
|
else if(a < 0) {
|
|
return (int)(a - 1);
|
|
}
|
|
|
|
else {
|
|
return (int)a;
|
|
}
|
|
}
|
|
|
|
public static double positive(double a) {
|
|
if(a < 0) return -a;
|
|
else return a;
|
|
}
|
|
|
|
public static int positive(int a) {
|
|
if(a < 0) return -a;
|
|
else return a;
|
|
}
|
|
|
|
public static Vec2d rotate2(Vec2d pos, double angle)
|
|
{
|
|
// Calculate 2 sin and cos values
|
|
double s = sin(angle);
|
|
double c = cos(angle);
|
|
|
|
// Return and calculate the new positions
|
|
return new Vec2d(
|
|
pos.x * c - pos.y * s,
|
|
pos.x * s + pos.y * c);
|
|
}
|
|
|
|
public static double biggest(double a, double b)
|
|
{
|
|
if(a > b) return a;
|
|
else return b;
|
|
}
|
|
|
|
public static double smallest(double a, double b)
|
|
{
|
|
if(a < b) return a;
|
|
else return b;
|
|
}
|
|
}
|