Module org.dyn4j

Class DistanceJoint

  • All Implemented Interfaces:
    DataContainer, Shiftable

    public class DistanceJoint
    extends Joint
    implements Shiftable, DataContainer
    Implementation of a fixed length distance joint.

    Given the two world space anchor points a distance is computed and used to constrain the attached Bodys at that distance. The bodies can rotate freely about the anchor points and the whole system can move and rotate freely, but the distance between the two anchor points is fixed.

    This joint doubles as a spring/damper distance joint where the length can change but is constantly approaching the target distance. Enable the spring/damper by setting the frequency and damping ratio to values greater than zero. A good starting point is a frequency of 8.0 and damping ratio of 0.3 then adjust as necessary.

    Since:
    1.0.0
    Version:
    3.2.1
    Author:
    William Bittle
    See Also:
    Documentation, Distance Constraint
    • Field Detail

      • localAnchor1

        protected Vector2 localAnchor1
        The local anchor point on the first Body
      • localAnchor2

        protected Vector2 localAnchor2
        The local anchor point on the second Body
      • frequency

        protected double frequency
        The oscillation frequency in hz
      • dampingRatio

        protected double dampingRatio
        The damping ratio
      • distance

        protected double distance
        The computed distance between the two world space anchor points
    • Constructor Detail

      • DistanceJoint

        public DistanceJoint​(Body body1,
                             Body body2,
                             Vector2 anchor1,
                             Vector2 anchor2)
        Minimal constructor.

        Creates a fixed distance Joint where the joined Bodys do not participate in collision detection and resolution.

        Parameters:
        body1 - the first Body
        body2 - the second Body
        anchor1 - in world coordinates
        anchor2 - in world coordinates
        Throws:
        java.lang.NullPointerException - if body1, body2, anchor1, or anchor2 is null
        java.lang.IllegalArgumentException - if body1 == body2
    • Method Detail

      • toString

        public java.lang.String toString()
        Overrides:
        toString in class Joint
      • initializeConstraints

        public void initializeConstraints​(Step step,
                                          Settings settings)
        Description copied from class: Joint
        Performs any initialization of the velocity and position constraints.
        Specified by:
        initializeConstraints in class Joint
        Parameters:
        step - the time step information
        settings - the current world settings
      • solveVelocityConstraints

        public void solveVelocityConstraints​(Step step,
                                             Settings settings)
        Description copied from class: Joint
        Solves the velocity constraints.
        Specified by:
        solveVelocityConstraints in class Joint
        Parameters:
        step - the time step information
        settings - the current world settings
      • solvePositionConstraints

        public boolean solvePositionConstraints​(Step step,
                                                Settings settings)
        Description copied from class: Joint
        Solves the position constraints.
        Specified by:
        solvePositionConstraints in class Joint
        Parameters:
        step - the time step information
        settings - the current world settings
        Returns:
        boolean true if the position constraints were solved
      • getAnchor1

        public Vector2 getAnchor1()
        Description copied from class: Joint
        Returns the anchor point on the first Body in world coordinates.
        Specified by:
        getAnchor1 in class Joint
        Returns:
        Vector2
      • getAnchor2

        public Vector2 getAnchor2()
        Description copied from class: Joint
        Returns the anchor point on the second Body in world coordinates.
        Specified by:
        getAnchor2 in class Joint
        Returns:
        Vector2
      • getReactionForce

        public Vector2 getReactionForce​(double invdt)
        Description copied from class: Joint
        Returns the force applied to the Bodys in order to satisfy the constraint in newtons.
        Specified by:
        getReactionForce in class Joint
        Parameters:
        invdt - the inverse delta time
        Returns:
        Vector2
      • getReactionTorque

        public double getReactionTorque​(double invdt)
        Returns the torque applied to the Bodys in order to satisfy the constraint in newton-meters.

        Not applicable to this joint. Always returns zero.

        Specified by:
        getReactionTorque in class Joint
        Parameters:
        invdt - the inverse delta time
        Returns:
        double
      • shift

        public void shift​(Vector2 shift)
        Description copied from interface: Shiftable
        Translates the object to match the given coordinate shift.
        Specified by:
        shift in interface Shiftable
        Parameters:
        shift - the amount to shift along the x and y axes
      • isSpring

        public boolean isSpring()
        Returns true if this distance joint is a spring distance joint.
        Returns:
        boolean
      • isSpringDamper

        public boolean isSpringDamper()
        Returns true if this distance joint is a spring distance joint with damping.
        Returns:
        boolean
      • getDistance

        public double getDistance()
        Returns the rest distance between the two constrained Bodys in meters.
        Returns:
        double
      • setDistance

        public void setDistance​(double distance)
        Sets the rest distance between the two constrained Bodys in meters.
        Parameters:
        distance - the distance in meters
        Throws:
        java.lang.IllegalArgumentException - if distance is less than zero
      • getDampingRatio

        public double getDampingRatio()
        Returns the damping ratio.
        Returns:
        double
      • setDampingRatio

        public void setDampingRatio​(double dampingRatio)
        Sets the damping ratio.

        Larger values reduce the oscillation of the spring.

        Parameters:
        dampingRatio - the damping ratio; in the range [0, 1]
        Throws:
        java.lang.IllegalArgumentException - if damping ration is less than zero or greater than 1
      • getFrequency

        public double getFrequency()
        Returns the spring frequency.
        Returns:
        double
      • setFrequency

        public void setFrequency​(double frequency)
        Sets the spring frequency.

        Larger values increase the stiffness of the spring.

        Parameters:
        frequency - the spring frequency in hz; must be greater than or equal to zero
        Throws:
        java.lang.IllegalArgumentException - if frequency is less than zero