Module org.dyn4j

Class WeldJoint<T extends PhysicsBody>

  • Type Parameters:
    T - the PhysicsBody type
    All Implemented Interfaces:
    DataContainer, AngularLimitsJoint, AngularSpringJoint, Joint<T>, PairedBodyJoint<T>, Shiftable, Ownable

    public class WeldJoint<T extends PhysicsBody>
    extends AbstractPairedBodyJoint<T>
    implements AngularLimitsJoint, AngularSpringJoint, PairedBodyJoint<T>, Joint<T>, Shiftable, DataContainer, Ownable
    Implementation of a weld joint.

    A weld joint joins two bodies together as if they were a single body. Both their relative linear and angular motion are constrained to keep them attached to each other. The system as a whole can rotate and translate freely.

    The bodies are locked in place in their current positions. The anchor point is used to detect joint separation and to serve as the pivot point for the angular spring.

    This joint supports an angular spring using the setSpringEnabled(boolean) method. You can control the behavior of the spring using the frequency, damping ratio, etc. settings. A good starting point is a frequency of 8.0 and damping ratio of 0.3 then adjust as necessary.

    This joint also supports angular limits, but only when the spring is enabled. Its recommended that you use limits when using the angular spring to prevent wrap-around (when the relative angle between the bodies changes from 180 to -180 for example).

    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 you want the lower and upper limit to be the same, disable the spring/limits for a more stable simulation.

    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 setLimitsReferenceAngle(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));
     
    Since:
    1.0.0
    Version:
    5.0.0
    Author:
    William Bittle
    See Also:
    Documentation, Weld Constraint
    • Field Detail

      • localAnchor1

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

        protected final Vector2 localAnchor2
        The local anchor point on the second PhysicsBody
      • limitsEnabled

        protected boolean limitsEnabled
        Whether the Joint limits are enabled or not
      • upperLimit

        protected double upperLimit
        The upper limit of the Joint
      • lowerLimit

        protected double lowerLimit
        The lower limit of the Joint
      • referenceAngle

        protected double referenceAngle
        The initial angle between the two PhysicsBodys
      • springEnabled

        protected boolean springEnabled
        True if the spring is enabled
      • springDamperEnabled

        protected boolean springDamperEnabled
        True if the spring-damper is enabled
      • springMode

        protected int springMode
        The spring mode (frequency or stiffness)
      • springFrequency

        protected double springFrequency
        The oscillation frequency in hz
      • springStiffness

        protected double springStiffness
        The stiffness of the spring
      • springDampingRatio

        protected double springDampingRatio
        The damping ratio
      • springMaximumTorqueEnabled

        protected boolean springMaximumTorqueEnabled
        True if the spring maximum force is enabled
      • springMaximumTorque

        protected double springMaximumTorque
        The maximum force the spring will apply
    • Method Detail

      • initializeConstraints

        public void initializeConstraints​(TimeStep step,
                                          Settings settings)
        Description copied from interface: Joint
        Performs any initialization of the velocity and position constraints.
        Specified by:
        initializeConstraints in interface 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 interface: Joint
        Solves the velocity constraints.
        Specified by:
        solveVelocityConstraints in interface 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 interface: Joint
        Solves the position constraints.
        Specified by:
        solvePositionConstraints in interface Joint<T extends PhysicsBody>
        Parameters:
        step - the time step information
        settings - the current world settings
        Returns:
        boolean true if the position constraints were solved
      • updateSpringCoefficients

        protected void updateSpringCoefficients()
        Computes the spring coefficients from the current state of the joint.

        This method is intended to set the springStiffness OR springFrequency and damping for use during constraint solving.

      • getAnchor1

        public Vector2 getAnchor1()
        Returns the world space anchor point for the first body.
        Returns:
        Vector2
      • getAnchor2

        public Vector2 getAnchor2()
        Returns the world space anchor point for the second body.
        Returns:
        Vector2
      • getReactionForce

        public Vector2 getReactionForce​(double invdt)
        Description copied from interface: Joint
        Returns the force applied to the PhysicsBodys in order to satisfy the constraint in newtons.
        Specified by:
        getReactionForce in interface Joint<T extends PhysicsBody>
        Parameters:
        invdt - the inverse delta time
        Returns:
        Vector2
      • getReactionTorque

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

        public void setSpringDampingRatio​(double dampingRatio)
        Description copied from interface: AngularSpringJoint
        Sets the damping ratio.

        Larger values reduce the oscillation of the spring.

        Specified by:
        setSpringDampingRatio in interface AngularSpringJoint
        Parameters:
        dampingRatio - the damping ratio; in the range (0, 1]
      • setSpringFrequency

        public void setSpringFrequency​(double frequency)
        Description copied from interface: AngularSpringJoint
        Sets the spring frequency.

        Larger values increase the stiffness of the spring.

        Calling this method puts the spring into fixed frequency mode. In this mode, the spring stiffness is computed based on the frequency.

        Specified by:
        setSpringFrequency in interface AngularSpringJoint
        Parameters:
        frequency - the spring frequency in hz; must be greater than zero
      • setSpringStiffness

        public void setSpringStiffness​(double stiffness)
        Description copied from interface: AngularSpringJoint
        Sets the spring stiffness.

        Larger values increase the stiffness of the spring.

        Calling this method puts the spring into fixed stiffness mode. In this mode, the spring frequency is computed based on the stiffness.

        Specified by:
        setSpringStiffness in interface AngularSpringJoint
        Parameters:
        stiffness - the spring stiffness (k); must be greater than zero
      • setMaximumSpringTorque

        public void setMaximumSpringTorque​(double maximum)
        Description copied from interface: AngularSpringJoint
        Sets the maximum spring torque the spring is allowed to exert.
        Specified by:
        setMaximumSpringTorque in interface AngularSpringJoint
        Parameters:
        maximum - the maximum spring torque
      • setMaximumSpringTorqueEnabled

        public void setMaximumSpringTorqueEnabled​(boolean enabled)
        Description copied from interface: AngularSpringJoint
        Sets whether the spring torque is limited to a maximum torque.
        Specified by:
        setMaximumSpringTorqueEnabled in interface AngularSpringJoint
        Parameters:
        enabled - true if the spring torque should be limited
      • setSpringEnabled

        public void setSpringEnabled​(boolean enabled)
        Description copied from interface: AngularSpringJoint
        Sets whether the spring is enabled or not.
        Specified by:
        setSpringEnabled in interface AngularSpringJoint
        Parameters:
        enabled - true if the spring should be enabled
      • getSpringTorque

        public double getSpringTorque​(double invdt)
        Description copied from interface: AngularSpringJoint
        Returns the torque applied to by the spring in the last step.
        Specified by:
        getSpringTorque in interface AngularSpringJoint
        Parameters:
        invdt - the inverse delta time
        Returns:
        double
      • setLimitsEnabled

        public void setLimitsEnabled​(boolean flag)
        Description copied from interface: AngularLimitsJoint
        Enables or disables the angular limits.
        Specified by:
        setLimitsEnabled in interface AngularLimitsJoint
        Parameters:
        flag - true if the limit should be enabled
      • setUpperLimit

        public void setUpperLimit​(double upperLimit)
        Description copied from interface: AngularLimitsJoint
        Sets the upper angular limit.

        Must be greater than or equal to the lower angular limit.

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

        Specified by:
        setUpperLimit in interface AngularLimitsJoint
        Parameters:
        upperLimit - the upper angular limit in radians
      • setLowerLimit

        public void setLowerLimit​(double lowerLimit)
        Description copied from interface: AngularLimitsJoint
        Sets the lower angular limit.

        Must be less than or equal to the upper angular limit.

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

        Specified by:
        setLowerLimit in interface AngularLimitsJoint
        Parameters:
        lowerLimit - the lower angular limit in radians
      • setLimits

        public void setLimits​(double lowerLimit,
                              double upperLimit)
        Description copied from interface: AngularLimitsJoint
        Sets the upper and lower angular limits.

        The lower limit must be less than or equal to the upper limit.

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

        Specified by:
        setLimits in interface AngularLimitsJoint
        Parameters:
        lowerLimit - the lower limit in radians
        upperLimit - the upper limit in radians
      • setLimitsEnabled

        public void setLimitsEnabled​(double lowerLimit,
                                     double upperLimit)
        Description copied from interface: AngularLimitsJoint
        Sets both the lower and upper limits and enables them.

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

        Specified by:
        setLimitsEnabled in interface AngularLimitsJoint
        Parameters:
        lowerLimit - the lower limit in radians
        upperLimit - the upper limit in radians
      • setLimitsEnabled

        public void setLimitsEnabled​(double limit)
        Description copied from interface: AngularLimitsJoint
        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.

        Specified by:
        setLimitsEnabled in interface AngularLimitsJoint
        Parameters:
        limit - the desired limit
      • setLimits

        public void setLimits​(double limit)
        Description copied from interface: AngularLimitsJoint
        Sets both the lower and upper limits to the given limit.

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

        Specified by:
        setLimits in interface AngularLimitsJoint
        Parameters:
        limit - the desired limit
      • getLimitsReferenceAngle

        public double getLimitsReferenceAngle()
        Description copied from interface: AngularLimitsJoint
        Returns the limits 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.

        Specified by:
        getLimitsReferenceAngle in interface AngularLimitsJoint
        Returns:
        double
      • setLimitsReferenceAngle

        public void setLimitsReferenceAngle​(double angle)
        Description copied from interface: AngularLimitsJoint
        Sets the limits 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 or when adjusting the limits.

        See the class documentation for more details.

        Specified by:
        setLimitsReferenceAngle in interface AngularLimitsJoint
        Parameters:
        angle - the reference angle in radians
        See Also:
        AngularLimitsJoint.getLimitsReferenceAngle()