Class PolygonObstacle

java.lang.Object
edu.cornell.gdiac.physics2.Obstacle
edu.cornell.gdiac.physics2.PolygonObstacle

public class PolygonObstacle extends Obstacle
A polygonal-shaped model to support collisions.

The polygon will be triangulated, so that the fixtures are guaranteed to be convex. The polyon coordinates are all in local space, relative to the object center.

  • Field Details

    • shapes

      protected com.badlogic.gdx.physics.box2d.PolygonShape[] shapes
      Shape information for this physics object
    • poly

      protected Poly2 poly
      The Poly2 representation of this obstacle
  • Constructor Details

    • PolygonObstacle

      public PolygonObstacle(float[] points)
      Creates a (not necessarily convex) polygon at the origin.

      The points given are relative to the origin. They are measured in Box2d units.

      Parameters:
      points - The polygon vertices
    • PolygonObstacle

      public PolygonObstacle(float[] points, float x, float y)
      Creates a (not necessarily convex) polygon

      The points given are relative to the obstacle's origin. They are measured in Box2d units.

      Parameters:
      points - The polygon vertices
      x - The x position of the polygon origin
      y - The y position of the polygon origin
    • PolygonObstacle

      public PolygonObstacle(com.badlogic.gdx.utils.FloatArray points)
      Creates a (not necessarily convex) polygon at the origin.

      The points given are relative to the origin. They are measured in Box2d units.

      Parameters:
      points - The polygon vertices
    • PolygonObstacle

      public PolygonObstacle(com.badlogic.gdx.utils.FloatArray points, float x, float y)
      Creates a (not necessarily convex) polygon

      The points given are relative to the obstacle's origin. They are measured in Box2d units.

      Parameters:
      points - The polygon vertices
      x - The x position of the polygon origin
      y - The y position of the polygon origin
    • PolygonObstacle

      public PolygonObstacle(Poly2 poly)
      Creates a (not necessarily convex) polygon at the origin.

      The points in the Poly2 object are specified relative to the origin. They are measured in Box2d units.

      Parameters:
      poly - The polygon shape
    • PolygonObstacle

      public PolygonObstacle(Poly2 poly, float x, float y)
      Creates a (not necessarily convex) polygon

      The points in the Poly2 object are specified relative to the obstacle's origin. They are measured in Box2d units.

      Parameters:
      poly - The polygon shape
      x - The x position of the polygon origin
      y - The y position of the polygon origin
  • Method Details

    • getBounds

      public com.badlogic.gdx.math.Rectangle getBounds()
      Returns the bounds of this polygon

      The bounds of a polygon is the axis-aligned bounding box containing all of the vertices. This value is not scaled by the physics units, so it is expressed in Box2d units.

      Returns:
      the bounds of this polygon
    • getBounds

      public com.badlogic.gdx.math.Rectangle getBounds(com.badlogic.gdx.math.Rectangle rect)
      Sets the given rectangle to the bounds of this polygon

      The bounds of a polygon is the axis-aligned bounding box containing all of the vertices. This value is not scaled by the physics units, so it is expressed in Box2d units.

      Parameters:
      rect - the rectangle to store the result
      Returns:
      the rectangle storing the result
    • setPoints

      public void setPoints(float[] points)
      Sets this polygon obstacle to contain the given points.

      This is an expensive method as it will retriangulate all of the fixtures. If you need to change the shape of an obstacle, we recommend that you create a Poly2 object first and use setPolygon(edu.cornell.gdiac.math.Poly2).

      The points given are relative to the polygon's origin. They are measured in Box2d units.

      Parameters:
      points - The points to triangulate
    • setPoints

      public void setPoints(float[] points, int offset, int length)
      Sets this polygon obstacle to contain the given points.

      This is an expensive method as it will retriangulate all of the fixtures. If you need to change the shape of an obstacle, we recommend that you create a Poly2 object first and use setPolygon(edu.cornell.gdiac.math.Poly2).

      The points given are relative to the polygon's origin. They are measured in Box2d units.

      Parameters:
      points - The points to triangulate
      offset - The offset into the array
      length - The length of the array
    • setPoints

      public void setPoints(com.badlogic.gdx.utils.FloatArray points)
      Sets this polygon obstacle to contain the given points.

      This is an expensive method as it will retriangulate all of the fixtures. If you need to change the shape of an obstacle, we recommend that you create a Poly2 object first and use setPolygon(edu.cornell.gdiac.math.Poly2).

      The points given are relative to the obstacle origin. They are measured in Box2d units.

      Parameters:
      points - The points to triangulate
    • setPolygon

      public void setPolygon(Poly2 poly)
      Sets this obstacle to be the given polygon.

      The polygon given are relative to the obstacle origin. They are measured in Box2d units. This method does not keep a reference to the provided Poly2 object.

      Parameters:
      poly - The polygon for this obstacle
    • createFixtures

      protected void createFixtures()
      Creates new fixtures for this body, defining the shape

      This is the primary method to override for custom physics objects

      Specified by:
      createFixtures in class Obstacle
    • releaseFixtures

      protected void releaseFixtures()
      Releases the fixtures for this body, reseting the shape

      This is the primary method to override for custom physics objects

      Specified by:
      releaseFixtures in class Obstacle
    • draw

      public void draw(SpriteBatch batch)
      Draws a wireframe of this physics object.

      The physics object will be drawn to the given sprite batch as a white wireframe, using the Obstacle.getPhysicsUnits() to properly scale the shape.

      Overrides:
      draw in class Obstacle
      Parameters:
      batch - the spritebatch to draw this object
    • draw

      public void draw(SpriteBatch batch, com.badlogic.gdx.graphics.Color color)
      Draws a wireframe of this physics object.

      The physics object will be drawn to the given sprite batch as a wireframe of the given color, using the Obstacle.getPhysicsUnits() to properly scale the shape.

      Overrides:
      draw in class Obstacle
      Parameters:
      batch - the spritebatch to draw this object
      color - the wireframe color
    • draw

      public void draw(SpriteBatch batch, com.badlogic.gdx.math.Affine2 affine)
      Draws a wireframe of this physics object.

      The physics object will be drawn to the given sprite batch as a white wireframe, using the Obstacle.getPhysicsUnits() to properly scale the shape. The affine transform will be applied to this scaled coordinate system.

      Overrides:
      draw in class Obstacle
      Parameters:
      batch - the spritebatch to draw this object
      affine - the transform to apply
    • draw

      public void draw(SpriteBatch batch, com.badlogic.gdx.graphics.Color color, com.badlogic.gdx.math.Affine2 affine)
      Draws a wireframe of this physics object.

      The physics object will be drawn to the given sprite batch as a wireframe of the given color, using the Obstacle.getPhysicsUnits() to properly scale the shape. The affine transform will be applied to this scaled coordinate system.

      Overrides:
      draw in class Obstacle
      Parameters:
      batch - the spritebatch to draw this object
      color - the wireframe color
      affine - the transform to apply