Module org.dyn4j

Class AngleJoint<T extends PhysicsBody>

  • Type Parameters:
    T - the PhysicsBody type
    All Implemented Interfaces:
    DataContainer, Shiftable, Ownable

    public class AngleJoint<T extends PhysicsBody>
    extends Joint<T>
    implements Shiftable, DataContainer
    Implementation of an angle joint.

    A angle joint constrains the relative rotation of two bodies. The bodies will continue to translate freely.

    By default the lower and upper limit angles are set to the current angle between the bodies. When the lower and upper limits are equal, the bodies rotate together and are not allowed rotate relative to one another. By default the limits are disabled.

    If the lower and upper limits are set explicitly, the values must follow these restrictions:

    • lower limit ≤ upper limit
    • lower limit > -180
    • upper limit < 180
    To create a joint with limits outside of this range use the setReferenceAngle(double) method. This method sets the baseline angle for the joint, which represents 0 radians in the context of the limits. For example:
     // we would like the joint limits to be [30, 260]
     // this is the same as the limits [-60, 170] if the reference angle is 90
     joint.setLimits(Math.toRadians(-60), Math.toRadians(170));
     joint.setReferenceAngle(Math.toRadians(90));
     
    The angle joint also allows a ratio value that allow the bodies to rotate at a specified value relative to the other. This can be used to simulate gears.

    Since the AngleJoint class defaults the upper and lower limits to the same value and by default the limits are enabled, you will need to modify the limits, or disable the limit to see the effect of the ratio.

    When the angle between the bodies reaches a limit, and limits are enabled, the ratio will be turned off.

    NOTE: The getAnchor1() and getAnchor2() methods return the world space center points for the joined bodies. This constraint doesn't need anchor points.

    Since:
    2.2.2
    Version:
    4.0.0
    Author:
    William Bittle
    See Also:
    Documentation, Angle Constraint
    • Method Detail

      • initializeConstraints

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

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

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

        public Vector2 getAnchor1()
        Returns the anchor point on the first PhysicsBody in world coordinates.

        Not applicable to this joint. This method returns the first body's world center.

        Specified by:
        getAnchor1 in class Joint<T extends PhysicsBody>
        Returns:
        Vector2
      • getAnchor2

        public Vector2 getAnchor2()
        Returns the anchor point on the second PhysicsBody in world coordinates.

        Not applicable to this joint. This method returns the second body's world center.

        Specified by:
        getAnchor2 in class Joint<T extends PhysicsBody>
        Returns:
        Vector2
      • getReactionForce

        public Vector2 getReactionForce​(double invdt)
        Returns the force applied to the PhysicsBodys in order to satisfy the constraint in newtons.

        Not applicable to this joint. Returns a new zero Vector2.

        Specified by:
        getReactionForce in class Joint<T extends PhysicsBody>
        Parameters:
        invdt - the inverse delta time
        Returns:
        Vector2
      • getReactionTorque

        public double getReactionTorque​(double invdt)
        Description copied from class: Joint
        Returns the torque applied to the PhysicsBodys in order to satisfy the constraint in newton-meters.
        Specified by:
        getReactionTorque in class Joint<T extends PhysicsBody>
        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
      • getJointAngle

        public double getJointAngle()
        Returns the relative angle between the two PhysicsBodys in radians in the range [-π, π].
        Returns:
        double
        Since:
        3.1.0
      • getRatio

        public double getRatio()
        Returns the angular velocity ratio between the two bodies.
        Returns:
        double
        Since:
        3.1.0
      • setRatio

        public void setRatio​(double ratio)
        Sets the angular velocity ratio between the two bodies.

        To disable the ratio and fix their velocities set the ratio to 1.0.

        The ratio can be negative to reverse the direction of the velocity of the other body.

        Parameters:
        ratio - the ratio
        Since:
        3.1.0
      • setLimitEnabled

        public void setLimitEnabled​(boolean flag)
        Sets whether the angle limits are enabled.
        Parameters:
        flag - true if the angle limits should be enforced
      • isLimitEnabled

        public boolean isLimitEnabled()
        Returns true if the limit is enabled.
        Returns:
        boolean true if the limit is enabled
        Since:
        3.0.1
      • getUpperLimit

        public double getUpperLimit()
        Returns the upper limit in radians.
        Returns:
        double
      • setUpperLimit

        public void setUpperLimit​(double upperLimit)
        Sets the upper limit in radians.

        See the class documentation for more details on the limit ranges.

        Parameters:
        upperLimit - the upper limit in radians
        Throws:
        IllegalArgumentException - if upperLimit is less than the current lower limit
      • getLowerLimit

        public double getLowerLimit()
        Returns the lower limit in radians.
        Returns:
        double
      • setLowerLimit

        public void setLowerLimit​(double lowerLimit)
        Sets the lower limit in radians.

        See the class documentation for more details on the limit ranges.

        Parameters:
        lowerLimit - the lower limit in radians
        Throws:
        IllegalArgumentException - if lowerLimit is greater than the current upper limit
      • setLimits

        public void setLimits​(double lowerLimit,
                              double upperLimit)
        Sets both the lower and upper limits.

        See the class documentation for more details on the limit ranges.

        Parameters:
        lowerLimit - the lower limit in radians
        upperLimit - the upper limit in radians
        Throws:
        IllegalArgumentException - if lowerLimit is greater than upperLimit
      • setLimitsEnabled

        public void setLimitsEnabled​(double lowerLimit,
                                     double upperLimit)
        Sets both the lower and upper limits and enables them.

        See the class documentation for more details on the limit ranges.

        Parameters:
        lowerLimit - the lower limit in radians
        upperLimit - the upper limit in radians
        Throws:
        IllegalArgumentException - if lowerLimit is greater than upperLimit
      • setLimits

        public void setLimits​(double limit)
        Sets both the lower and upper limits to the given limit.

        See the class documentation for more details on the limit ranges.

        Parameters:
        limit - the desired limit
      • setLimitsEnabled

        public void setLimitsEnabled​(double limit)
        Sets both the lower and upper limits to the given limit and enables them.

        See the class documentation for more details on the limit ranges.

        Parameters:
        limit - the desired limit
      • getReferenceAngle

        public double getReferenceAngle()
        Returns the reference angle.

        The reference angle is the angle calculated when the joint was created from the two joined bodies. The reference angle is the angular difference between the bodies.

        Returns:
        double
        Since:
        3.0.1
      • setReferenceAngle

        public void setReferenceAngle​(double angle)
        Sets the reference angle.

        This method can be used to set the reference angle to override the computed reference angle from the constructor. This is useful in recreating the joint from a current state.

        See the class documentation for more details.

        Parameters:
        angle - the reference angle in radians
        Since:
        3.0.1
        See Also:
        getReferenceAngle()
      • getLimitState

        @Deprecated
        public LimitState getLimitState()
        Deprecated.
        Deprecated in 4.0.0.
        Returns the current state of the limit.
        Returns:
        LimitState
        Since:
        3.2.0