Package de.extio.spacecraft.shared.world
Class WorldUtils
- java.lang.Object
-
- de.extio.spacecraft.shared.world.WorldUtils
-
public final class WorldUtils extends Object
Library for geometric problems
-
-
Constructor Summary
Constructors Constructor Description WorldUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
calcEdgeRequirement(CoordI2 c0, CoordI2 c1)
Calculates edge required at c1 to block the path from c0 to c1.static List<CoordI2>
evade(HasPositionAndDimension2 origin, double velOrigin, CoordD2 vOrigin, HasPositionAndDimension2 target, CoordD2 vTarget, CoordI2 oTarget, CoordI2 pInterception_)
Calculates waypoints to evade a collision between origin and target.static CoordI2
getClosestPoint(CoordI2 origin, HasPositionAndDimension2 target)
Get's the closest point at area target from originstatic double
getDistance(CoordD2 v)
The Pythagorean theoremstatic double
getDistance(CoordD2 c0, CoordD2 c1)
The Pythagorean theoremstatic double
getDistance(CoordI2 v)
The Pythagorean theoremstatic double
getDistance(CoordI2 c0, CoordI2 c1)
The Pythagorean theoremstatic double
getDistance(HasPosition2 p0, HasPosition2 p1)
The Pythagorean theoremstatic CoordD2
getPedal(CoordI2 a, CoordI2 b, CoordI2 c)
Calculates the pedal of altitude c of triangle A.B,Cstatic CoordD2
getPedalOnLineSegmentAB(CoordI2 a, CoordI2 b, CoordI2 c)
Get's the pedal of altitude c of triangle A.B,C or the closest point on line segment a,bstatic CoordI2
getRandomPointInCircle(CoordI2 center, int radius)
Returns a random point in a circlestatic CoordI2
getRandomPointInCircle(CoordI2 center, int radius, Random random)
Returns a point in a circlestatic CoordI2
getRandomPointOnCircle(CoordI2 center, int radius)
Returns a random point on a circlestatic CoordI2
getRandomPointOnCircle(CoordI2 center, int radius, Random random)
Returns a random point on a circlestatic CoordI2
getReproduciblePointOnCircle(CoordI2 center, int radius, AtomicInteger counter)
Returns a reproducible point on a circlestatic Edge2
getSector(CoordI2 point, HasPositionAndDimension2 source)
Returns the sector the point is located from source's viewstatic List<Area2>
getSides(HasPositionAndDimension2 obj)
x-----x | | | | x-----xstatic double
getVectorAngle(CoordD2 v)
Calculate the direction of a vectorstatic double
getVectorAngle(CoordD2 v0, CoordD2 v1)
Calculate the angle between 2 vectorsstatic double
getVectorAngle(CoordI2 v)
Calculate the direction of a vectorstatic double
getVectorAngle(CoordI2 v0, CoordI2 v1)
Calculate the angle between 2 vectorsstatic double
getVectorDotProduct(CoordD2 v0, CoordD2 v1)
Calculate the dot productstatic List<CoordI2>
getVertices(HasPositionAndDimension2 obj)
x-----x | | | | x-----xstatic CoordI2
intercept(CoordI2 pOrigin_, double velOrigin, CoordI2 pTarget_, CoordD2 vTarget_)
Calculates the point of intersection to intercept target from origin with velocity velOriginstatic Area2
intersectAreas(HasPositionAndDimension2 a0, HasPositionAndDimension2 a1)
Intersects 2 areasstatic RectI2
intersectRectangles(RectI2 r0, RectI2 r1)
Intersects 2 recanglesstatic boolean
intersects(CoordI2 position0, CoordI2 dimension0, CoordI2 position1, CoordI2 dimension1)
Checks whether 2 objects intersectstatic boolean
intersects(HasPositionAndDimension2 obj0, HasPositionAndDimension2 obj1)
Checks whether 2 objects intersectstatic boolean
isInBounds(CoordI2 coord, CoordI2 dimension)
Checks whether coord is within bounds (e.g.static boolean
isInBounds(HasPosition2 hasPosition, CoordI2 dimension)
Checks whether coord is within bounds (e.g.static Area2
pointsToArea(CoordI2... points)
Builds an aligned Area2 from arbitrary pointsstatic CoordD2
rotateVector(CoordD2 v, double θ)
Rotates a vectorstatic int
strictSignum(double n)
Signum function that strictly returns -1 or 1.static int
strictSignum(int n)
Signum function that strictly returns -1 or 1.static CoordI2
translateEntityPosToWorldPos(AbstractCompositeEntity compositeEntity, CoordI2 entityPos)
Translates coordinate within composite entity to absolute world coordinatestatic CoordI2
translateWorldPosToEntityPos(AbstractCompositeEntity compositeEntity, CoordI2 worldPos, boolean ignoreBounds)
Translates world coordinate to relative coordinate within composite entity
-
-
-
Method Detail
-
getDistance
public static double getDistance(CoordI2 v)
The Pythagorean theorem- Parameters:
v
- Vector- Returns:
- Length of vector v
-
getDistance
public static double getDistance(CoordD2 v)
The Pythagorean theorem- Parameters:
v
- Vector- Returns:
- Length of vector v
-
getDistance
public static double getDistance(HasPosition2 p0, HasPosition2 p1)
The Pythagorean theorem- Parameters:
p0
- Coordinate 0p1
- Coordinate 1- Returns:
- Distance
-
getDistance
public static double getDistance(CoordI2 c0, CoordI2 c1)
The Pythagorean theorem- Parameters:
c0
- Coordinate 0c1
- Coordinate 1- Returns:
- Distance
-
getDistance
public static double getDistance(CoordD2 c0, CoordD2 c1)
The Pythagorean theorem- Parameters:
c0
- Coordinate 0c1
- Coordinate 1- Returns:
- Distance
-
getVectorAngle
public static double getVectorAngle(CoordI2 v0, CoordI2 v1)
Calculate the angle between 2 vectors- Parameters:
v0
- Vector 0v1
- Vector 1- Returns:
- Angle (radians)
-
getVectorAngle
public static double getVectorAngle(CoordD2 v0, CoordD2 v1)
Calculate the angle between 2 vectors- Parameters:
v0
- Vector 0v1
- Vector 1- Returns:
- Angle (radians)
-
getVectorAngle
public static double getVectorAngle(CoordI2 v)
Calculate the direction of a vector- Parameters:
v0
- Vector 0v1
- Vector 1- Returns:
- Angle (radians)
-
getVectorAngle
public static double getVectorAngle(CoordD2 v)
Calculate the direction of a vector- Parameters:
v0
- Vector 0v1
- Vector 1- Returns:
- Angle (radians)
-
getVectorDotProduct
public static double getVectorDotProduct(CoordD2 v0, CoordD2 v1)
Calculate the dot product- Parameters:
v0
- Vector 0v1
- Vector 1- Returns:
- Dot product
-
rotateVector
public static CoordD2 rotateVector(CoordD2 v, double θ)
Rotates a vector- Returns:
- Rotated vector
-
isInBounds
public static boolean isInBounds(HasPosition2 hasPosition, CoordI2 dimension)
Checks whether coord is within bounds (e.g. composite entity)- Parameters:
hasPosition
-- Returns:
-
isInBounds
public static boolean isInBounds(CoordI2 coord, CoordI2 dimension)
Checks whether coord is within bounds (e.g. composite entity)- Parameters:
coord
-- Returns:
-
strictSignum
public static int strictSignum(int n)
Signum function that strictly returns -1 or 1.- Parameters:
n
-- Returns:
- Mathematical signum function that returns -1 if n is negative and 1 if n is positive (including 0)
-
strictSignum
public static int strictSignum(double n)
Signum function that strictly returns -1 or 1.- Parameters:
n
-- Returns:
- Mathematical signum function that returns -1 if n is negative and 1 if n is positive (including 0)
-
intersectRectangles
public static RectI2 intersectRectangles(RectI2 r0, RectI2 r1)
Intersects 2 recangles- Parameters:
r0
- Rectangle 0r1
- Rectangle 1- Returns:
- The intersected area if r0 and r1 overlap, else null
-
intersectAreas
public static Area2 intersectAreas(HasPositionAndDimension2 a0, HasPositionAndDimension2 a1)
Intersects 2 areas- Parameters:
r0
- Area 0r1
- Area 1- Returns:
- The intersected area if r0 and r1 overlap, else null
-
intersects
public static boolean intersects(HasPositionAndDimension2 obj0, HasPositionAndDimension2 obj1)
Checks whether 2 objects intersect- Parameters:
obj0
- HasPositionAndDimension2 object 0obj1
- HasPositionAndDimension2 object 1- Returns:
- true / false whether obj0 intersects with obj1
-
intersects
public static boolean intersects(CoordI2 position0, CoordI2 dimension0, CoordI2 position1, CoordI2 dimension1)
Checks whether 2 objects intersect- Parameters:
position0
- position of object 0dimension0
- dimension of object 0position1
- position of object 1dimension1
- dimension of object 1- Returns:
- true / false whether obj0 intersects with obj1
-
translateWorldPosToEntityPos
public static CoordI2 translateWorldPosToEntityPos(AbstractCompositeEntity compositeEntity, CoordI2 worldPos, boolean ignoreBounds)
Translates world coordinate to relative coordinate within composite entity- Parameters:
compositeEntity
-worldPos
-- Returns:
- Relative entity coordinate, null if coordinate is outside bounds of the entity
-
translateEntityPosToWorldPos
public static CoordI2 translateEntityPosToWorldPos(AbstractCompositeEntity compositeEntity, CoordI2 entityPos)
Translates coordinate within composite entity to absolute world coordinate- Parameters:
compositeEntity
-entityPos
-- Returns:
- Coordinate of entityPos in the world
-
pointsToArea
public static Area2 pointsToArea(CoordI2... points)
Builds an aligned Area2 from arbitrary points- Returns:
-
intercept
public static CoordI2 intercept(CoordI2 pOrigin_, double velOrigin, CoordI2 pTarget_, CoordD2 vTarget_)
Calculates the point of intersection to intercept target from origin with velocity velOrigin- Parameters:
pOrigin_
- Position of origin (e.g. ship that intercepts target)velOrigin
- Velocity of originpTarget_
- Position of targetvTarget_
- Velocity vector of target- Returns:
- Point of interception or null if interception is not possible
-
getSector
public static Edge2 getSector(CoordI2 point, HasPositionAndDimension2 source)
Returns the sector the point is located from source's view- Parameters:
point
-source
-- Returns:
- Sector (Edge2)
-
getPedal
public static CoordD2 getPedal(CoordI2 a, CoordI2 b, CoordI2 c)
Calculates the pedal of altitude c of triangle A.B,C- Parameters:
a
- Point Ab
- Point Bc
- Point C- Returns:
- Pedal of altitude c
-
getPedalOnLineSegmentAB
public static CoordD2 getPedalOnLineSegmentAB(CoordI2 a, CoordI2 b, CoordI2 c)
Get's the pedal of altitude c of triangle A.B,C or the closest point on line segment a,ba-----p2-------b p1 | \ | | \ | | \| c2 c1
Returns null if there is no pedal.
-
getClosestPoint
public static CoordI2 getClosestPoint(CoordI2 origin, HasPositionAndDimension2 target)
Get's the closest point at area target from origin*-----* | | | x _______ origin | | *-----*
- Parameters:
origin
-target
-- Returns:
-
evade
public static List<CoordI2> evade(HasPositionAndDimension2 origin, double velOrigin, CoordD2 vOrigin, HasPositionAndDimension2 target, CoordD2 vTarget, CoordI2 oTarget, CoordI2 pInterception_)
Calculates waypoints to evade a collision between origin and target.- Parameters:
pOrigin
- OriginvelOrigin
- Origin velocityvOrigin
- Origin velocity vector, origin course not necessarily the current velocity vectorpTarget
- TargetvTarget
- Target velocity vectoroTarget
- Collision offset on target (entityPos)pInterception
- (Optional) Point of interception. Can be null, in this case it is calculated based on the velocity of source and the velocity vector of target.- Returns:
- Waypoints for evasion or null if there is no collision
-
getVertices
public static List<CoordI2> getVertices(HasPositionAndDimension2 obj)
x-----x | | | | x-----x
Returns the 4 vertices of a rectangle- Parameters:
obj
- Rectangle- Returns:
- 4 vertices ordered clock-wise
-
getSides
public static List<Area2> getSides(HasPositionAndDimension2 obj)
x-----x | | | | x-----x
Returns the 4 sides of a rectangle- Parameters:
obj
- Rectangle- Returns:
- 4 sides ordered clock-wise
-
getRandomPointInCircle
public static CoordI2 getRandomPointInCircle(CoordI2 center, int radius)
Returns a random point in a circle- Parameters:
center
- of the circleradius
- of the circle- Returns:
- random point in the circle
-
getRandomPointInCircle
public static CoordI2 getRandomPointInCircle(CoordI2 center, int radius, Random random)
Returns a point in a circle- Parameters:
center
- of the circleradius
- of the circlerandom
- rng- Returns:
- random point in the circle
-
getRandomPointOnCircle
public static CoordI2 getRandomPointOnCircle(CoordI2 center, int radius)
Returns a random point on a circle- Parameters:
center
- of the circleradius
- of the circle- Returns:
- random point in the circle
-
getRandomPointOnCircle
public static CoordI2 getRandomPointOnCircle(CoordI2 center, int radius, Random random)
Returns a random point on a circle- Parameters:
center
- of the circleradius
- of the circlerandom
- rng- Returns:
- random point in the circle
-
getReproduciblePointOnCircle
public static CoordI2 getReproduciblePointOnCircle(CoordI2 center, int radius, AtomicInteger counter)
Returns a reproducible point on a circle- Parameters:
center
- of the circleradius
- of the circlecounter
- decides where the point is- Returns:
- random point in the circle
-
calcEdgeRequirement
public static int calcEdgeRequirement(CoordI2 c0, CoordI2 c1)
Calculates edge required at c1 to block the path from c0 to c1. Swap both parameters to get edge required at c0 to block the path from c1 to c0.
c0==> |c1
c0| <==c1- Parameters:
c0
- Positionc1
- Target- Returns:
- Edges that are required as bit mask
-
-