Class CapsuleObstacle

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

public class CapsuleObstacle extends Obstacle
A capsule-shaped model to support collisions.

A capsule typically is a pill-like shape that fits inside of given rectangle (e.g. it has half circles at the ends of the longest side). If width < height, the capsule will be oriented vertically with the rounded portions at the top and bottom. Otherwise it will be oriented horizontally.

This class allows for the creation of half-capsules, simply by using the enumeration Capsule. The enumeration specifies which side should be rounded in case of a half-capsule. Half-capsules are sized so that the corresponding full capsule would fit in the bounding box.

Unless otherwise specified, the center of mass is the center of the capsule.

  • Field Details

    • shape

      protected com.badlogic.gdx.physics.box2d.PolygonShape shape
      Shape information for this box
    • end1

      protected com.badlogic.gdx.physics.box2d.CircleShape end1
      Shape information for the end cap
    • end2

      protected com.badlogic.gdx.physics.box2d.CircleShape end2
      Shape information for the end cap
    • center

      protected com.badlogic.gdx.math.Rectangle center
      Rectangle representation of capsule core for fast computation
  • Constructor Details

    • CapsuleObstacle

      public CapsuleObstacle(float width, float height)
      Creates a new box at the origin.

      The orientation of the capsule will be a full capsule along the major axis. If width == height, it will be a degenerate capsule (e.g a circle).

      The size is expressed in physics units NOT pixels. In order for drawing to work properly, you MUST call Obstacle.setPhysicsUnits(float).

      Parameters:
      width - The object width in physics units
      height - The object width in physics units
    • CapsuleObstacle

      public CapsuleObstacle(float x, float y, float width, float height)
      Creates a new capsule object.

      The orientation of the capsule will be a full capsule along the major axis. If width == height, it will be a degenerate capsule (e.g a circle).

      The size is expressed in physics units NOT pixels. In order for drawing to work properly, you MUST call Obstacle.setPhysicsUnits(float).

      Parameters:
      x - Initial x position of the box center
      y - Initial y position of the box center
      width - The object width in physics units
      height - The object width in physics units
    • CapsuleObstacle

      public CapsuleObstacle(float x, float y, float width, float height, Capsule style)
      Creates a new capsule object width the given style.

      The size is expressed in physics units NOT pixels. In order for drawing to work properly, you MUST call Obstacle.setPhysicsUnits(float).

      Parameters:
      x - Initial x position of the box center
      y - Initial y position of the box center
      width - The object width in physics units
      height - The object width in physics units
      style - The capsule style
  • Method Details

    • getDimension

      public com.badlogic.gdx.math.Vector2 getDimension()
      Returns the dimensions of this capsule

      This method does NOT return a reference to the dimension vector. Changes to this vector will not affect the shape. However, it returns the same vector each time its is called, and so cannot be used as an allocator.

      Returns:
      the dimensions of this capsule
    • setDimension

      public void setDimension(com.badlogic.gdx.math.Vector2 value)
      Sets the dimensions of this capsule

      This method does not keep a reference to the parameter.

      Parameters:
      value - the dimensions of this capsule
    • setDimension

      public void setDimension(float width, float height)
      Sets the dimensions of this capsule
      Parameters:
      width - The width of this capsule
      height - The height of this capsule
    • getWidth

      public float getWidth()
      Returns the capsule width
      Returns:
      the capsule width
    • setWidth

      public void setWidth(float value)
      Sets the capsule width
      Parameters:
      value - the capsule width
    • getHeight

      public float getHeight()
      Returns the capsule height
      Returns:
      the capsule height
    • setHeight

      public void setHeight(float value)
      Sets the capsule height
      Parameters:
      value - the capsule height
    • getStyle

      public Capsule getStyle()
      Returns the style of this capsule
      Returns:
      the style of this capsule
    • setOrientation

      public void setOrientation(Capsule value)
      Sets the style of this capsule.

      Note that style only controls whether the capsule is full, half, or half-reverse . The actual orientation is determined by the longest axis.

      Parameters:
      value - the style of this capsule
    • getSeamOffset

      public float getSeamOffset()
      Returns the seam offset of the core rectangle

      If the center rectangle is exactly the same size as the circle radius, you may get catching at the seems. To prevent this, you should make the center rectangle epsilon narrower so that everything rolls off the round shape. This parameter is that epsilon value.

      Returns:
      the seam offset of the core rectangle
    • setSeamOffset

      public void setSeamOffset(float value)
      Sets the seam offset of the core rectangle

      If the center rectangle is exactly the same size as the circle radius, you may get catching at the seems. To prevent this, you should make the center rectangle epsilon narrower so that everything rolls off the round shape. This parameter is that epsilon value.

      Parameters:
      value - the seam offset of the core rectangle
    • getTolerance

      public float getTolerance()
      Returns the outline tolerance of this obstacle. In box2d, the ends of the capsule will be perfect circles. But we cannot draw perfect circles. As a result, the outline drawn in debug mode may not perfectly match the box2d fixtures. This value defines that drawing tolerance. Lower values mean the ends are more likely to be circular (default value is 0.05). This value only affects debugging. It has no affect on box2d.
      Returns:
      the outline tolerance of this obstacle.
    • setTolerance

      public void setTolerance(float tolerance)
      Sets the outline tolerance of this obstacle. In box2d, the ends of the capsule will be perfect circles. But we cannot draw perfect circles. As a result, the outline drawn in debug mode may not perfectly match the box2d fixtures. This value defines that drawing tolerance. Lower values mean the ends are more likely to be circular (default value is 0.05). This value only affects debugging. It has no affect on box2d.
      Parameters:
      tolerance - The outline tolerance of this obstacle.
    • setDensity

      public void setDensity(float value)
      Sets the density of this body

      The density is typically measured in usually in kg/m^2. The density can be zero or positive. You should generally use similar densities for all your fixtures. This will improve stacking stability.

      Overrides:
      setDensity in class Obstacle
      Parameters:
      value - the density of this body
    • 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