50 lines
1.1 KiB
Java
50 lines
1.1 KiB
Java
public class PhysicsObject {
|
|
public float x;
|
|
public float y;
|
|
public float v_x;
|
|
public float v_y;
|
|
|
|
public static int width = 0;
|
|
public static int height = 0;
|
|
public static float max_speed = 0;
|
|
|
|
public void simulate() {
|
|
v_x = clamp(v_x, -max_speed, max_speed) * 0.998f;
|
|
v_y = clamp(v_y, -max_speed, max_speed) * 0.998f;
|
|
|
|
x += v_x;
|
|
y += v_y;
|
|
while (x < 0) {
|
|
x += width;
|
|
}
|
|
while (y < 0) {
|
|
y += height;
|
|
}
|
|
x %= width;
|
|
y %= height;
|
|
}
|
|
|
|
private float clamp(float f, float min, float max) {
|
|
return Math.min(max, Math.max(f, min));
|
|
}
|
|
|
|
public void apply_force(float x, float y, float factor) {
|
|
if ((x == 0 && y == 0) || factor == 0)
|
|
return;
|
|
double len = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
|
|
v_x += (x / len) * factor;
|
|
v_y += (y / len) * factor;
|
|
}
|
|
|
|
public PhysicsObject(int x, int y, float v_x, float v_y) {
|
|
this.x = x;
|
|
this.y = y;
|
|
this.v_x = v_x;
|
|
this.v_y = v_y;
|
|
}
|
|
|
|
public double get_sqr_distance(PhysicsObject obj) {
|
|
return Math.pow(obj.x - x, 2) + Math.pow(obj.y - y, 2);
|
|
}
|
|
}
|