package game;
import engine.geometry.Circle;
import engine.geometry.Vector;
public class MagrayPhysics {
/**
* @param metal - The circle of metal
* @param position - The position to shoot the magray from
* @param direction - The _normalised_ direction to shoot the magray in
* @param beamWidth - The growth of the radius per unit time
* @return The first "time" at which the circles collide.
*/
public static double computeTime(Circle metal, Vector position, Vector direction, double beamWidth) {
double x = position.getX() - metal.getX();
double y = position.getY() - metal.getY();
double r = metal.getRadius();
double dx = direction.getX();
double dy = direction.getY();
double dr = beamWidth;
double a = dx * dx + dy * dy - dr * dr;
double b = 2 * (x * dx + y * dy - r * dr);
double c = x * x + y * y - r * r;
return (-b - Math.sqrt(b * b - 4 * a * c)) / (2 * a);
}
/**
* @param metal - The circle of metal
* @param position - The position to shoot the magray from
* @param direction - The _normalised_ direction to shoot the magray in
* @param time - The first "time" at which the circles collide. (Use the {@link #computeTime} function)
* @return The direction that the metal should repel in.
*/
public static Vector computeNormal(Circle metal, Vector position, Vector direction, double time) {
double dx = position.getX() + direction.getX() * time - metal.getX();
double dy = position.getY() + direction.getY() * time - metal.getY();
return new Vector(dx, dy).normalise();
}
}