- Object
-
- AbstractJoint<T>
-
- AbstractPairedBodyJoint<T>
-
- DistanceJoint<T>
-
- Type Parameters:
T
- thePhysicsBody
type
- All Implemented Interfaces:
DataContainer
,Joint<T>
,LinearLimitsJoint
,LinearSpringJoint
,PairedBodyJoint<T>
,Shiftable
,Ownable
public class DistanceJoint<T extends PhysicsBody> extends AbstractPairedBodyJoint<T> implements LinearLimitsJoint, LinearSpringJoint, PairedBodyJoint<T>, Joint<T>, Shiftable, DataContainer, Ownable
Implementation of a fixed length distance joint with optional, spring-damper and limits.Given the two world space anchor points a distance is computed and used to constrain the attached
PhysicsBody
s 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. The rest distance determines the fixed distance and can be changed usingsetRestDistance(double)
. The rest distance must be zero or greater.This joint doubles as a spring/damper distance joint where the length can change but is constantly approaching the rest distance. Enable the spring- damper by setting the frequency and damping ratio values and using the
setSpringEnabled(boolean)
andsetSpringDamperEnabled(boolean)
methods. A good starting point is a frequency of 8.0 and damping ratio of 0.3 then adjust as necessary. You can also impose a maximum force the spring will apply by usingsetMaximumSpringForce(double)
. The maximum force needs to be enabled usingsetMaximumSpringForceEnabled(boolean)
method. As with all spring-damper enabled joints, the spring must be enabled for the damper to be applied. Also note that when the damper is disabled, the spring still experiences "damping" aka energy loss - this is a side effect of the solver and intended behavior.This joint also supports limits. The joint will only accept positive limits (in contrast to other joints). A lower limit of zero (the minimum valid lower limit) means that the distance can reach zero, where the anchor points overlap. Setting the limits to the same value will create a fixed length distance joint, but this is not recommended as you will get a less stable result - instead disable the limits and set the rest distance using the
setRestDistance(double)
method. You can enable the upper and lower limits independently using thesetLowerLimitEnabled(boolean)
andsetUpperLimitEnabled(boolean)
methods. When limits are enabled, the rest distance is ignored. You can combine limits and spring-damper by enabling as mentioned earlier. In this case, the rest distance is used for the spring-damper and the limits control the maximum and minimum extension of the spring. The limit interval does not have to include the rest distance. If the lower and upper limits are equal or near equal (2 times theSettings.getLinearTolerance()
) the joint will be treated as a fixed length joint.- Since:
- 1.0.0
- Version:
- 5.0.0
- Author:
- William Bittle
- See Also:
- Documentation, Distance Constraint
-
-
Field Summary
Fields Modifier and Type Field Description protected Vector2
localAnchor1
The local anchor point on the firstPhysicsBody
protected Vector2
localAnchor2
The local anchor point on the secondPhysicsBody
protected double
lowerLimit
The minimum distance between the two world space anchor pointsprotected boolean
lowerLimitEnabled
Whether the minimum distance is enabledprotected double
restDistance
The rest distanceprotected boolean
springDamperEnabled
True if the spring's damper is enabledprotected double
springDampingRatio
The damping ratioprotected boolean
springEnabled
True if the spring is enabledprotected double
springFrequency
The oscillation frequency in hzprotected double
springMaximumForce
The maximum force the spring can applyprotected boolean
springMaximumForceEnabled
True if the spring maximum force is enabledprotected int
springMode
The current spring modeprotected double
springStiffness
The stiffness (k) of the springprotected double
upperLimit
The maximum distance between the two world space anchor pointsprotected boolean
upperLimitEnabled
Whether the maximum distance is enabled-
Fields inherited from class AbstractPairedBodyJoint
body1, body2
-
Fields inherited from class AbstractJoint
bodies, collisionAllowed, owner, userData
-
Fields inherited from interface Joint
SPRING_MODE_FREQUENCY, SPRING_MODE_STIFFNESS
-
-
Constructor Summary
Constructors Constructor Description DistanceJoint(T body1, T body2, Vector2 anchor1, Vector2 anchor2)
Minimal constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Vector2
getAnchor1()
Returns the world-space anchor point on the first body.Vector2
getAnchor2()
Returns the world-space anchor point on the second body.double
getCurrentDistance()
Returns the current distance between the anchor points.double
getLowerLimit()
Returns the lower limit in meters.double
getMaximumSpringForce()
Returns the maximum spring force that will be applied.Vector2
getReactionForce(double invdt)
Returns the force applied to thePhysicsBody
s in order to satisfy the constraint in newtons.double
getReactionTorque(double invdt)
Returns the torque applied to thePhysicsBody
s in order to satisfy the constraint in newton-meters.double
getRestDistance()
Returns the rest distance between the two constrainedPhysicsBody
s in meters.double
getSpringDampingRatio()
Returns the damping ratio for the spring's damper.double
getSpringForce(double invdt)
Returns the force in netwons applied by the spring in the last timestep.double
getSpringFrequency()
Returns the spring frequency.int
getSpringMode()
Returns the current spring mode.double
getSpringStiffness()
Returns the spring stiffness.double
getUpperLimit()
Returns the upper limit in meters.void
initializeConstraints(TimeStep step, Settings settings)
Performs any initialization of the velocity and position constraints.boolean
isLowerLimitEnabled()
Returns true if the lower limit is enabled.boolean
isMaximumSpringForceEnabled()
Returns true if the spring force is limited to the maximum.boolean
isSpringDamperEnabled()
Returns true if the damper is enabled for the spring.boolean
isSpringEnabled()
Returns true if the spring is enabled.boolean
isUpperLimitEnabled()
Returns true if the upper limit is enabled.void
setLimits(double limit)
Sets both the lower and upper limits to the given limit.void
setLimits(double lowerLimit, double upperLimit)
Sets both the lower and upper limits.void
setLimitsEnabled(boolean flag)
Enables or disables both the lower and upper limits.void
setLimitsEnabled(double limit)
Sets both the lower and upper limits to the given limit and enables both.void
setLimitsEnabled(double lowerLimit, double upperLimit)
Sets both the lower and upper limits and enables both.void
setLowerLimit(double lowerLimit)
Sets the lower limit in meters.void
setLowerLimitEnabled(boolean flag)
Sets whether the lower limit is enabled.void
setMaximumSpringForce(double maximum)
Sets the maximum force the spring can apply.void
setMaximumSpringForceEnabled(boolean enabled)
Sets whether the spring force is limited to the maximum.void
setRestDistance(double distance)
Sets the rest distance between the two constrainedPhysicsBody
s in meters.void
setSpringDamperEnabled(boolean enabled)
Sets whether the spring's damper is enabled or not.void
setSpringDampingRatio(double dampingRatio)
Sets the damping ratio.void
setSpringEnabled(boolean enabled)
Sets whether the spring is enabled or not.void
setSpringFrequency(double frequency)
Sets the spring frequency.void
setSpringStiffness(double stiffness)
Sets the spring stiffness.void
setUpperLimit(double upperLimit)
Sets the upper limit in meters.void
setUpperLimitEnabled(boolean flag)
Sets whether the upper limit is enabled.void
shift(Vector2 shift)
Translates the object to match the given coordinate shift.boolean
solvePositionConstraints(TimeStep step, Settings settings)
Solves the position constraints.void
solveVelocityConstraints(TimeStep step, Settings settings)
Solves the velocity constraints.String
toString()
protected void
updateSpringCoefficients()
Computes the spring coefficients from the current state of the joint.-
Methods inherited from class AbstractPairedBodyJoint
getBody, getBody1, getBody2, getBodyCount, getOtherBody, getReducedInertia, getReducedMass, isEnabled, isMember, setCollisionAllowed
-
Methods inherited from class AbstractJoint
getBodies, getBodyIterator, getConstraintImpulseMixing, getErrorReductionParameter, getFrequency, getNaturalFrequency, getNaturalFrequency, getOwner, getSpringDampingCoefficient, getSpringStiffness, getUserData, isCollisionAllowed, setOwner, setUserData
-
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface DataContainer
getUserData, setUserData
-
Methods inherited from interface Joint
getBodies, getBody, getBodyCount, getBodyIterator, isCollisionAllowed, isEnabled, isMember, setCollisionAllowed
-
Methods inherited from interface PairedBodyJoint
getBody1, getBody2, getOtherBody
-
-
-
-
Field Detail
-
localAnchor1
protected final Vector2 localAnchor1
The local anchor point on the firstPhysicsBody
-
localAnchor2
protected final Vector2 localAnchor2
The local anchor point on the secondPhysicsBody
-
restDistance
protected double restDistance
The rest distance
-
springEnabled
protected boolean springEnabled
True if the spring is enabled
-
springMode
protected int springMode
The current spring mode
-
springFrequency
protected double springFrequency
The oscillation frequency in hz
-
springStiffness
protected double springStiffness
The stiffness (k) of the spring
-
springDamperEnabled
protected boolean springDamperEnabled
True if the spring's damper is enabled
-
springDampingRatio
protected double springDampingRatio
The damping ratio
-
springMaximumForceEnabled
protected boolean springMaximumForceEnabled
True if the spring maximum force is enabled
-
springMaximumForce
protected double springMaximumForce
The maximum force the spring can apply
-
upperLimit
protected double upperLimit
The maximum distance between the two world space anchor points
-
lowerLimit
protected double lowerLimit
The minimum distance between the two world space anchor points
-
upperLimitEnabled
protected boolean upperLimitEnabled
Whether the maximum distance is enabled
-
lowerLimitEnabled
protected boolean lowerLimitEnabled
Whether the minimum distance is enabled
-
-
Constructor Detail
-
DistanceJoint
public DistanceJoint(T body1, T body2, Vector2 anchor1, Vector2 anchor2)
Minimal constructor.Creates a fixed distance
Joint
where the joinedPhysicsBody
s do not participate in collision detection and resolution.- Parameters:
body1
- the firstPhysicsBody
body2
- the secondPhysicsBody
anchor1
- in world coordinatesanchor2
- in world coordinates- Throws:
NullPointerException
- if body1, body2, anchor1, or anchor2 is nullIllegalArgumentException
- if body1 == body2
-
-
Method Detail
-
toString
public String toString()
- Overrides:
toString
in classAbstractJoint<T extends PhysicsBody>
-
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 interfaceJoint<T extends PhysicsBody>
- Parameters:
step
- the time step informationsettings
- 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 interfaceJoint<T extends PhysicsBody>
- Parameters:
step
- the time step informationsettings
- 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 interfaceJoint<T extends PhysicsBody>
- Parameters:
step
- the time step informationsettings
- 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 on the first body.- Returns:
Vector2
-
getAnchor2
public Vector2 getAnchor2()
Returns the world-space anchor point on the second body.- Returns:
Vector2
-
getReactionForce
public Vector2 getReactionForce(double invdt)
Description copied from interface:Joint
Returns the force applied to thePhysicsBody
s in order to satisfy the constraint in newtons.- Specified by:
getReactionForce
in interfaceJoint<T extends PhysicsBody>
- Parameters:
invdt
- the inverse delta time- Returns:
Vector2
-
getReactionTorque
public double getReactionTorque(double invdt)
Returns the torque applied to thePhysicsBody
s in order to satisfy the constraint in newton-meters.Not applicable to this joint. Always returns zero.
- Specified by:
getReactionTorque
in interfaceJoint<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.
-
getRestDistance
public double getRestDistance()
Returns the rest distance between the two constrainedPhysicsBody
s in meters.- Returns:
- double
- Since:
- 4.2.0
-
setRestDistance
public void setRestDistance(double distance)
Sets the rest distance between the two constrainedPhysicsBody
s in meters.- Parameters:
distance
- the distance in meters- Throws:
IllegalArgumentException
- if distance is less than zero- Since:
- 4.2.0
-
getCurrentDistance
public double getCurrentDistance()
Returns the current distance between the anchor points.- Returns:
- double
- Since:
- 4.2.0
-
getSpringDampingRatio
public double getSpringDampingRatio()
Description copied from interface:LinearSpringJoint
Returns the damping ratio for the spring's damper.- Specified by:
getSpringDampingRatio
in interfaceLinearSpringJoint
- Returns:
- double
-
setSpringDampingRatio
public void setSpringDampingRatio(double dampingRatio)
Description copied from interface:LinearSpringJoint
Sets the damping ratio.Larger values reduce the oscillation of the spring.
- Specified by:
setSpringDampingRatio
in interfaceLinearSpringJoint
- Parameters:
dampingRatio
- the damping ratio; in the range (0, 1]
-
getSpringFrequency
public double getSpringFrequency()
Description copied from interface:LinearSpringJoint
Returns the spring frequency.- Specified by:
getSpringFrequency
in interfaceLinearSpringJoint
- Returns:
- double
-
getSpringStiffness
public double getSpringStiffness()
Description copied from interface:LinearSpringJoint
Returns the spring stiffness.- Specified by:
getSpringStiffness
in interfaceLinearSpringJoint
- Returns:
- double
-
setSpringFrequency
public void setSpringFrequency(double frequency)
Description copied from interface:LinearSpringJoint
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 interfaceLinearSpringJoint
- Parameters:
frequency
- the spring frequency in hz; must be greater than zero
-
setSpringStiffness
public void setSpringStiffness(double stiffness)
Description copied from interface:LinearSpringJoint
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 interfaceLinearSpringJoint
- Parameters:
stiffness
- the spring stiffness (k); must be greater than zero
-
getMaximumSpringForce
public double getMaximumSpringForce()
Description copied from interface:LinearSpringJoint
Returns the maximum spring force that will be applied.- Specified by:
getMaximumSpringForce
in interfaceLinearSpringJoint
- Returns:
- double
-
setMaximumSpringForce
public void setMaximumSpringForce(double maximum)
Description copied from interface:LinearSpringJoint
Sets the maximum force the spring can apply.- Specified by:
setMaximumSpringForce
in interfaceLinearSpringJoint
- Parameters:
maximum
- the maximum force
-
isMaximumSpringForceEnabled
public boolean isMaximumSpringForceEnabled()
Description copied from interface:LinearSpringJoint
Returns true if the spring force is limited to the maximum.- Specified by:
isMaximumSpringForceEnabled
in interfaceLinearSpringJoint
- Returns:
- boolean
-
setMaximumSpringForceEnabled
public void setMaximumSpringForceEnabled(boolean enabled)
Description copied from interface:LinearSpringJoint
Sets whether the spring force is limited to the maximum.- Specified by:
setMaximumSpringForceEnabled
in interfaceLinearSpringJoint
- Parameters:
enabled
- true if the spring force should be limited
-
isSpringEnabled
public boolean isSpringEnabled()
Description copied from interface:LinearSpringJoint
Returns true if the spring is enabled.- Specified by:
isSpringEnabled
in interfaceLinearSpringJoint
- Returns:
- boolean
-
setSpringEnabled
public void setSpringEnabled(boolean enabled)
Description copied from interface:LinearSpringJoint
Sets whether the spring is enabled or not.- Specified by:
setSpringEnabled
in interfaceLinearSpringJoint
- Parameters:
enabled
- true if the spring should be enabled
-
isSpringDamperEnabled
public boolean isSpringDamperEnabled()
Description copied from interface:LinearSpringJoint
Returns true if the damper is enabled for the spring.NOTE: for the damper to have any effect, the spring must be enabled.
- Specified by:
isSpringDamperEnabled
in interfaceLinearSpringJoint
- Returns:
- boolean
- See Also:
LinearSpringJoint.isSpringEnabled()
-
setSpringDamperEnabled
public void setSpringDamperEnabled(boolean enabled)
Description copied from interface:LinearSpringJoint
Sets whether the spring's damper is enabled or not.NOTE: for the damper to have any effect, the spring must be enabled.
- Specified by:
setSpringDamperEnabled
in interfaceLinearSpringJoint
- Parameters:
enabled
- true if the damper should be enabled- See Also:
LinearSpringJoint.setSpringEnabled(boolean)
-
getSpringForce
public double getSpringForce(double invdt)
Description copied from interface:LinearSpringJoint
Returns the force in netwons applied by the spring in the last timestep.- Specified by:
getSpringForce
in interfaceLinearSpringJoint
- Parameters:
invdt
- the inverse delta time- Returns:
- double
-
getSpringMode
public int getSpringMode()
Description copied from interface:LinearSpringJoint
Returns the current spring mode.NOTE: The spring mode is set automatically when you call either
LinearSpringJoint.setSpringFrequency(double)
orLinearSpringJoint.setSpringStiffness(double)
. Use this method to store the spring mode when saving the simulation state and use the value to call eitherLinearSpringJoint.setSpringFrequency(double)
orLinearSpringJoint.setSpringStiffness(double)
.- Specified by:
getSpringMode
in interfaceLinearSpringJoint
- Returns:
- int
- See Also:
Joint.SPRING_MODE_FREQUENCY
,Joint.SPRING_MODE_STIFFNESS
-
getUpperLimit
public double getUpperLimit()
Description copied from interface:LinearLimitsJoint
Returns the upper limit in meters.- Specified by:
getUpperLimit
in interfaceLinearLimitsJoint
- Returns:
- double
-
setUpperLimit
public void setUpperLimit(double upperLimit)
Sets the upper limit in meters.- Specified by:
setUpperLimit
in interfaceLinearLimitsJoint
- Parameters:
upperLimit
- the upper limit in meters; must be zero or greater- Throws:
IllegalArgumentException
- if upperLimit is less than zero or upperLimit is less than the current lower limit
-
setUpperLimitEnabled
public void setUpperLimitEnabled(boolean flag)
Description copied from interface:LinearLimitsJoint
Sets whether the upper limit is enabled.- Specified by:
setUpperLimitEnabled
in interfaceLinearLimitsJoint
- Parameters:
flag
- true if the upper limit should be enabled
-
isUpperLimitEnabled
public boolean isUpperLimitEnabled()
Description copied from interface:LinearLimitsJoint
Returns true if the upper limit is enabled.- Specified by:
isUpperLimitEnabled
in interfaceLinearLimitsJoint
- Returns:
- boolean true if the upper limit is enabled
-
getLowerLimit
public double getLowerLimit()
Description copied from interface:LinearLimitsJoint
Returns the lower limit in meters.- Specified by:
getLowerLimit
in interfaceLinearLimitsJoint
- Returns:
- double
-
setLowerLimit
public void setLowerLimit(double lowerLimit)
Sets the lower limit in meters.- Specified by:
setLowerLimit
in interfaceLinearLimitsJoint
- Parameters:
lowerLimit
- the lower limit in meters; must be zero or greater- Throws:
IllegalArgumentException
- if lowerLimit is less than zero or lowerLimit is greater than the current upper limit
-
setLowerLimitEnabled
public void setLowerLimitEnabled(boolean flag)
Description copied from interface:LinearLimitsJoint
Sets whether the lower limit is enabled.- Specified by:
setLowerLimitEnabled
in interfaceLinearLimitsJoint
- Parameters:
flag
- true if the lower limit should be enabled
-
isLowerLimitEnabled
public boolean isLowerLimitEnabled()
Description copied from interface:LinearLimitsJoint
Returns true if the lower limit is enabled.- Specified by:
isLowerLimitEnabled
in interfaceLinearLimitsJoint
- Returns:
- boolean true if the lower limit is enabled
-
setLimits
public void setLimits(double lowerLimit, double upperLimit)
Sets both the lower and upper limits.- Specified by:
setLimits
in interfaceLinearLimitsJoint
- Parameters:
lowerLimit
- the lower limit in meters; must be zero or greaterupperLimit
- the upper limit in meters; must be zero or greater- Throws:
IllegalArgumentException
- if lowerLimit is less than zero, the upperLimit is less than zero, or lowerLimit is greater than the upperLimit
-
setLimitsEnabled
public void setLimitsEnabled(double lowerLimit, double upperLimit)
Sets both the lower and upper limits and enables both.- Specified by:
setLimitsEnabled
in interfaceLinearLimitsJoint
- Parameters:
lowerLimit
- the lower limit in meters; must be zero or greaterupperLimit
- the upper limit in meters; must be zero or greater- Throws:
IllegalArgumentException
- if lowerLimit is less than zero, the upperLimit is less than zero, or lowerLimit is greater than the upperLimit
-
setLimitsEnabled
public void setLimitsEnabled(boolean flag)
Description copied from interface:LinearLimitsJoint
Enables or disables both the lower and upper limits.- Specified by:
setLimitsEnabled
in interfaceLinearLimitsJoint
- Parameters:
flag
- true if both limits should be enabled
-
setLimits
public void setLimits(double limit)
Sets both the lower and upper limits to the given limit.- Specified by:
setLimits
in interfaceLinearLimitsJoint
- Parameters:
limit
- the lower and upper limit in meters; must be zero or greater- Throws:
IllegalArgumentException
- if limit is less than zero
-
setLimitsEnabled
public void setLimitsEnabled(double limit)
Sets both the lower and upper limits to the given limit and enables both.- Specified by:
setLimitsEnabled
in interfaceLinearLimitsJoint
- Parameters:
limit
- the lower and upper limit in meters; must be zero or greater- Throws:
IllegalArgumentException
- if limit is less than zero
-
-