Package de.gurkenlabs.litiengine.physics
Class PhysicsEngine
java.lang.Object
de.gurkenlabs.litiengine.physics.PhysicsEngine
- All Implemented Interfaces:
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
ConstructorsConstructorDescriptionYou should never call this manually! Instead use theGame.physics()instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(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.voidclear()Clears all previously registered participants in the collision process from this instance.booleancollides(double x, double y) Checks if the point at the given coordinates collides with anything registered in thePhysicsEngine.booleancollides(double x, double y, ICollisionEntity entity) Checks if a givenICollisionEntitycollides with anything that intersects specific coordinates.booleanChecks if the point at the given coordinates collides with anything of the givenCollisiontype.booleancollides(ICollisionEntity entity) Checks if a givenICollisionEntitycollides with anything registered in thePhysicsEngine.booleancollides(ICollisionEntity entity, Collision collision) Checks if a givenICollisionEntitycollides with anything of the givenCollisiontype.booleanChecks if a given line collides with anything registered in thePhysicsEngine.booleancollides(Line2D line, ICollisionEntity entity) Checks if a givenICollisionEntitycollides with anything that intersects a specific line.booleanChecks if a line collides with anything of the givenCollisiontype.booleancollides(Line2D line, Collision collision, ICollisionEntity entity) Checks if a givenICollisionEntitycollides with anyICollisionEntitiesof a givenCollisiontype that intersect a specific line.booleanChecks if a given point collides with anything registered in thePhysicsEngine.booleancollides(Point2D location, ICollisionEntity entity) Checks if a givenICollisionEntitycollides with anything that intersects a specific point.booleanChecks if a point collides with anything of the givenCollisiontype.booleancollides(Point2D location, Collision collision, ICollisionEntity entity) Checks if a givenICollisionEntitycollides with anyICollisionEntitiesof a givenCollisiontype that intersect a specific point.booleancollides(Rectangle2D rect) Checks if a given rectangle collides with anything registered in thePhysicsEngine.booleancollides(Rectangle2D rect, ICollisionEntity entity) Checks if a givenICollisionEntitycollides with anything that intersects a specific rectangle.booleancollides(Rectangle2D rect, Collision collision) Checks if a rectangle collides with anything of the givenCollisiontype.booleancollides(Rectangle2D rect, Collision collision, ICollisionEntity entity) Checks if a givenICollisionEntitycollides with anyICollisionEntitiesof a givenCollisiontype that intersect a specific rectangle.Gets the environment bounds that confine the operation area of thePhysicsEngine.Gets allCollisionBoxes, regardless of theirCollisiontype.getCollisionBoxes(Collision type) Gets allCollisionBoxeswith the givenCollisiontype.Gets allICollisionEntities, regardless of theirCollisiontype.Gets allICollisionEntitieswith the givenCollisiontype.booleanmove(IMobileEntity entity, double angle, double distance) Moves the specified entity by a given distance and angle.booleanmove(IMobileEntity entity, double x, double y, float distance) Moves the specified entity by a given distance towards the target coordinates.booleanmove(IMobileEntity entity, float distance) Moves the specified entity by a given distance and the entity's angle.booleanmove(IMobileEntity entity, Direction direction, double distance) Moves the specified entity by a given distance and angle.booleanmove(IMobileEntity entity, Point2D target) Moves the specified entity to a target point.booleanmove(IMobileEntity entity, Point2D target, boolean turnTowardsTarget) Moves the specified entity to a target point.booleanmove(IMobileEntity entity, Point2D target, float distance) Moves the specified entity by a given distance towards the target coordinates.Cast a ray along a given line [from (x1,y1) to (x2,y2)] and see if it hits anything.raycast(Line2D line, ICollisionEntity entity) Cast a ray along a given line [from (x1,y1) to (x2,y2)] and see if it hits a givenICollisionEntity.Cast a ray along a given line [from (x1,y1) to (x2,y2)] and see if it hits anything with the givenCollisiontype.raycast(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 certainCollisiontype that collides with the givenICollisionEntity.From a given point, cast a ray of indefinite length with the given angle and see if it hits anything.From a given point, cast a ray to another point and see if it hits anything.From a given point, cast a ray to another point and see if it hits anything with the givenCollisiontype.voidremove(ICollisionEntity entity) Removes the specified entity from any collision processing.voidsetBounds(Rectangle2D environmentBounds) Sets the environment bounds that confine the operation area of thePhysicsEngine.voidupdate()Clears all collision boxes registered on thePhysicsEngineonce per tick and re-adds them with their updated positions.
-
Constructor Details
-
PhysicsEngine
public PhysicsEngine()You should never call this manually! Instead use theGame.physics()instance.- See Also:
-
-
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
ICollisionEntiyto 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:
-
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
Gets allCollisionBoxes, regardless of theirCollisiontype.- Returns:
- A
Collectionof allCollisionBoxes registered on thePhysicsEngine.
-
getCollisionBoxes
Gets allCollisionBoxeswith the givenCollisiontype.- Parameters:
type- TheCollisiontype by which theCollisionBoxesare selected.- Returns:
- If the
Collisiontype isNONE, return an empty set. Otherwise, aCollectionof allCollisionBoxesregistered on thePhysicsEnginethat have the givenCollisiontype.
-
getCollisionEntities
Gets allICollisionEntities, regardless of theirCollisiontype.- Returns:
- A
Collectionof allICollisionEntitiesregistered on thePhysicsEngine.
-
getCollisionEntities
Gets allICollisionEntitieswith the givenCollisiontype.- Parameters:
type- TheCollisiontype by which theICollisionEntitiesare selected.- Returns:
- If the
Collisiontype isNONE, return an empty set. Otherwise, aCollectionof allICollisionEntitiesregistered on thePhysicsEnginethat have the givenCollisiontype.
-
getBounds
Gets the environment bounds that confine the operation area of thePhysicsEngine.- Returns:
- The
Rectangle2Dconfining the operation area of thePhysicsEngine.
-
setBounds
Sets the environment bounds that confine the operation area of thePhysicsEngine.- Parameters:
environmentBounds- TheRectangle2Dconfining the operation area of thePhysicsEngine.
-
collides
Checks if a given line collides with anything registered in thePhysicsEngine.- Parameters:
line- TheLine2Dto check for collision.- Returns:
trueif the line collides with anything.falseotherwise.
-
collides
Checks if a line collides with anything of the givenCollisiontype.- Parameters:
line- TheLine2Dto check for collision.collision- TheCollisiontype to check for collisions.- Returns:
trueif the line collides with anything of the givenCollisiontype.falseotherwise.- See Also:
-
collides
Checks if a givenICollisionEntitycollides with anything that intersects a specific line.- Parameters:
line- TheLine2Dto check for collision.entity- TheICollisionEntityto check for collision.- Returns:
trueif anyICollisionEntityintersecting the line collides with the givenICollisionEntity.falseotherwise.- See Also:
-
collides
Checks if a givenICollisionEntitycollides with anyICollisionEntitiesof a givenCollisiontype that intersect a specific line.- Parameters:
line- TheLine2Dto check for collision.collision- TheCollisiontype to check for collision.entity- TheICollisionEntityto check for collision.- Returns:
trueif the entity collides with anyICollisionEntityon the given line.falseotherwise.- See Also:
-
collides
Checks if a given rectangle collides with anything registered in thePhysicsEngine.- Parameters:
rect- TheRectangle2Dto check for collision.- Returns:
trueif the rectangle collides with anything.falseotherwise.
-
collides
Checks if a givenICollisionEntitycollides with anything that intersects a specific rectangle.- Parameters:
rect- TheRectangle2Dto check for collision.entity- TheICollisionEntityto check for collision.- Returns:
trueif the entity collides with anyICollisionEntityin the given rectangle.falseotherwise.- See Also:
-
collides
Checks if a rectangle collides with anything of the givenCollisiontype.- Parameters:
rect- TheRectangle2Dto check for collision.collision- TheCollisiontype to check for collisions.- Returns:
trueif the rectangle collides with anything of the givenCollisiontype.falseotherwise.- See Also:
-
collides
Checks if a givenICollisionEntitycollides with anyICollisionEntitiesof a givenCollisiontype that intersect a specific rectangle.- Parameters:
rect- TheRectangle2Dto check for collision.collision- TheCollisiontype to check for collision.entity- TheICollisionEntityto check for collision.- Returns:
trueif the entity collides with anyICollisionEntityin the given rectangle.falseotherwise.- See Also:
-
collides
Checks if a given point collides with anything registered in thePhysicsEngine.- Parameters:
location- ThePoint2Dto check for collision.- Returns:
trueif the point collides with anything.falseotherwise.
-
collides
Checks if a point collides with anything of the givenCollisiontype.- Parameters:
location- ThePoint2Dto check for collision.collision- TheCollisiontype to check for collisions.- Returns:
trueif the point collides with anything of the givenCollisiontype.falseotherwise.- See Also:
-
collides
Checks if a givenICollisionEntitycollides with anything that intersects a specific point.- Parameters:
location- ThePoint2Dto check for collision.entity- TheICollisionEntityto check for collision.- Returns:
trueif the entity collides with anyICollisionEntityon the given point.falseotherwise.- See Also:
-
collides
Checks if a givenICollisionEntitycollides with anyICollisionEntitiesof a givenCollisiontype that intersect a specific point.- Parameters:
location- ThePoint2Dto check for collision.collision- TheCollisiontype to check for collision.entity- TheICollisionEntityto check for collision.- Returns:
trueif the entity collides with anyICollisionEntityon the given point.falseotherwise.- See Also:
-
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:
trueif the coordinates collide with anything.falseotherwise.
-
collides
Checks if the point at the given coordinates collides with anything of the givenCollisiontype.- Parameters:
x- The x coordinate to check for collision.y- The y coordinate to check for collision.collision- TheCollisiontype to check for collisions.- Returns:
trueif the coordinates collide with anything of the givenCollisiontype.falseotherwise.- See Also:
-
collides
Checks if a givenICollisionEntitycollides with anything that intersects specific coordinates.- Parameters:
x- The x coordinate to check for collision.y- The y coordinate to check for collision.entity- TheICollisionEntityto check for collision.- Returns:
trueif the entity collides with anyICollisionEntityon the given coordinates.falseotherwise.- See Also:
-
collides
Checks if a givenICollisionEntitycollides with anything registered in thePhysicsEngine.- Parameters:
entity- TheICollisionEntityto check for collision.- Returns:
trueif the entity collides with any otherICollisionEntity.falseotherwise.- See Also:
-
collides
Checks if a givenICollisionEntitycollides with anything of the givenCollisiontype.- Parameters:
entity- TheICollisionEntityto check for collision.collision- TheCollisiontype to check for collisions.- Returns:
trueif the entity collides with anything of the givenCollisiontype.falseotherwise.- See Also:
-
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
RaycastHitdetermining 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
RaycastHitdetermining 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 with the givenCollisiontype.- Parameters:
start- The start point of the raycast.target- The end point of the raycast.collision- TheCollisiontype to check for collision.- Returns:
- A
RaycastHitdetermining 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
RaycastHitdetermining 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 givenCollisiontype.- Parameters:
line- The line along which the ray is cast.collision- TheCollisiontype to check for collision.- Returns:
- A
RaycastHitdetermining 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- TheICollisionEntitytype to check for collision.- Returns:
- A
RaycastHitdetermining 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 certainCollisiontype that collides with the givenICollisionEntity.- Parameters:
line- The line along which the ray is cast.collision- TheCollisiontype to check for collision.entity- TheICollisionEntitytype to check for collision.- Returns:
- A
RaycastHitdetermining 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:
-
move
Moves the specified entity by a given distance and angle.- Parameters:
entity- TheIMobileEntitywhich is moveddirection- TheDirectionin which the entity is moveddistance- The distance to move the entity- Returns:
true, if the entity can be moved without colliding, otherwisefalse.- See Also:
-
move
Moves the specified entity by a given distance towards the target coordinates.- Parameters:
entity- TheIMobileEntitywhich 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- TheIMobileEntitywhich is moveddistance- The distance to move the entity- Returns:
true, if the entity can be moved without colliding, otherwisefalse.- See Also:
-
move
Moves the specified entity to a target point. IfturnTowardsTargetistrue, set the entity's angle towards the target.- Parameters:
entity- TheIMobileEntitywhich is movedtarget- The target pointturnTowardsTarget- Boolean that determines whether the movement should turn the entity towards the target point.- Returns:
true, if the entity can be moved without colliding, otherwisefalse.- See Also:
-
move
Moves the specified entity to a target point. Ifentity.turnOnMove()istrue, set the entity's angle towards the target.- Parameters:
entity- TheIMobileEntitywhich is movedtarget- The target point- Returns:
true, if the entity can be moved without colliding, otherwisefalse.- See Also:
-
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- TheIMobileEntitywhich is movedtarget- The target pointdistance- The distance to move the entity- Returns:
true, if the entity can be moved without colliding, otherwisefalse.- See Also:
-
update
public void update()Clears all collision boxes registered on thePhysicsEngineonce per tick and re-adds them with their updated positions.- Specified by:
updatein interfaceIUpdateable- See Also:
-