public class MathUtils
extends Object
Modifier and Type | Method and Description |
---|---|
static float |
clampAngle(float angle)
Clamps an angle within 360 degrees (ex: 400 degrees becomes 40 degrees).
|
static boolean |
equals(float a,
float b)
Tests for near-equality of floating point numbers.
|
static float |
getDistance(CombatEntityAPI entity1,
CombatEntityAPI entity2)
Returns the distance between two
CombatEntityAPI s, including
collision radii. |
static float |
getDistance(CombatEntityAPI entity,
Vector2f loc)
Returns the distance between a
CombatEntityAPI and a
Vector2f , including collision radius. |
static float |
getDistance(SectorEntityToken token1,
SectorEntityToken token2)
Returns the distance between two
SectorEntityToken s,
including interaction radii. |
static float |
getDistance(SectorEntityToken token,
Vector2f loc)
Returns the distance between a
SectorEntityToken and a
Vector2f , including interaction radius. |
static float |
getDistance(Vector2f loc1,
Vector2f loc2)
Returns the distance between two
Vector2f s. |
static float |
getDistanceSquared(CombatEntityAPI entity1,
CombatEntityAPI entity2)
Returns the distance squared between two
CombatEntityAPI s,
including collision radii. |
static float |
getDistanceSquared(CombatEntityAPI entity,
Vector2f loc)
Returns the distance squared between a
CombatEntityAPI and a
Vector2f (includes collision radius). |
static float |
getDistanceSquared(SectorEntityToken token1,
SectorEntityToken token2)
Returns the distance squared between two
SectorEntityToken s,
including interaction radii. |
static float |
getDistanceSquared(SectorEntityToken token,
Vector2f loc)
Returns the distance squared between a
SectorEntityToken and
a Vector2f , including interaction radius. |
static float |
getDistanceSquared(Vector2f loc1,
Vector2f loc2)
Returns the distance squared between two
Vector2f s (avoids a
costly sqrt()). |
static List<Vector2f> |
getEquidistantPointsInsideCircle(Vector2f center,
float radius,
float spaceBetweenPoints)
Returns a
List of evenly spaced Vector2f s inside a
circle. |
static Vector2f |
getMidpoint(Vector2f point1,
Vector2f point2)
Returns the point exactly between two other points.
|
static Vector2f |
getPointOnCircumference(Vector2f center,
float radius,
float angle)
Returns a point along the circumference of a circle at the given angle.
|
static List<Vector2f> |
getPointsAlongCircumference(Vector2f center,
float radius,
int numPoints,
float angleOffset)
Returns an evenly distributed
List of points along a circle's
circumference. |
static Random |
getRandom()
Returns the random number generator used by LazyLib's methods.
|
static float |
getRandomNumberInRange(float min,
float max)
Returns a random float within a given range.
|
static int |
getRandomNumberInRange(int min,
int max)
Returns a random integer within a given range.
|
static Vector2f |
getRandomPointInCircle(Vector2f center,
float radius)
Returns a random point inside of a circle with uniform distribution.
|
static Vector2f |
getRandomPointInCone(Vector2f center,
float radius,
float minAngle,
float maxAngle)
Returns a random point inside of a circular sector (2d cone) with uniform
distribution.
|
static Vector2f |
getRandomPointOnCircumference(Vector2f center,
float radius)
Returns a random point along the circumference of a circle.
|
static Vector2f |
getRandomPointOnLine(Vector2f lineStart,
Vector2f lineEnd)
Returns a random point along the line between two
Vector2f s. |
static float |
getShortestRotation(float currAngle,
float destAngle)
Returns the direction and length of the quickest rotation between two
angles.
|
static boolean |
isPointOnLine(Vector2f point,
Vector2f lineStart,
Vector2f lineEnd)
Check if a point is along the line between two
Vector2f s. |
static boolean |
isPointWithinCircle(Vector2f point,
Vector2f center,
float radius)
Checks whether a point is on or within the bounds of a circle.
|
static boolean |
isWithinRange(CombatEntityAPI entity1,
CombatEntityAPI entity2,
float range)
Check if two
CombatEntityAPI s are within a certain distance of
each other, including collision radii. |
static boolean |
isWithinRange(CombatEntityAPI entity,
Vector2f loc,
float range)
Check if a
CombatEntityAPI is within a certain distance of a
location, including collision radius. |
static boolean |
isWithinRange(SectorEntityToken token1,
SectorEntityToken token2,
float range)
Check if two
SectorEntityToken s are within a certain distance of
each other, including interaction radii. |
static boolean |
isWithinRange(SectorEntityToken token,
Vector2f loc,
float range)
Check if a
SectorEntityToken is within a certain distance of a
location, including interaction radius. |
static boolean |
isWithinRange(Vector2f loc1,
Vector2f loc2,
float range)
Check if two objects are within a certain distance of each other.
|
public static float getDistance(SectorEntityToken token1, SectorEntityToken token2)
SectorEntityToken
s,
including interaction radii.getDistance(Vector2f, Vector2f)
public static float getDistance(SectorEntityToken token, Vector2f loc)
SectorEntityToken
and a
Vector2f
, including interaction radius.getDistance(Vector2f, Vector2f)
public static float getDistance(CombatEntityAPI entity1, CombatEntityAPI entity2)
CombatEntityAPI
s, including
collision radii.getDistance(Vector2f, Vector2f)
public static float getDistance(CombatEntityAPI entity, Vector2f loc)
CombatEntityAPI
and a
Vector2f
, including collision radius.getDistance(Vector2f, Vector2f)
public static float getDistance(Vector2f loc1, Vector2f loc2)
Vector2f
s.
For comparing distances, it is vastly more efficient to use
getDistanceSquared(org.lwjgl.util.vector.Vector2f,
org.lwjgl.util.vector.Vector2f)
.
public static float getDistanceSquared(SectorEntityToken token1, SectorEntityToken token2)
SectorEntityToken
s,
including interaction radii.
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.
getDistanceSquared(Vector2f, Vector2f)
public static float getDistanceSquared(SectorEntityToken token, Vector2f loc)
SectorEntityToken
and
a Vector2f
, including interaction radius.
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.
getDistanceSquared(Vector2f, Vector2f)
public static float getDistanceSquared(CombatEntityAPI entity1, CombatEntityAPI entity2)
CombatEntityAPI
s,
including collision radii.
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.
getDistanceSquared(Vector2f, Vector2f)
public static float getDistanceSquared(CombatEntityAPI entity, Vector2f loc)
CombatEntityAPI
and a
Vector2f
(includes collision radius).
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.
getDistanceSquared(Vector2f, Vector2f)
public static float getDistanceSquared(Vector2f loc1, Vector2f loc2)
Vector2f
s (avoids a
costly sqrt()).
When comparing distances, use this function instead of
getDistance(Vector2f, Vector2f)
.
public static boolean isWithinRange(SectorEntityToken token1, SectorEntityToken token2, float range)
SectorEntityToken
s are within a certain distance of
each other, including interaction radii.
This is vastly more efficient than comparisons using
getDistance(SectorEntityToken, SectorEntityToken)
,
and should be used whenever possible.
token1
- The first SectorEntityToken
to check.token2
- The second SectorEntityToken
to check.range
- The minimum distance between token1
and
token2
.
token1
is within range
su of
token2
.
public static boolean isWithinRange(SectorEntityToken token, Vector2f loc, float range)
SectorEntityToken
is within a certain distance of a
location, including interaction radius.
This is vastly more efficient than comparisons using
getDistance(SectorEntityToken, Vector2f)
,
and should be used whenever possible.
token
- The SectorEntityToken
to check.loc
- The Vector2f
to check.range
- The minimum distance between token
and loc
.
token
is within range
su of loc
.
public static boolean isWithinRange(CombatEntityAPI entity1, CombatEntityAPI entity2, float range)
CombatEntityAPI
s are within a certain distance of
each other, including collision radii.
This is vastly more efficient than comparisons using
getDistance(CombatEntityAPI, CombatEntityAPI)
,
and should be used whenever possible.
entity1
- The first CombatEntityAPI
to check.entity2
- The second CombatEntityAPI
to check.range
- The minimum distance between entity1
and
entity2
.
entity1
is within range
su of
entity2
.
public static boolean isWithinRange(CombatEntityAPI entity, Vector2f loc, float range)
CombatEntityAPI
is within a certain distance of a
location, including collision radius.
This is vastly more efficient than comparisons using
getDistance(CombatEntityAPI, Vector2f)
,
and should be used whenever possible.
entity
- The CombatEntityAPI
to check.loc
- The Vector2f
to check.range
- The minimum distance between entity
and
loc
.
entity
is within range
su of loc
.
public static boolean isWithinRange(Vector2f loc1, Vector2f loc2, float range)
public static float clampAngle(float angle)
angle
- The angle to be clamped.
public static float getShortestRotation(float currAngle, float destAngle)
currAngle
- The current facing.destAngle
- The facing to rotate towards.
public static Vector2f getMidpoint(Vector2f point1, Vector2f point2)
point1
- The first point.point2
- The second point.
Vector2f
at the midpoint of the line between
point1
and point2
.
public static Vector2f getPointOnCircumference(Vector2f center, float radius, float angle)
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.
Vector2f
at [@code angle} degrees along the
circumference of the given circle.
public static Vector2f getRandomPointOnCircumference(Vector2f center, float radius)
center
- The center point of the circle (can be null for a 0, 0
origin).radius
- The radius of the circle.
getPointOnCircumference(Vector2f, float, float)
public static Vector2f getRandomPointInCircle(Vector2f center, float radius)
center
- The center point of the circle (can be null for a 0, 0
origin).radius
- The radius of the circle.
public static Vector2f getRandomPointInCone(Vector2f center, float radius, float minAngle, float maxAngle)
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.
public static Vector2f getRandomPointOnLine(Vector2f lineStart, Vector2f lineEnd)
Vector2f
s.lineStart
- The starting point of the line.lineEnd
- The end point of the line.
Vector2f
along the line between
lineStart
and lineEnd
.
public static List<Vector2f> getPointsAlongCircumference(Vector2f center, float radius, int numPoints, float angleOffset)
List
of points along a circle's
circumference.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.
List
of Vector2f
s that are evenly distributed
along the circle's circumference.
public static boolean isPointWithinCircle(Vector2f point, Vector2f center, float radius)
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.
true
if point
is on or within the circle,
false
otherwise.
public static boolean isPointOnLine(Vector2f point, Vector2f lineStart, Vector2f lineEnd)
Vector2f
s.
Accurate to within 1/100 of a unit.point
- The point to check.lineStart
- The starting point of the line.lineEnd
- The end point of the line.
true
if the point is along the line, false
otherwise.
public static List<Vector2f> getEquidistantPointsInsideCircle(Vector2f center, float radius, float spaceBetweenPoints)
List
of evenly spaced Vector2f
s 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 Vector2f
s
created!
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.
List
of evenly spaced Vector2f
s inside a
circle.
public static float getRandomNumberInRange(float min, float max)
min
- The minimum value to select.max
- The maximum value to select.
Float
between min
and max
.
public static int getRandomNumberInRange(int min, int max)
min
- The minimum value to select.max
- The maximum value to select (inclusive).
Integer
between min
and max
,
inclusive.
public static boolean equals(float a, float b)
a
- The first float to compare.b
- The second float to compare.
true
if a
and b
are within 99.99999%
of each other, false
otherwise.public static Random getRandom()
Random
instance used by LazyLib.