Package de.gurkenlabs.litiengine.physics
Class PhysicsEngine
java.lang.Object
de.gurkenlabs.litiengine.physics.PhysicsEngine
- All Implemented Interfaces:
IUpdateable
public final class PhysicsEngine
extends java.lang.Object
implements IUpdateable
This class is used to hold all collision aware instances and static collision boxes.
It is responsible for resolving movement that respects the collision boxes in the game. This is achieved by the
The
move
method
group. The
collides
method group can detect a collision at a certain location, for rectangles, or collision aware entities.
Also, there's an overload that takes a Line2D
to perform a basic raycast check.Constructor Summary
Constructors Constructor Description PhysicsEngine()
You should never call this manually! Instead use theGame.physics()
instance.Method Summary
Modifier and Type Method Description void
add(ICollisionEntity entity)
Adds the specified collision aware entity to the physics engine which will make it respect the entity's collision box for upcoming calls.void
clear()
Clears all previously registered participants in the collision process from this instance.boolean
collides(double x, double y)
Checks if the point at the given coordinates collides with anything registered in thePhysicsEngine
.boolean
collides(double x, double y, ICollisionEntity entity)
Checks if a givenICollisionEntity
collides with anything that intersects specific coordinates.boolean
collides(double x, double y, Collision collision)
Checks if the point at the given coordinates collides with anything of the givenCollision
type.boolean
collides(ICollisionEntity entity)
Checks if a givenICollisionEntity
collides with anything registered in thePhysicsEngine
.boolean
collides(ICollisionEntity entity, Collision collision)
Checks if a givenICollisionEntity
collides with anything of the givenCollision
type.boolean
collides(java.awt.geom.Line2D line)
Checks if a given line collides with anything registered in thePhysicsEngine
.boolean
collides(java.awt.geom.Line2D line, ICollisionEntity entity)
Checks if a givenICollisionEntity
collides with anything that intersects a specific line.boolean
collides(java.awt.geom.Line2D line, Collision collision)
Checks if a line collides with anything of the givenCollision
type.boolean
collides(java.awt.geom.Line2D line, Collision collision, ICollisionEntity entity)
Checks if a givenICollisionEntity
collides with anyICollisionEntities
of a givenCollision
type that intersect a specific line.boolean
collides(java.awt.geom.Point2D location)
Checks if a given point collides with anything registered in thePhysicsEngine
.boolean
collides(java.awt.geom.Point2D location, ICollisionEntity entity)
Checks if a givenICollisionEntity
collides with anything that intersects a specific point.boolean
collides(java.awt.geom.Point2D location, Collision collision)
Checks if a point collides with anything of the givenCollision
type.boolean
collides(java.awt.geom.Point2D location, Collision collision, ICollisionEntity entity)
Checks if a givenICollisionEntity
collides with anyICollisionEntities
of a givenCollision
type that intersect a specific point.boolean
collides(java.awt.geom.Rectangle2D rect)
Checks if a given rectangle collides with anything registered in thePhysicsEngine
.boolean
collides(java.awt.geom.Rectangle2D rect, ICollisionEntity entity)
Checks if a givenICollisionEntity
collides with anything that intersects a specific rectangle.boolean
collides(java.awt.geom.Rectangle2D rect, Collision collision)
Checks if a rectangle collides with anything of the givenCollision
type.boolean
collides(java.awt.geom.Rectangle2D rect, Collision collision, ICollisionEntity entity)
Checks if a givenICollisionEntity
collides with anyICollisionEntities
of a givenCollision
type that intersect a specific rectangle.java.awt.geom.Rectangle2D
getBounds()
Gets the environment bounds that confine the operation area of thePhysicsEngine
.java.util.Collection<java.awt.geom.Rectangle2D>
getCollisionBoxes()
Gets allCollisionBoxes
, regardless of theirCollision
type.java.util.Collection<java.awt.geom.Rectangle2D>
getCollisionBoxes(Collision type)
Gets allCollisionBoxes
with the givenCollision
type.java.util.Collection<ICollisionEntity>
getCollisionEntities()
Gets allICollisionEntities
, regardless of theirCollision
type.java.util.Collection<ICollisionEntity>
getCollisionEntities(Collision type)
Gets allICollisionEntities
with the givenCollision
type.boolean
move(IMobileEntity entity, double angle, double distance)
Moves the specified entity by a given distance and angle.boolean
move(IMobileEntity entity, double x, double y, float distance)
Moves the specified entity by a given distance towards the target coordinates.boolean
move(IMobileEntity entity, float distance)
Moves the specified entity by a given distance and the entity's angle.boolean
move(IMobileEntity entity, Direction direction, double distance)
Moves the specified entity by a given distance and angle.boolean
move(IMobileEntity entity, java.awt.geom.Point2D target)
Moves the specified entity to a target point.boolean
move(IMobileEntity entity, java.awt.geom.Point2D target, float distance)
Moves the specified entity by a given distance towards the target coordinates.RaycastHit
raycast(java.awt.geom.Line2D line)
Cast a ray along a given line [from (x1,y1) to (x2,y2)] and see if it hits anything.RaycastHit
raycast(java.awt.geom.Line2D line, ICollisionEntity entity)
Cast a ray along a given line [from (x1,y1) to (x2,y2)] and see if it hits a givenICollisionEntity
.RaycastHit
raycast(java.awt.geom.Line2D line, Collision collision)
Cast a ray along a given line [from (x1,y1) to (x2,y2)] and see if it hits anything with the givenCollision
type.RaycastHit
raycast(java.awt.geom.Line2D line, Collision collision, ICollisionEntity entity)
Cast a ray along a given line [from (x1,y1) to (x2,y2)] and see if it hits anything with a certainCollision
type that collides with the givenICollisionEntity
.RaycastHit
raycast(java.awt.geom.Point2D start, double angle)
From a given point, cast a ray of indefinite length with the given angle and see if it hits anything.RaycastHit
raycast(java.awt.geom.Point2D start, java.awt.geom.Point2D target)
From a given point, cast a ray to another point and see if it hits anything.RaycastHit
raycast(java.awt.geom.Point2D start, java.awt.geom.Point2D target, Collision collision)
From a given point, cast a ray to another point and see if it hits anything with the givenCollision
type.void
remove(ICollisionEntity entity)
Removes the specified entity from any collision processing.void
setBounds(java.awt.geom.Rectangle2D environmentBounds)
Sets the environment bounds that confine the operation area of thePhysicsEngine
.void
update()
Clears all collision boxes registered on thePhysicsEngine
once per tick and re-adds them with their updated positions.
Constructor Details
PhysicsEngine
public PhysicsEngine()You should never call this manually! Instead use the
Game.physics()
instance.- See Also:
Game.physics()
Method Details
add
Adds the specified collision aware entity to the physics engine which will make it respect the entity's collision box for upcoming calls.If you add a
ICollisionEntiy
to your Environment, it will automatically be added to the the PhysicsEngine. There is typically no need to call this explicitly.- Parameters:
entity
- The collision entity to be added.- See Also:
ICollisionEntity.getCollisionBox()
,remove(ICollisionEntity)
remove
Removes the specified entity from any collision processing. Typically this method is implicitly called when an entity is removed from the current environment.- Parameters:
entity
- The entity that is about to be removed.
clear
public void clear()Clears all previously registered participants in the collision process from this instance. This includes all entities, static collision boxes and the map boundaries.getCollisionBoxes
public java.util.Collection<java.awt.geom.Rectangle2D> getCollisionBoxes()Gets allCollisionBoxes
, regardless of theirCollision
type.- Returns:
- A
Collection
of allCollisionBox
es registered on thePhysicsEngine
.
getCollisionBoxes
Gets allCollisionBoxes
with the givenCollision
type.- Parameters:
type
- TheCollision
type by which theCollisionBoxes
are selected.- Returns:
- If the
Collision
type isNONE
, return an empty set. Otherwise, aCollection
of allCollisionBoxes
registered on thePhysicsEngine
that have the givenCollision
type.
getCollisionEntities
Gets allICollisionEntities
, regardless of theirCollision
type.- Returns:
- A
Collection
of allICollisionEntities
registered on thePhysicsEngine
.
getCollisionEntities
Gets allICollisionEntities
with the givenCollision
type.- Parameters:
type
- TheCollision
type by which theICollisionEntities
are selected.- Returns:
- If the
Collision
type isNONE
, return an empty set. Otherwise, aCollection
of allICollisionEntities
registered on thePhysicsEngine
that have the givenCollision
type.
getBounds
public java.awt.geom.Rectangle2D getBounds()Gets the environment bounds that confine the operation area of thePhysicsEngine
.- Returns:
- The
Rectangle2D
confining the operation area of thePhysicsEngine
.
setBounds
public void setBounds(java.awt.geom.Rectangle2D environmentBounds)Sets the environment bounds that confine the operation area of thePhysicsEngine
.- Parameters:
environmentBounds
- TheRectangle2D
confining the operation area of thePhysicsEngine
.
collides
public boolean collides(java.awt.geom.Line2D line)Checks if a given line collides with anything registered in thePhysicsEngine
.- Parameters:
line
- TheLine2D
to check for collision.- Returns:
true
if the line collides with anything.false
otherwise.
collides
Checks if a line collides with anything of the givenCollision
type.- Parameters:
line
- TheLine2D
to check for collision.collision
- TheCollision
type to check for collisions.- Returns:
true
if the line collides with anything of the givenCollision
type.false
otherwise.- See Also:
Collision
collides
Checks if a givenICollisionEntity
collides with anything that intersects a specific line.- Parameters:
line
- TheLine2D
to check for collision.entity
- TheICollisionEntity
to check for collision.- Returns:
true
if anyICollisionEntity
intersecting the line collides with the givenICollisionEntity
.false
otherwise.- See Also:
Collision
,ICollisionEntity
collides
Checks if a givenICollisionEntity
collides with anyICollisionEntities
of a givenCollision
type that intersect a specific line.- Parameters:
line
- TheLine2D
to check for collision.collision
- TheCollision
type to check for collision.entity
- TheICollisionEntity
to check for collision.- Returns:
true
if the entity collides with anyICollisionEntity
on the given line.false
otherwise.- See Also:
Collision
,ICollisionEntity
collides
public boolean collides(java.awt.geom.Rectangle2D rect)Checks if a given rectangle collides with anything registered in thePhysicsEngine
.- Parameters:
rect
- TheRectangle2D
to check for collision.- Returns:
true
if the rectangle collides with anything.false
otherwise.
collides
Checks if a givenICollisionEntity
collides with anything that intersects a specific rectangle.- Parameters:
rect
- TheRectangle2D
to check for collision.entity
- TheICollisionEntity
to check for collision.- Returns:
true
if the entity collides with anyICollisionEntity
in the given rectangle.false
otherwise.- See Also:
ICollisionEntity
collides
Checks if a rectangle collides with anything of the givenCollision
type.- Parameters:
rect
- TheRectangle2D
to check for collision.collision
- TheCollision
type to check for collisions.- Returns:
true
if the rectangle collides with anything of the givenCollision
type.false
otherwise.- See Also:
Collision
collides
public boolean collides(java.awt.geom.Rectangle2D rect, Collision collision, ICollisionEntity entity)Checks if a givenICollisionEntity
collides with anyICollisionEntities
of a givenCollision
type that intersect a specific rectangle.- Parameters:
rect
- TheRectangle2D
to check for collision.collision
- TheCollision
type to check for collision.entity
- TheICollisionEntity
to check for collision.- Returns:
true
if the entity collides with anyICollisionEntity
in the given rectangle.false
otherwise.- See Also:
Collision
,ICollisionEntity
collides
public boolean collides(java.awt.geom.Point2D location)Checks if a given point collides with anything registered in thePhysicsEngine
.- Parameters:
location
- ThePoint2D
to check for collision.- Returns:
true
if the point collides with anything.false
otherwise.
collides
Checks if a point collides with anything of the givenCollision
type.- Parameters:
location
- ThePoint2D
to check for collision.collision
- TheCollision
type to check for collisions.- Returns:
true
if the point collides with anything of the givenCollision
type.false
otherwise.- See Also:
Collision
collides
Checks if a givenICollisionEntity
collides with anything that intersects a specific point.- Parameters:
location
- ThePoint2D
to check for collision.entity
- TheICollisionEntity
to check for collision.- Returns:
true
if the entity collides with anyICollisionEntity
on the given point.false
otherwise.- See Also:
ICollisionEntity
collides
public boolean collides(java.awt.geom.Point2D location, Collision collision, ICollisionEntity entity)Checks if a givenICollisionEntity
collides with anyICollisionEntities
of a givenCollision
type that intersect a specific point.- Parameters:
location
- ThePoint2D
to check for collision.collision
- TheCollision
type to check for collision.entity
- TheICollisionEntity
to check for collision.- Returns:
true
if the entity collides with anyICollisionEntity
on the given point.false
otherwise.- See Also:
Collision
,ICollisionEntity
collides
public boolean collides(double x, double y)Checks if the point at the given coordinates collides with anything registered in thePhysicsEngine
.- Parameters:
x
- The x coordinate to check for collision.y
- The y coordinate to check for collision.- Returns:
true
if the coordinates collide with anything.false
otherwise.
collides
Checks if the point at the given coordinates collides with anything of the givenCollision
type.- Parameters:
x
- The x coordinate to check for collision.y
- The y coordinate to check for collision.collision
- TheCollision
type to check for collisions.- Returns:
true
if the coordinates collide with anything of the givenCollision
type.false
otherwise.- See Also:
Collision
collides
Checks if a givenICollisionEntity
collides with anything that intersects specific coordinates.- Parameters:
x
- The x coordinate to check for collision.y
- The y coordinate to check for collision.entity
- TheICollisionEntity
to check for collision.- Returns:
true
if the entity collides with anyICollisionEntity
on the given coordinates.false
otherwise.- See Also:
ICollisionEntity
collides
Checks if a givenICollisionEntity
collides with anything registered in thePhysicsEngine
.- Parameters:
entity
- TheICollisionEntity
to check for collision.- Returns:
true
if the entity collides with any otherICollisionEntity
.false
otherwise.- See Also:
ICollisionEntity
collides
Checks if a givenICollisionEntity
collides with anything of the givenCollision
type.- Parameters:
entity
- TheICollisionEntity
to check for collision.collision
- TheCollision
type to check for collisions.- Returns:
true
if the entity collides with anything of the givenCollision
type.false
otherwise.- See Also:
Collision
raycast
From a given point, cast a ray of indefinite length with the given angle and see if it hits anything.- Parameters:
start
- The start point of the raycast.angle
- The angle in degrees.- Returns:
- A
RaycastHit
determining the hit point, ray length, and correspondingICollisionEntity
, if the ray hit something.
raycast
From a given point, cast a ray to another point and see if it hits anything.- Parameters:
start
- The start point of the raycast.target
- The end point of the raycast.- Returns:
- A
RaycastHit
determining the hit point, ray length, and correspondingICollisionEntity
, if the ray hit something.
raycast
public RaycastHit raycast(java.awt.geom.Point2D start, java.awt.geom.Point2D target, Collision collision)From a given point, cast a ray to another point and see if it hits anything with the givenCollision
type.- Parameters:
start
- The start point of the raycast.target
- The end point of the raycast.collision
- TheCollision
type to check for collision.- Returns:
- A
RaycastHit
determining the hit point, ray length, and correspondingICollisionEntity
, if the ray hit something.
raycast
Cast a ray along a given line [from (x1,y1) to (x2,y2)] and see if it hits anything.- Parameters:
line
- The line along which the ray is cast.- Returns:
- A
RaycastHit
determining the hit point, ray length, and correspondingICollisionEntity
, if the ray hit something.
raycast
Cast a ray along a given line [from (x1,y1) to (x2,y2)] and see if it hits anything with the givenCollision
type.- Parameters:
line
- The line along which the ray is cast.collision
- TheCollision
type to check for collision.- Returns:
- A
RaycastHit
determining the hit point, ray length, and correspondingICollisionEntity
, if the ray hit something.
raycast
Cast a ray along a given line [from (x1,y1) to (x2,y2)] and see if it hits a givenICollisionEntity
.- Parameters:
line
- The line along which the ray is cast.entity
- TheICollisionEntity
type to check for collision.- Returns:
- A
RaycastHit
determining the hit point, ray length, and correspondingICollisionEntity
.
raycast
Cast a ray along a given line [from (x1,y1) to (x2,y2)] and see if it hits anything with a certainCollision
type that collides with the givenICollisionEntity
.- Parameters:
line
- The line along which the ray is cast.collision
- TheCollision
type to check for collision.entity
- TheICollisionEntity
type to check for collision.- Returns:
- A
RaycastHit
determining the hit point, ray length, and correspondingICollisionEntity
.
move
Moves the specified entity by a given distance and angle.- Parameters:
entity
- The entity which is movedangle
- The angle in degreesdistance
- The distance to move the entity- Returns:
true
, if the entity can be moved without colliding, otherwisefalse
.- See Also:
GeometricUtilities.project(Point2D, double, double)
move
Moves the specified entity by a given distance and angle.- Parameters:
entity
- TheIMobileEntity
which is moveddirection
- TheDirection
in which the entity is moveddistance
- The distance to move the entity- Returns:
true
, if the entity can be moved without colliding, otherwisefalse
.- See Also:
Direction
move
Moves the specified entity by a given distance towards the target coordinates.- Parameters:
entity
- TheIMobileEntity
which is movedx
- The target x coordinatey
- The target y coordinatedistance
- The distance to move the entity- Returns:
true
, if the entity can be moved without colliding, otherwisefalse
.
move
Moves the specified entity by a given distance and the entity's angle.- Parameters:
entity
- TheIMobileEntity
which is moveddistance
- The distance to move the entity- Returns:
true
, if the entity can be moved without colliding, otherwisefalse
.- See Also:
Direction
move
Moves the specified entity to a target point. Ifentity.turnOnMove()
istrue
, set the entity's angle towards the target.- Parameters:
entity
- TheIMobileEntity
which is movedtarget
- The target point- Returns:
true
, if the entity can be moved without colliding, otherwisefalse
.- See Also:
resolveCollisionForNewPosition(de.gurkenlabs.litiengine.entities.ICollisionEntity, java.awt.geom.Point2D)
move
Moves the specified entity by a given distance towards the target coordinates. Ifentity.turnOnMove()
istrue
, set the entity's angle towards the target.- Parameters:
entity
- TheIMobileEntity
which is movedtarget
- The target pointdistance
- The distance to move the entity- Returns:
true
, if the entity can be moved without colliding, otherwisefalse
.- See Also:
resolveCollisionForNewPosition(de.gurkenlabs.litiengine.entities.ICollisionEntity, java.awt.geom.Point2D)
update
public void update()Clears all collision boxes registered on thePhysicsEngine
once per tick and re-adds them with their updated positions.- Specified by:
update
in interfaceIUpdateable
- See Also:
ClientConfiguration.setMaxFps(int)