Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.lazywizard.lazylib.FastTrig;
- import org.lazywizard.lazylib.MathUtils;
- import org.lazywizard.lazylib.VectorUtils;
- import org.lwjgl.util.vector.Vector2f;
- public class Collider {
- // Slightly faster than the other methods
- public static boolean isWithinRange(Vector2f a, Vector2f b, float r, float rsquared) {
- float dx = a.x - b.x;
- if (dx > r || dx < -r) {
- return false;
- }
- float dy = a.y - b.y;
- if (dy > r || dy < -r) {
- return false;
- }
- return dx * dx + dy * dy <= rsquared;
- }
- public static boolean isWithinRange(Vector2f a, Vector2f b, float r) {
- float dx = a.x - b.x;
- if (dx > r || dx < -r) {
- return false;
- }
- float dy = a.y - b.y;
- if (dy > r || dy < -r) {
- return false;
- }
- return dx * dx + dy * dy <= r * r;
- }
- // Slower than the other methods
- public static boolean isWithinRangeSquared(Vector2f a, Vector2f b, float rsquared) {
- float dx = a.x - b.x;
- dx *= dx;
- if (dx > rsquared) {
- return false;
- }
- float dy = a.y - b.y;
- dy *= dy;
- if (dy > rsquared) {
- return false;
- }
- return dx + dy <= rsquared;
- }
- // Slightly slower than the other method
- public static boolean pointInEllipse(Vector2f point, Vector2f ellipseCenter, float sideRadius, float forwardRadius, float ellipseAngle) {
- double angle = VectorUtils.getAngle(ellipseCenter, point);
- double squareDistance = MathUtils.getDistanceSquared(point, ellipseCenter);
- double sideRadiusSquared = sideRadius * sideRadius;
- double forwardRadiusSquared = forwardRadius * forwardRadius;
- double cos = FastTrig.cos(angle - ellipseAngle);
- double cosSquared = cos * cos;
- double squareRadius = forwardRadiusSquared + cosSquared * (sideRadiusSquared - forwardRadiusSquared);
- return squareDistance <= squareRadius;
- }
- // Slightly faster than the other method
- public static boolean pointInEllipseSquared(Vector2f point, Vector2f ellipseCenter, float sideRadiusSquared, float forwardRadiusSquared, float ellipseAngle) {
- double angle = VectorUtils.getAngle(ellipseCenter, point);
- double squareDistance = MathUtils.getDistanceSquared(point, ellipseCenter);
- double cos = FastTrig.cos(angle - ellipseAngle);
- double cosSquared = cos * cos;
- double squareRadius = forwardRadiusSquared + cosSquared * (sideRadiusSquared - forwardRadiusSquared);
- return squareDistance <= squareRadius;
- }
- // Slightly slower than the other method
- public static boolean circleIntersectsEllipse(Vector2f point, float radius, Vector2f ellipseCenter, float sideRadius, float forwardRadius, float ellipseAngle) {
- double angle = VectorUtils.getAngle(ellipseCenter, point);
- double distance = MathUtils.getDistance(point, ellipseCenter) + radius;
- double squareDistance = distance * distance;
- double sideRadiusSquared = sideRadius * sideRadius;
- double forwardRadiusSquared = forwardRadius * forwardRadius;
- double cos = FastTrig.cos(angle - ellipseAngle);
- double cosSquared = cos * cos;
- double squareRadius = forwardRadiusSquared + cosSquared * (sideRadiusSquared - forwardRadiusSquared);
- return squareDistance <= squareRadius;
- }
- // Slightly faster than the other method
- public static boolean circleIntersectsEllipseSquared(Vector2f point, float radius, Vector2f ellipseCenter, float sideRadiusSquared, float forwardRadiusSquared, float ellipseAngle) {
- double angle = VectorUtils.getAngle(ellipseCenter, point);
- double distance = MathUtils.getDistance(point, ellipseCenter) + radius;
- double squareDistance = distance * distance;
- double cos = FastTrig.cos(angle - ellipseAngle);
- double cosSquared = cos * cos;
- double squareRadius = forwardRadiusSquared + cosSquared * (sideRadiusSquared - forwardRadiusSquared);
- return squareDistance <= squareRadius;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement