135 lines
2.5 KiB
Java
Executable File
135 lines
2.5 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;
|
|
|
|
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 Math.sqrt( squared(x2 - x1) + squared(y2 - y1) + squared(z2 - z1) );
|
|
}
|
|
|
|
public static double distance2d(double x1, double y1, double x2, double y2) {
|
|
return Math.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(Math.sin(angle) * amount, Math.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 = Math.sin(angles.y) * amount;
|
|
m = Math.cos(angles.y);
|
|
points.x = Math.sin(angles.x) * m * amount;
|
|
m = Math.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 = Math.sin(angle);
|
|
double c = Math.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;
|
|
}
|
|
}
|