Module org.dyn4j

Class Capsule

  • All Implemented Interfaces:
    DataContainer, Convex, Rotatable, Shape, Transformable, Translatable

    public class Capsule
    extends AbstractShape
    implements Convex, Shape, Transformable, DataContainer
    Implementation of a Capsule Convex Shape.

    A capsule can be described as a rectangle with two half circle caps on both ends. A capsule is created by specifying the bounding rectangle of the entire Shape.

    If the height is larger than the width the caps will be on the top and bottom of the shape. Otherwise the caps are on the left and right ends of the shape.

    A capsule's width and height must be larger than zero and cannot be equal. A Circle should be used instead of an equal width/height capsule for both performance and stability.

    Since:
    3.1.5
    Version:
    3.4.0
    Author:
    William Bittle
    • Field Detail

      • EDGE_FEATURE_SELECTION_CRITERIA

        protected static final double EDGE_FEATURE_SELECTION_CRITERIA
        The Capsule shape has two edge features which could be returned from the getFarthestFeature(Vector2, Transform) method. Under normal floating point conditions the edges will never be selected as the farthest features. Due to this, stacking of capsule shapes is very unstable (or any resting contact that involves the edge). We introduce this factor (% of projected normal) to help select the edge in cases where the collision normal is nearly parallel to the edge normal.
        See Also:
        Constant Field Values
      • EDGE_FEATURE_EXPANSION_FACTOR

        protected static final double EDGE_FEATURE_EXPANSION_FACTOR
        Because we are selecting an edge even when the farthest feature should be a vertex, when the edges are clipped against each other (in the ClippingManifoldSolver) they will not overlap. Due to this, we introduce an expansion value (% of the width) that expands the edge feature so that in these cases a collision manifold is still generated.
        See Also:
        Constant Field Values
      • length

        final double length
        The bounding rectangle width
      • capRadius

        final double capRadius
        The end cap radius
      • foci

        final Vector2[] foci
        The focal points for the caps
      • localXAxis

        final Vector2 localXAxis
        The local x-axis
    • Constructor Detail

      • Capsule

        public Capsule​(double width,
                       double height)
        Minimal constructor.

        Creates an axis-aligned capsule centered on the origin with the caps on ends of the larger dimension.

        Parameters:
        width - the bounding rectangle width
        height - the bounding rectangle height
        Throws:
        java.lang.IllegalArgumentException - thrown if width or height are less than or equal to zero or if the width and height are near equal
    • Method Detail

      • getAxes

        public Vector2[] getAxes​(Vector2[] foci,
                                 Transform transform)
        Description copied from interface: Convex
        Returns an array of separating axes to test for this Shape.

        The foci parameter is an array of circular focal points of the other Shape.

        If foci points are given, this method will return the separating axes for this Shape's voronoi regions also. The points in the foci array are assumed to be in world space.

        The returned axes are normalized and in world space.

        Specified by:
        getAxes in interface Convex
        Parameters:
        foci - the world space points representing foci of curved Shapes; can be null
        transform - the local to world space Transform of this Convex Shape
        Returns:
        Vector2[]
      • getFoci

        public Vector2[] getFoci​(Transform transform)
        Description copied from interface: Convex
        Returns an array of world space foci points for circular curved edges.

        This method returns null if the Shape has zero curved edges.

        The returned points are in world space.

        Specified by:
        getFoci in interface Convex
        Parameters:
        transform - the local to world space Transform of this Convex Shape
        Returns:
        Vector2[]
      • getFarthestPoint

        public Vector2 getFarthestPoint​(Vector2 vector,
                                        Transform transform)
        Description copied from interface: Convex
        Returns the point farthest in the direction of the given vector. If two points are equally distant along the given Vector2 the first one is used.

        The returned point is in world space.

        Specified by:
        getFarthestPoint in interface Convex
        Parameters:
        vector - the direction
        transform - the local to world space Transform of this Convex Shape
        Returns:
        Vector2
      • createMass

        public Mass createMass​(double density)
        Description copied from interface: Shape
        Creates a Mass object using the geometric properties of this Shape and the given density.
        Specified by:
        createMass in interface Shape
        Parameters:
        density - the density in kg/m2
        Returns:
        Mass the Mass of this Shape
      • getRadius

        public double getRadius​(Vector2 center)
        Description copied from interface: Shape
        Returns the radius of the shape if the given point was the center for this shape.
        Specified by:
        getRadius in interface Shape
        Parameters:
        center - the center point
        Returns:
        double
      • contains

        public boolean contains​(Vector2 point,
                                Transform transform)
        Description copied from interface: Shape
        Returns true if the given point is inside this Shape.

        If the given point lies on an edge the point is considered to be inside the Shape.

        The given point is assumed to be in world space.

        Specified by:
        contains in interface Shape
        Parameters:
        point - world space point
        transform - Transform for this Shape
        Returns:
        boolean
      • rotate

        public void rotate​(Rotation rotation,
                           double x,
                           double y)
        Description copied from interface: Rotatable
        Rotates the object about the given point.
        Specified by:
        rotate in interface Rotatable
        Overrides:
        rotate in class AbstractShape
        Parameters:
        rotation - the Rotation representing the rotation amount
        x - the x coordinate to rotate about
        y - the y coordinate to rotate about
      • translate

        public void translate​(double x,
                              double y)
        Description copied from interface: Translatable
        Translates the object the given amounts in the respective directions.
        Specified by:
        translate in interface Translatable
        Overrides:
        translate in class AbstractShape
        Parameters:
        x - the translation in the x direction
        y - the translation in the y direction
      • getRotationAngle

        public double getRotationAngle()
        Returns the rotation about the local center in radians in the range [-π, π].
        Returns:
        double the rotation in radians
      • getRotation

        public Rotation getRotation()
        Returns:
        the Rotation object that represents the local rotation
      • getLength

        public double getLength()
        Returns the length of the capsule.

        The length is the largest dimension of the capsule's bounding rectangle.

        Returns:
        double
      • getCapRadius

        public double getCapRadius()
        Returns the end cap radius.
        Returns:
        double