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 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 
    ConstructorDescription
    PhysicsEngine()
    You should never call this manually! Instead use the Game.physics() instance.
  • Method Summary

    Modifier and TypeMethodDescription
    voidadd​(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 the PhysicsEngine.
    booleancollides​(double x, double y, ICollisionEntity entity)
    Checks if a given ICollisionEntity collides with anything that intersects specific coordinates.
    booleancollides​(double x, double y, Collision collision)
    Checks if the point at the given coordinates collides with anything of the given Collision type.
    booleancollides​(ICollisionEntity entity)
    Checks if a given ICollisionEntity collides with anything registered in the PhysicsEngine.
    booleancollides​(ICollisionEntity entity, Collision collision)
    Checks if a given ICollisionEntity collides with anything of the given Collision type.
    booleancollides​(java.awt.geom.Line2D line)
    Checks if a given line collides with anything registered in the PhysicsEngine.
    booleancollides​(java.awt.geom.Line2D line, ICollisionEntity entity)
    Checks if a given ICollisionEntity collides with anything that intersects a specific line.
    booleancollides​(java.awt.geom.Line2D line, Collision collision)
    Checks if a line collides with anything of the given Collision type.
    booleancollides​(java.awt.geom.Line2D line, Collision collision, ICollisionEntity entity)
    Checks if a given ICollisionEntity collides with any ICollisionEntities of a given Collision type that intersect a specific line.
    booleancollides​(java.awt.geom.Point2D location)
    Checks if a given point collides with anything registered in the PhysicsEngine.
    booleancollides​(java.awt.geom.Point2D location, ICollisionEntity entity)
    Checks if a given ICollisionEntity collides with anything that intersects a specific point.
    booleancollides​(java.awt.geom.Point2D location, Collision collision)
    Checks if a point collides with anything of the given Collision type.
    booleancollides​(java.awt.geom.Point2D location, Collision collision, ICollisionEntity entity)
    Checks if a given ICollisionEntity collides with any ICollisionEntities of a given Collision type that intersect a specific point.
    booleancollides​(java.awt.geom.Rectangle2D rect)
    Checks if a given rectangle collides with anything registered in the PhysicsEngine.
    booleancollides​(java.awt.geom.Rectangle2D rect, ICollisionEntity entity)
    Checks if a given ICollisionEntity collides with anything that intersects a specific rectangle.
    booleancollides​(java.awt.geom.Rectangle2D rect, Collision collision)
    Checks if a rectangle collides with anything of the given Collision type.
    booleancollides​(java.awt.geom.Rectangle2D rect, Collision collision, ICollisionEntity entity)
    Checks if a given ICollisionEntity collides with any ICollisionEntities of a given Collision type that intersect a specific rectangle.
    java.awt.geom.Rectangle2DgetBounds()
    Gets the environment bounds that confine the operation area of the PhysicsEngine.
    java.util.Collection<java.awt.geom.Rectangle2D>getCollisionBoxes()
    Gets all CollisionBoxes, regardless of their Collision type.
    java.util.Collection<java.awt.geom.Rectangle2D>getCollisionBoxes​(Collision type)
    Gets all CollisionBoxes with the given Collision type.
    java.util.Collection<ICollisionEntity>getCollisionEntities()
    Gets all ICollisionEntities, regardless of their Collision type.
    java.util.Collection<ICollisionEntity>getCollisionEntities​(Collision type)
    Gets all ICollisionEntities with the given Collision type.
    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, java.awt.geom.Point2D target)
    Moves the specified entity to a target point.
    booleanmove​(IMobileEntity entity, java.awt.geom.Point2D target, float distance)
    Moves the specified entity by a given distance towards the target coordinates.
    RaycastHitraycast​(java.awt.geom.Line2D line)
    Cast a ray along a given line [from (x1,y1) to (x2,y2)] and see if it hits anything.
    RaycastHitraycast​(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 given ICollisionEntity.
    RaycastHitraycast​(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 given Collision type.
    RaycastHitraycast​(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 certain Collision type that collides with the given ICollisionEntity.
    RaycastHitraycast​(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.
    RaycastHitraycast​(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.
    RaycastHitraycast​(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 given Collision type.
    voidremove​(ICollisionEntity entity)
    Removes the specified entity from any collision processing.
    voidsetBounds​(java.awt.geom.Rectangle2D environmentBounds)
    Sets the environment bounds that confine the operation area of the PhysicsEngine.
    voidupdate()
    Clears all collision boxes registered on the PhysicsEngine once per tick and re-adds them with their updated positions.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • PhysicsEngine

      public PhysicsEngine()

      You should never call this manually! Instead use the Game.physics() instance.

      See Also:
      Game.physics()
  • Method Details

    • add

      public 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.

      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

      public void remove​(ICollisionEntity entity)
      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 all CollisionBoxes, regardless of their Collision type.
      Returns:
      A Collection of all CollisionBoxes registered on the PhysicsEngine.
    • getCollisionBoxes

      public java.util.Collection<java.awt.geom.Rectangle2D> getCollisionBoxes​(Collision type)
      Gets all CollisionBoxes with the given Collision type.
      Parameters:
      type - The Collision type by which the CollisionBoxes are selected.
      Returns:
      If the Collision type is NONE, return an empty set. Otherwise, a Collection of all CollisionBoxes registered on the PhysicsEngine that have the given Collision type.
    • getCollisionEntities

      public java.util.Collection<ICollisionEntity> getCollisionEntities()
      Gets all ICollisionEntities, regardless of their Collision type.
      Returns:
      A Collection of all ICollisionEntities registered on the PhysicsEngine.
    • getCollisionEntities

      public java.util.Collection<ICollisionEntity> getCollisionEntities​(Collision type)
      Gets all ICollisionEntities with the given Collision type.
      Parameters:
      type - The Collision type by which the ICollisionEntities are selected.
      Returns:
      If the Collision type is NONE, return an empty set. Otherwise, a Collection of all ICollisionEntities registered on the PhysicsEngine that have the given Collision type.
    • getBounds

      public java.awt.geom.Rectangle2D getBounds()
      Gets the environment bounds that confine the operation area of the PhysicsEngine.
      Returns:
      The Rectangle2D confining the operation area of the PhysicsEngine.
    • setBounds

      public void setBounds​(java.awt.geom.Rectangle2D environmentBounds)
      Sets the environment bounds that confine the operation area of the PhysicsEngine.
      Parameters:
      environmentBounds - The Rectangle2D confining the operation area of the PhysicsEngine.
    • collides

      public boolean collides​(java.awt.geom.Line2D line)
      Checks if a given line collides with anything registered in the PhysicsEngine.
      Parameters:
      line - The Line2D to check for collision.
      Returns:
      true if the line collides with anything. false otherwise.
    • collides

      public boolean collides​(java.awt.geom.Line2D line, Collision collision)
      Checks if a line collides with anything of the given Collision type.
      Parameters:
      line - The Line2D to check for collision.
      collision - The Collision type to check for collisions.
      Returns:
      true if the line collides with anything of the given Collision type. false otherwise.
      See Also:
      Collision
    • collides

      public boolean collides​(java.awt.geom.Line2D line, ICollisionEntity entity)
      Checks if a given ICollisionEntity collides with anything that intersects a specific line.
      Parameters:
      line - The Line2D to check for collision.
      entity - The ICollisionEntity to check for collision.
      Returns:
      true if any ICollisionEntity intersecting the line collides with the given ICollisionEntity. false otherwise.
      See Also:
      Collision, ICollisionEntity
    • collides

      public boolean collides​(java.awt.geom.Line2D line, Collision collision, ICollisionEntity entity)
      Checks if a given ICollisionEntity collides with any ICollisionEntities of a given Collision type that intersect a specific line.
      Parameters:
      line - The Line2D to check for collision.
      collision - The Collision type to check for collision.
      entity - The ICollisionEntity to check for collision.
      Returns:
      true if the entity collides with any ICollisionEntity 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 the PhysicsEngine.
      Parameters:
      rect - The Rectangle2D to check for collision.
      Returns:
      true if the rectangle collides with anything. false otherwise.
    • collides

      public boolean collides​(java.awt.geom.Rectangle2D rect, ICollisionEntity entity)
      Checks if a given ICollisionEntity collides with anything that intersects a specific rectangle.
      Parameters:
      rect - The Rectangle2D to check for collision.
      entity - The ICollisionEntity to check for collision.
      Returns:
      true if the entity collides with any ICollisionEntity in the given rectangle. false otherwise.
      See Also:
      ICollisionEntity
    • collides

      public boolean collides​(java.awt.geom.Rectangle2D rect, Collision collision)
      Checks if a rectangle collides with anything of the given Collision type.
      Parameters:
      rect - The Rectangle2D to check for collision.
      collision - The Collision type to check for collisions.
      Returns:
      true if the rectangle collides with anything of the given Collision type. false otherwise.
      See Also:
      Collision
    • collides

      public boolean collides​(java.awt.geom.Rectangle2D rect, Collision collision, ICollisionEntity entity)
      Checks if a given ICollisionEntity collides with any ICollisionEntities of a given Collision type that intersect a specific rectangle.
      Parameters:
      rect - The Rectangle2D to check for collision.
      collision - The Collision type to check for collision.
      entity - The ICollisionEntity to check for collision.
      Returns:
      true if the entity collides with any ICollisionEntity 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 the PhysicsEngine.
      Parameters:
      location - The Point2D to check for collision.
      Returns:
      true if the point collides with anything. false otherwise.
    • collides

      public boolean collides​(java.awt.geom.Point2D location, Collision collision)
      Checks if a point collides with anything of the given Collision type.
      Parameters:
      location - The Point2D to check for collision.
      collision - The Collision type to check for collisions.
      Returns:
      true if the point collides with anything of the given Collision type. false otherwise.
      See Also:
      Collision
    • collides

      public boolean collides​(java.awt.geom.Point2D location, ICollisionEntity entity)
      Checks if a given ICollisionEntity collides with anything that intersects a specific point.
      Parameters:
      location - The Point2D to check for collision.
      entity - The ICollisionEntity to check for collision.
      Returns:
      true if the entity collides with any ICollisionEntity 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 given ICollisionEntity collides with any ICollisionEntities of a given Collision type that intersect a specific point.
      Parameters:
      location - The Point2D to check for collision.
      collision - The Collision type to check for collision.
      entity - The ICollisionEntity to check for collision.
      Returns:
      true if the entity collides with any ICollisionEntity 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 the PhysicsEngine.
      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

      public boolean collides​(double x, double y, Collision collision)
      Checks if the point at the given coordinates collides with anything of the given Collision type.
      Parameters:
      x - The x coordinate to check for collision.
      y - The y coordinate to check for collision.
      collision - The Collision type to check for collisions.
      Returns:
      true if the coordinates collide with anything of the given Collision type. false otherwise.
      See Also:
      Collision
    • collides

      public boolean collides​(double x, double y, ICollisionEntity entity)
      Checks if a given ICollisionEntity 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 - The ICollisionEntity to check for collision.
      Returns:
      true if the entity collides with any ICollisionEntity on the given coordinates. false otherwise.
      See Also:
      ICollisionEntity
    • collides

      public boolean collides​(ICollisionEntity entity)
      Checks if a given ICollisionEntity collides with anything registered in the PhysicsEngine.
      Parameters:
      entity - The ICollisionEntity to check for collision.
      Returns:
      true if the entity collides with any other ICollisionEntity. false otherwise.
      See Also:
      ICollisionEntity
    • collides

      public boolean collides​(ICollisionEntity entity, Collision collision)
      Checks if a given ICollisionEntity collides with anything of the given Collision type.
      Parameters:
      entity - The ICollisionEntity to check for collision.
      collision - The Collision type to check for collisions.
      Returns:
      true if the entity collides with anything of the given Collision type. false otherwise.
      See Also:
      Collision
    • raycast

      public 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.
      Parameters:
      start - The start point of the raycast.
      angle - The angle in degrees.
      Returns:
      A RaycastHit determining the hit point, ray length, and corresponding ICollisionEntity, if the ray hit something.
    • raycast

      public 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.
      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 corresponding ICollisionEntity, 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 given Collision type.
      Parameters:
      start - The start point of the raycast.
      target - The end point of the raycast.
      collision - The Collision type to check for collision.
      Returns:
      A RaycastHit determining the hit point, ray length, and corresponding ICollisionEntity, if the ray hit something.
    • raycast

      public 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.
      Parameters:
      line - The line along which the ray is cast.
      Returns:
      A RaycastHit determining the hit point, ray length, and corresponding ICollisionEntity, if the ray hit something.
    • raycast

      public 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 given Collision type.
      Parameters:
      line - The line along which the ray is cast.
      collision - The Collision type to check for collision.
      Returns:
      A RaycastHit determining the hit point, ray length, and corresponding ICollisionEntity, if the ray hit something.
    • raycast

      public 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 given ICollisionEntity.
      Parameters:
      line - The line along which the ray is cast.
      entity - The ICollisionEntity type to check for collision.
      Returns:
      A RaycastHit determining the hit point, ray length, and corresponding ICollisionEntity.
    • raycast

      public 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 certain Collision type that collides with the given ICollisionEntity.
      Parameters:
      line - The line along which the ray is cast.
      collision - The Collision type to check for collision.
      entity - The ICollisionEntity type to check for collision.
      Returns:
      A RaycastHit determining the hit point, ray length, and corresponding ICollisionEntity.
    • move

      public boolean move​(IMobileEntity entity, double angle, double distance)
      Moves the specified entity by a given distance and angle.
      Parameters:
      entity - The entity which is moved
      angle - The angle in degrees
      distance - The distance to move the entity
      Returns:
      true, if the entity can be moved without colliding, otherwise false.
      See Also:
      GeometricUtilities.project(Point2D, double, double)
    • move

      public boolean move​(IMobileEntity entity, Direction direction, double distance)
      Moves the specified entity by a given distance and angle.
      Parameters:
      entity - The IMobileEntity which is moved
      direction - The Direction in which the entity is moved
      distance - The distance to move the entity
      Returns:
      true, if the entity can be moved without colliding, otherwise false.
      See Also:
      Direction
    • move

      public boolean move​(IMobileEntity entity, double x, double y, float distance)
      Moves the specified entity by a given distance towards the target coordinates.
      Parameters:
      entity - The IMobileEntity which is moved
      x - The target x coordinate
      y - The target y coordinate
      distance - The distance to move the entity
      Returns:
      true, if the entity can be moved without colliding, otherwise false.
    • move

      public boolean move​(IMobileEntity entity, float distance)
      Moves the specified entity by a given distance and the entity's angle.
      Parameters:
      entity - The IMobileEntity which is moved
      distance - The distance to move the entity
      Returns:
      true, if the entity can be moved without colliding, otherwise false.
      See Also:
      Direction
    • move

      public boolean move​(IMobileEntity entity, java.awt.geom.Point2D target)
      Moves the specified entity to a target point. If entity.turnOnMove() is true, set the entity's angle towards the target.
      Parameters:
      entity - The IMobileEntity which is moved
      target - The target point
      Returns:
      true, if the entity can be moved without colliding, otherwise false.
      See Also:
      resolveCollisionForNewPosition(de.gurkenlabs.litiengine.entities.ICollisionEntity, java.awt.geom.Point2D)
    • move

      public boolean move​(IMobileEntity entity, java.awt.geom.Point2D target, float distance)
      Moves the specified entity by a given distance towards the target coordinates. If entity.turnOnMove() is true, set the entity's angle towards the target.
      Parameters:
      entity - The IMobileEntity which is moved
      target - The target point
      distance - The distance to move the entity
      Returns:
      true, if the entity can be moved without colliding, otherwise false.
      See Also:
      resolveCollisionForNewPosition(de.gurkenlabs.litiengine.entities.ICollisionEntity, java.awt.geom.Point2D)
    • update

      public void update()
      Clears all collision boxes registered on the PhysicsEngine once per tick and re-adds them with their updated positions.
      Specified by:
      update in interface IUpdateable
      See Also:
      ClientConfiguration.setMaxFps(int)