- Object
-
- AbstractJoint<T>
-
- AbstractSingleBodyJoint<T>
-
- PinJoint<T>
-
- Type Parameters:
T
- thePhysicsBody
type
- All Implemented Interfaces:
DataContainer
,Joint<T>
,LinearSpringJoint
,SingleBodyJoint<T>
,Shiftable
,Ownable
public class PinJoint<T extends PhysicsBody> extends AbstractSingleBodyJoint<T> implements LinearSpringJoint, SingleBodyJoint<T>, Joint<T>, Shiftable, DataContainer, Ownable
Implementation of a pin joint.A pin joint is a joint that pins a body to a specified world space point. This joint will attempt to place the given anchor point at the target position.
NOTE: The anchor point does not have to be within the bounds of the body.
By default the target position will be the given world space anchor. Use the
setTarget(Vector2)
method to set a different target.By default the pin joint is setup with a linear spring-damper with a maximum force. The defaults are a frequency of 8.0, damping ratio of 0.3 and a maximum force of 1000.0.
You can disable the spring-damper using the
setSpringEnabled(boolean)
method. This turns the joint into an unaryMotorJoint
. You can use thesetCorrectionFactor(double)
method to set how quickly the constraint violation should be resolved and you can set the maximum force the correction should apply using thesetMaximumCorrectionForce(double)
. Both of these settings are only relevant when the spring-damper is disabled.The
getAnchor()
method returns the anchor point on body in world space. ThegetTarget()
returns the target point in world space.Renamed from MouseJoint in 3.2.0. Can function without a spring-damper as of 5.0.0.
- Since:
- 1.0.0
- Version:
- 5.0.0
- Author:
- William Bittle
- See Also:
- Documentation
-
-
Field Summary
Fields Modifier and Type Field Description protected double
correctionFactor
The correction factor in the range [0, 1]protected double
correctionMaximumForce
The maximum force the constraint can applyprotected Vector2
localAnchor
The local anchor point for the bodyprotected 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 Vector2
target
The world space target point-
Fields inherited from class AbstractSingleBodyJoint
body
-
Fields inherited from class AbstractJoint
bodies, collisionAllowed, owner, userData
-
Fields inherited from interface Joint
SPRING_MODE_FREQUENCY, SPRING_MODE_STIFFNESS
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Vector2
getAnchor()
Returns the anchor point on the body in world space.double
getCorrectionFactor()
Returns the correction factor.double
getCorrectionForce(double invdt)
Returns the correction force from the last step.double
getMaximumCorrectionForce()
Returns the maximum correction force this constraint will apply in newtons.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
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.Vector2
getTarget()
Returns the target point in world coordinatesvoid
initializeConstraints(TimeStep step, Settings settings)
Performs any initialization of the velocity and position constraints.boolean
isCollisionAllowed()
Returns true if collision between the joinedPhysicsBody
s is allowed.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.void
setCorrectionFactor(double correctionFactor)
Sets the correction factor.void
setMaximumCorrectionForce(double maximumForce)
Sets the maximum correction force this constraint will apply in newtons.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
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
setTarget(Vector2 target)
Returns the target point in world coordinates.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 AbstractSingleBodyJoint
getBody, getBody, getBodyCount, isEnabled, isMember
-
Methods inherited from class AbstractJoint
getBodies, getBodyIterator, getConstraintImpulseMixing, getErrorReductionParameter, getFrequency, getNaturalFrequency, getNaturalFrequency, getOwner, getSpringDampingCoefficient, getSpringStiffness, getUserData, setCollisionAllowed, 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, isEnabled, isMember, setCollisionAllowed
-
Methods inherited from interface SingleBodyJoint
getBody
-
-
-
-
Field Detail
-
target
protected final Vector2 target
The world space target point
-
localAnchor
protected final Vector2 localAnchor
The local anchor point for the body
-
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
-
correctionFactor
protected double correctionFactor
The correction factor in the range [0, 1]
-
correctionMaximumForce
protected double correctionMaximumForce
The maximum force the constraint can apply
-
-
Constructor Detail
-
PinJoint
public PinJoint(T body, Vector2 anchor)
Full constructor.- Parameters:
body
- the body to attach the joint toanchor
- the anchor point on the body- Throws:
NullPointerException
- if body or anchor is null
-
-
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.
-
getAnchor
public Vector2 getAnchor()
Returns the anchor point on the body in world space.- Returns:
- Vector2
-
setTarget
public void setTarget(Vector2 target)
Returns the target point in world coordinates.- Parameters:
target
- the target point- Throws:
NullPointerException
- if target is null
-
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
-
isCollisionAllowed
public boolean isCollisionAllowed()
Description copied from interface:Joint
Returns true if collision between the joinedPhysicsBody
s is allowed.- Specified by:
isCollisionAllowed
in interfaceJoint<T extends PhysicsBody>
- Overrides:
isCollisionAllowed
in classAbstractJoint<T extends PhysicsBody>
- Returns:
- boolean
-
shift
public void shift(Vector2 shift)
Description copied from interface:Shiftable
Translates the object to match the given coordinate shift.
-
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
-
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
-
getCorrectionFactor
public double getCorrectionFactor()
Returns the correction factor.- Returns:
- double
- Since:
- 5.0.0
-
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]- Since:
- 5.0.0
-
getMaximumCorrectionForce
public double getMaximumCorrectionForce()
Returns the maximum correction force this constraint will apply in newtons.- Returns:
- double
- Since:
- 5.0.0
-
setMaximumCorrectionForce
public void setMaximumCorrectionForce(double maximumForce)
Sets the maximum correction force this constraint will apply in newtons.- Parameters:
maximumForce
- the maximum force in newtons; in the range [0, ∞]- Throws:
IllegalArgumentException
- if maxForce is less than zero- Since:
- 5.0.0
-
getCorrectionForce
public double getCorrectionForce(double invdt)
Returns the correction force from the last step.- Parameters:
invdt
- the inverse delta time- 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
-
-