Class MathUtils

Object
MathUtils

public class MathUtils extends Object
Contains methods for working with vectors, angles, distances, and circles.
Since:
1.0
Author:
LazyWizard
  • Field Details

  • Method Details

    • getDistance

      public static float getDistance(SectorEntityToken token1, SectorEntityToken token2)
      Returns the distance between two SectorEntityTokens, taking interaction radii into account.
      Since:
      1.0
      See Also:
    • getDistance

      public static float getDistance(SectorEntityToken token, Vector2f loc)
      Returns the distance between a SectorEntityToken and a Vector2f, taking interaction radius into account.
      Since:
      1.1
      See Also:
    • getDistance

      public static float getDistance(CombatEntityAPI entity1, CombatEntityAPI entity2)
      Returns the distance between two CombatEntityAPIs, taking collision radii into account.
      Since:
      1.0
      See Also:
    • getDistance

      public static float getDistance(CombatEntityAPI entity, Vector2f loc)
      Returns the distance between a CombatEntityAPI and a Vector2f, taking collision radius into account.
      Since:
      1.0
      See Also:
    • getDistance

      public static float getDistance(Vector2f loc1, Vector2f loc2)
      Returns the distance between two Vector2fs.

      For comparing distances, it is vastly more efficient to use getDistanceSquared(org.lwjgl.util.vector.Vector2f, org.lwjgl.util.vector.Vector2f).

      Returns:
      The distance between the two vectors.
      Since:
      1.0
    • getDistanceSquared

      public static float getDistanceSquared(SectorEntityToken token1, SectorEntityToken token2)
      Returns the distance squared between two SectorEntityTokens, taking interaction radii into account.

      With the addition of collision radius checking, there's no way to avoid calculating the square root. getDistance(SectorEntityToken, SectorEntityToken) will be just as efficient.

      Since:
      1.0
      See Also:
    • getDistanceSquared

      public static float getDistanceSquared(SectorEntityToken token, Vector2f loc)
      Returns the distance squared between a SectorEntityToken and a Vector2f, taking interaction radius into account.

      With the addition of collision radius checking, there's no way to avoid calculating the square root. getDistance(SectorEntityToken, Vector2f) will be just as efficient.

      Since:
      1.1
      See Also:
    • getDistanceSquared

      public static float getDistanceSquared(CombatEntityAPI entity1, CombatEntityAPI entity2)
      Returns the distance squared between two CombatEntityAPIs, taking collision radii into account.

      With the addition of collision radius checking, there's no way to avoid calculating the square root. getDistance(CombatEntityAPI, CombatEntityAPI) will be just as efficient.

      Since:
      1.0
      See Also:
    • getDistanceSquared

      public static float getDistanceSquared(CombatEntityAPI entity, Vector2f loc)
      Returns the distance squared between a CombatEntityAPI and a Vector2f, taking collision radius into account.

      With the addition of collision radius checking, there's no way to avoid calculating the square root. getDistance(CombatEntityAPI, Vector2f) will be just as efficient.

      Since:
      1.0
      See Also:
    • getDistanceSquared

      public static float getDistanceSquared(Vector2f loc1, Vector2f loc2)
      Returns the distance squared between two Vector2fs (avoids a costly sqrt()).

      When comparing distances, use this function instead of getDistance(Vector2f, Vector2f).

      Returns:
      The distance squared between the two vectors.
      Since:
      1.0
    • isWithinRange

      public static boolean isWithinRange(SectorEntityToken token1, SectorEntityToken token2, float range)
      Check if two SectorEntityTokens are within a certain distance of each other, taking interaction radii into account.

      This is vastly more efficient than comparisons using getDistance(SectorEntityToken, SectorEntityToken), and should be used whenever possible.

      Parameters:
      token1 - The first SectorEntityToken to check.
      token2 - The second SectorEntityToken to check.
      range - The minimum distance between token1 and token2.
      Returns:
      Whether token1 is within range su of token2.
      Since:
      1.8
    • isWithinRange

      public static boolean isWithinRange(SectorEntityToken token, Vector2f loc, float range)
      Check if a SectorEntityToken is within a certain distance of a location, taking interaction radius into account.

      This is vastly more efficient than comparisons using getDistance(SectorEntityToken, Vector2f), and should be used whenever possible.

      Parameters:
      token - The SectorEntityToken to check.
      loc - The Vector2f to check.
      range - The minimum distance between token and loc.
      Returns:
      Whether token is within range su of loc.
      Since:
      1.8
    • isWithinRange

      public static boolean isWithinRange(CombatEntityAPI entity1, CombatEntityAPI entity2, float range)
      Check if two CombatEntityAPIs are within a certain distance of each other, taking collision radii into account.

      This is vastly more efficient than comparisons using getDistance(CombatEntityAPI, CombatEntityAPI), and should be used whenever possible.

      Parameters:
      entity1 - The first CombatEntityAPI to check.
      entity2 - The second CombatEntityAPI to check.
      range - The minimum distance between entity1 and entity2.
      Returns:
      Whether entity1 is within range su of entity2.
      Since:
      1.8
    • isWithinRange

      public static boolean isWithinRange(CombatEntityAPI entity, Vector2f loc, float range)
      Check if a CombatEntityAPI is within a certain distance of a location, taking collision radius into account.

      This is vastly more efficient than comparisons using getDistance(CombatEntityAPI, Vector2f), and should be used whenever possible.

      Parameters:
      entity - The CombatEntityAPI to check.
      loc - The Vector2f to check.
      range - The minimum distance between entity and loc.
      Returns:
      Whether entity is within range su of loc.
      Since:
      1.8
    • isWithinRange

      public static boolean isWithinRange(Vector2f loc1, Vector2f loc2, float range)
      Check if two objects are within a certain distance of each other.
      Parameters:
      loc1 - The first Vector2f.
      loc2 - The second Vector2f.
      range - The minimum distance between loc1 and loc2.
      Returns:
      Whether loc1 is within range su of loc2.
      Since:
      1.8
    • clampAngle

      public static float clampAngle(float angle)
      Clamps an angle within 360 degrees (ex: 400 degrees becomes 40 degrees).
      Parameters:
      angle - The angle to be clamped.
      Returns:
      A value between 0 and 360 degrees.
      Since:
      1.2
    • clamp

      public static float clamp(float toClamp, float min, float max)
      Clamps a Float, ensuring it falls within a given range.
      Parameters:
      toClamp - The Float to be clamped.
      min - The minimum value you wish the return value to hold.
      max - The maximum value you wish the return value to hold.
      Returns:
      toClamp if it falls between min and max, min if it falls below, or max if it falls above.
      Since:
      2.3
    • clamp

      public static int clamp(int toClamp, int min, int max)
      Clamps an Integer, ensuring it falls within a given range.
      Parameters:
      toClamp - The Integer to be clamped.
      min - The minimum value you wish the return value to hold.
      max - The maximum value you wish the return value to hold.
      Returns:
      toClamp if it falls between min and max, min if it falls below, or max if it falls above.
      Since:
      2.3
    • getShortestRotation

      public static float getShortestRotation(float currAngle, float destAngle)
      Returns the direction and length of the quickest rotation between two angles.
      Parameters:
      currAngle - The current facing.
      destAngle - The facing to rotate towards.
      Returns:
      The shortest distance between the two angles, in degrees.
      Since:
      1.7
    • getMidpoint

      public static Vector2f getMidpoint(Vector2f point1, Vector2f point2)
      Returns the point exactly between two other points.
      Parameters:
      point1 - The first point.
      point2 - The second point.
      Returns:
      A Vector2f at the midpoint of the line between point1 and point2.
      Since:
      1.9
    • getPointOnCircumference

      public static Vector2f getPointOnCircumference(@Nullable Vector2f center, float radius, float angle)
      Returns a point along the circumference of a circle at the given angle.
      Parameters:
      center - The center point of the circle (can be null for a 0, 0 origin).
      radius - The radius of the circle.
      angle - The angle, in degrees, to get the point at.
      Returns:
      A Vector2f at [@code angle} degrees along the circumference of the given circle.
      Since:
      1.0
    • getPoint

      public static Vector2f getPoint(@Nullable Vector2f center, float radius, float angle)
      Returns a point along the circumference of a circle at the given angle. Provided as an alias for getPointOnCircumference(Vector2f, float, float).
      Parameters:
      center - The center point of the circle (can be null for a 0, 0 origin).
      radius - The radius of the circle.
      angle - The angle, in degrees, to get the point at.
      Returns:
      A Vector2f at angle degrees along the circumference of the given circle.
      Since:
      2.0
      See Also:
    • getRandomPointOnCircumference

      public static Vector2f getRandomPointOnCircumference(@Nullable Vector2f center, float radius)
      Returns a random point along the circumference of a circle.
      Parameters:
      center - The center point of the circle (can be null for a 0, 0 origin).
      radius - The radius of the circle.
      Returns:
      A random point along the circumference of the given circle.
      Since:
      1.0
      See Also:
    • getRandomPointInCircle

      public static Vector2f getRandomPointInCircle(@Nullable Vector2f center, float radius)
      Returns a random point inside of a circle with uniform distribution.
      Parameters:
      center - The center point of the circle (can be null for a 0, 0 origin).
      radius - The radius of the circle.
      Returns:
      A random point inside of the given circle.
      Since:
      1.0
    • getRandomPointInCone

      public static Vector2f getRandomPointInCone(@Nullable Vector2f center, float radius, float minAngle, float maxAngle)
      Returns a random point inside of a circular sector (2d cone) with uniform distribution.
      Parameters:
      center - The center point of the cone (can be null for a 0, 0 origin).
      radius - The radius of the cone.
      minAngle - The minimum angular bounds.
      maxAngle - The maximum angular bounds.
      Returns:
      A random point inside of the given circular sector.
      Since:
      1.7
    • getRandomPointOnLine

      public static Vector2f getRandomPointOnLine(Vector2f lineStart, Vector2f lineEnd)
      Returns a random point along the line between two Vector2fs.
      Parameters:
      lineStart - The starting point of the line.
      lineEnd - The end point of the line.
      Returns:
      A random Vector2f along the line between lineStart and lineEnd.
      Since:
      1.6
    • getPointsAlongCircumference

      public static List<Vector2f> getPointsAlongCircumference(@Nullable Vector2f center, float radius, int numPoints, float angleOffset)
      Returns an evenly distributed List of points along a circle's circumference.
      Parameters:
      center - The center point of the circle (can be null for a 0, 0 origin).
      radius - The radius of the circle.
      numPoints - How many points to generate.
      angleOffset - The offset angle of the initial point.
      Returns:
      A List of Vector2fs that are evenly distributed along the circle's circumference.
      Since:
      1.0
    • isPointWithinCircle

      public static boolean isPointWithinCircle(Vector2f point, @Nullable Vector2f center, float radius)
      Checks whether a point is on or within the bounds of a circle.
      Parameters:
      point - The Vector2f to check.
      center - The center point of the circle (can be null for a 0, 0 origin).
      radius - The radius of the circle.
      Returns:
      true if point is on or within the circle, false otherwise.
      Since:
      1.0
    • isPointOnLine

      public static boolean isPointOnLine(Vector2f point, Vector2f lineStart, Vector2f lineEnd)
      Check if a point is along the line between two Vector2fs. Accurate to within 1/100 of a unit.
      Parameters:
      point - The point to check.
      lineStart - The starting point of the line.
      lineEnd - The end point of the line.
      Returns:
      true if the point is along the line, false otherwise.
      Since:
      1.6
    • getNearestPointOnLine

      public static Vector2f getNearestPointOnLine(Vector2f source, Vector2f lineStart, Vector2f lineEnd)
      Given a point and a line, returns the nearest point on said line to that point.
      Parameters:
      source - The point to test distance from.
      lineStart - The start point of the line to check.
      lineEnd - The end point of the line to check.
      Returns:
      The point on the line between lineStart and lineEnd nearest to source.
      Since:
      2.3
    • getEquidistantPointsInsideCircle

      public static List<Vector2f> getEquidistantPointsInsideCircle(@Nullable Vector2f center, float radius, float spaceBetweenPoints)
      Returns a List of evenly spaced Vector2fs inside a circle.

      WARNING: be VERY conservative using this method - a radius of 250 and a spacing of 5 will result in 10,000 circle checks and 7,825 Vector2fs created!

      Parameters:
      center - The center point of the circle (can be null for a 0, 0 origin).
      radius - The radius of the circle.
      spaceBetweenPoints - How much space should be between each point.
      Returns:
      A List of evenly spaced Vector2fs inside a circle.
      Since:
      1.4
    • getRandomNumberInRange

      public static float getRandomNumberInRange(float min, float max)
      Returns a random float within a given range.
      Parameters:
      min - The minimum value to select.
      max - The maximum value to select.
      Returns:
      A random Float between min and max.
      Since:
      1.4
    • getRandomNumberInRange

      public static int getRandomNumberInRange(int min, int max)
      Returns a random integer within a given range.
      Parameters:
      min - The minimum value to select.
      max - The maximum value to select (inclusive).
      Returns:
      A random Integer between min and max, inclusive.
      Since:
      2.0
    • equals

      public static boolean equals(float a, float b)
      Tests for near-equality of floating point numbers.
      Parameters:
      a - The first float to compare.
      b - The second float to compare.
      Returns:
      true if a and b are within 99.99999% of each other, false otherwise.
    • getRandom

      public static Random getRandom()
      Returns the random number generator used by LazyLib's methods. Useful for seed manipulation or to avoid instantiating your own.
      Returns:
      The Random instance used by LazyLib.
      Since:
      1.5