99 lines
2.2 KiB
Java
99 lines
2.2 KiB
Java
package shootergame.util.math;
|
|
|
|
import shootergame.util.math.vec.Vec2d;
|
|
import shootergame.util.math.vec.Vec3d;
|
|
|
|
public class MathHelpers
|
|
{
|
|
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)
|
|
{
|
|
// Get the angle and the distance from the centre to the point
|
|
double p_angle = Math.atan2(pos.y, pos.x);
|
|
double p_distance = Math.sqrt(pos.y*pos.y + pos.x*pos.x);
|
|
|
|
// Return and calculate the new positions
|
|
return new Vec2d(
|
|
p_distance * Math.sin(angle - p_angle),
|
|
p_distance * Math.cos(angle - p_angle));
|
|
}
|
|
}
|