public class MathUtils extends Object
Modifier and Type | Method and Description |
---|---|
static float |
clamp(float toClamp,
float min,
float max)
Clamps a
Float , ensuring it falls within a given range. |
static int |
clamp(int toClamp,
int min,
int max)
Clamps an
Integer , ensuring it falls within a given range. |
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, taking
collision radii into account. |
static float |
getDistance(CombatEntityAPI entity,
Vector2f loc)
Returns the distance between a
CombatEntityAPI and a
Vector2f , taking collision radius into account. |
static float |
getDistance(SectorEntityToken token1,
SectorEntityToken token2)
Returns the distance between two
SectorEntityToken s,
taking interaction radii into account. |
static float |
getDistance(SectorEntityToken token,
Vector2f loc)
Returns the distance between a
SectorEntityToken and a
Vector2f , taking interaction radius into account. |
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,
taking collision radii into account. |
static float |
getDistanceSquared(CombatEntityAPI entity,
Vector2f loc)
Returns the distance squared between a
CombatEntityAPI and a
Vector2f , taking collision radius into account. |
static float |
getDistanceSquared(SectorEntityToken token1,
SectorEntityToken token2)
Returns the distance squared between two
SectorEntityToken s,
taking interaction radii into account. |
static float |
getDistanceSquared(SectorEntityToken token,
Vector2f loc)
Returns the distance squared between a
SectorEntityToken and
a Vector2f , taking interaction radius into account. |
static float |
getDistanceSquared(Vector2f loc1,
Vector2f loc2)
Returns the distance squared between two
Vector2f s (avoids a
costly sqrt()). |
static List<Vector2f> |
getEquidistantPointsInsideCircle(@Nullable Vector2f center,
float radius,
float spaceBetweenPoints)
|
static Vector2f |
getMidpoint(Vector2f point1,
Vector2f point2)
Returns the point exactly between two other points.
|
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.
|
static Vector2f |
getPoint(@Nullable Vector2f center,
float radius,
float angle)
Returns a point along the circumference of a circle at the given angle.
|
static Vector2f |
getPointOnCircumference(@Nullable Vector2f center,
float radius,
float angle)
Returns a point along the circumference of a circle at the given angle.
|
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. |
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(@Nullable Vector2f center,
float radius)
Returns a random point inside of a circle with uniform distribution.
|
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.
|
static Vector2f |
getRandomPointOnCircumference(@Nullable 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,
@Nullable 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, taking collision radii into account. |
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. |
static boolean |
isWithinRange(SectorEntityToken token1,
SectorEntityToken token2,
float range)
Check if two
SectorEntityToken s are within a certain distance of
each other, taking interaction radii into account. |
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. |
static boolean |
isWithinRange(Vector2f loc1,
Vector2f loc2,
float range)
Check if two objects are within a certain distance of each other.
|
public static final float FPI
public static final float FTAU
public static float getDistance(SectorEntityToken token1, SectorEntityToken token2)
SectorEntityToken
s,
taking interaction radii into account.getDistance(Vector2f, Vector2f)
public static float getDistance(SectorEntityToken token, Vector2f loc)
SectorEntityToken
and a
Vector2f
, taking interaction radius into account.getDistance(Vector2f, Vector2f)
public static float getDistance(CombatEntityAPI entity1, CombatEntityAPI entity2)
CombatEntityAPI
s, taking
collision radii into account.getDistance(Vector2f, Vector2f)
public static float getDistance(CombatEntityAPI entity, Vector2f loc)
CombatEntityAPI
and a
Vector2f
, taking collision radius into account.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,
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.
getDistanceSquared(Vector2f, Vector2f)
public static float getDistanceSquared(SectorEntityToken token, Vector2f loc)
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.
getDistanceSquared(Vector2f, Vector2f)
public static float getDistanceSquared(CombatEntityAPI entity1, CombatEntityAPI entity2)
CombatEntityAPI
s,
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.
getDistanceSquared(Vector2f, Vector2f)
public static float getDistanceSquared(CombatEntityAPI entity, Vector2f loc)
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.
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, taking interaction radii into account.
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, taking interaction radius into account.
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, taking collision radii into account.
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, taking collision radius into account.
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 clamp(float toClamp, float min, float max)
Float
, ensuring it falls within a given range.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.toClamp
if it falls between min
and max
,
min
if it falls below, or max
if it falls above.public static int clamp(int toClamp, int min, int max)
Integer
, ensuring it falls within a given range.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.toClamp
if it falls between min
and max
,
min
if it falls below, or max
if it falls above.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(@Nullable @Nullable 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 getPoint(@Nullable @Nullable Vector2f center, float radius, float angle)
getPointOnCircumference(Vector2f, float, float)
.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 angle
degrees along the
circumference of the given circle.getPointOnCircumference(Vector2f, float, float)
public static Vector2f getRandomPointOnCircumference(@Nullable @Nullable 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(@Nullable @Nullable 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(@Nullable @Nullable 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(@Nullable @Nullable 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, @Nullable @Nullable 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 Vector2f getNearestPointOnLine(Vector2f source, Vector2f lineStart, Vector2f lineEnd)
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.lineStart
and lineEnd
nearest to source
.public static List<Vector2f> getEquidistantPointsInsideCircle(@Nullable @Nullable Vector2f center, float radius, float spaceBetweenPoints)
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.