Module org.dyn4j

Class MotorJoint

  • All Implemented Interfaces:
    DataContainer, Shiftable

    public class MotorJoint
    extends Joint
    implements Shiftable, DataContainer
    Implementation a motor joint.

    A motor joint uses a motor to apply forces and torques to move the joined bodies together.

    The motor is limited by a maximum force and torque. By default these are zero and will need to be set before the joint will function properly. Larger values will allow the motor to apply more force and torque to the bodies. This can have two effects. The first is that the bodies will move to their correct positions faster. The second is that the bodies will be moving faster and may overshoot more causing more oscillation. Use the setCorrectionFactor(double) method to help reduce the oscillation.

    The linear and angular targets are the target distance and angle that the bodies should achieve relative to each other's position and rotation. By default, the linear target will be the distance between the two body centers and the angular target will be the relative rotation of the bodies. Use the setLinearTarget(Vector2) and setAngularTarget(double) methods to set the desired relative translation and rotate between the bodies.

    This joint is ideal for character movement as it allows direct control of the motion using targets, but yet still allows interaction with the environment. The best way to achieve this effect is to have the second body be an infinite mass body that doesn't collide with anything. Then, simply set the current position and rotation of the infinite mass body. The character body will move and rotate smoothly, participating in any collision or with other joints to match the infinite mass body.

    Since:
    3.1.0
    Version:
    3.2.1
    Author:
    William Bittle
    See Also:
    Documentation
    • Field Detail

      • linearTarget

        protected Vector2 linearTarget
        The linear target distance from body1's world space center
      • angularTarget

        protected double angularTarget
        The target angle between the two body's angles
      • correctionFactor

        protected double correctionFactor
        The correction factor in the range [0, 1]
      • maximumForce

        protected double maximumForce
        The maximum force the constraint can apply
      • maximumTorque

        protected double maximumTorque
        The maximum torque the constraint can apply
    • Constructor Detail

      • MotorJoint

        public MotorJoint​(Body body1,
                          Body body2)
        Minimal constructor.
        Parameters:
        body1 - the first Body
        body2 - the second Body
        Throws:
        java.lang.NullPointerException - if body1 or body2
        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()
        Returns the anchor point on the first Body in world coordinates.

        Not applicable to this joint. Returns the first body's world center.

        Specified by:
        getAnchor1 in class Joint
        Returns:
        Vector2
      • getAnchor2

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

        Not applicable to this joint. Returns the second body's world center.

        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)
        Description copied from class: Joint
        Returns the torque applied to the Bodys in order to satisfy the constraint in newton-meters.
        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
      • getLinearTarget

        public Vector2 getLinearTarget()
        Returns the desired linear distance along the x and y coordinates from body1's world center.

        To get the world linear target:

         joint.getBody1().getWorldVector(joint.getLinearTarget());
         
        Returns:
        Vector2
      • setLinearTarget

        public void setLinearTarget​(Vector2 target)
        Sets the desired linear distance along the x and y coordinates from body1's world center.
        Parameters:
        target - the desired distance along the x and y coordinates
      • getAngularTarget

        public double getAngularTarget()
        Returns the desired angle between the bodies.
        Returns:
        double
      • setAngularTarget

        public void setAngularTarget​(double target)
        Sets the desired angle between the bodies.
        Parameters:
        target - the desired angle between the bodies
      • getCorrectionFactor

        public double getCorrectionFactor()
        Returns the correction factor.
        Returns:
        double
      • setCorrectionFactor

        public void setCorrectionFactor​(double correctionFactor)
        Sets the correction factor.

        The correction factor controls the rate at which the bodies perform the desired actions. The default is 0.3.

        A value of zero means that the bodies do not perform any action.

        Parameters:
        correctionFactor - the correction factor in the range [0, 1]
      • getMaximumTorque

        public double getMaximumTorque()
        Returns the maximum torque this constraint will apply in newton-meters.
        Returns:
        double
      • setMaximumTorque

        public void setMaximumTorque​(double maximumTorque)
        Sets the maximum torque this constraint will apply in newton-meters.
        Parameters:
        maximumTorque - the maximum torque in newton-meters; in the range [0, ∞]
        Throws:
        java.lang.IllegalArgumentException - if maxTorque is less than zero
      • getMaximumForce

        public double getMaximumForce()
        Returns the maximum force this constraint will apply in newtons.
        Returns:
        double
      • setMaximumForce

        public void setMaximumForce​(double maximumForce)
        Sets the maximum force this constraint will apply in newtons.
        Parameters:
        maximumForce - the maximum force in newtons; in the range [0, ∞]
        Throws:
        java.lang.IllegalArgumentException - if maxForce is less than zero