-
- Type Parameters:
T
- thePhysicsBody
type
- All Implemented Interfaces:
DataContainer
,Shiftable
,Ownable
public class MotorJoint<T extends PhysicsBody> extends Joint<T> 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)
andsetAngularTarget(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:
- 4.0.0
- Author:
- William Bittle
- See Also:
- Documentation
-
-
Constructor Summary
Constructors Constructor Description MotorJoint(T body1, T body2)
Minimal constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Vector2
getAnchor1()
Returns the anchor point on the firstPhysicsBody
in world coordinates.Vector2
getAnchor2()
Returns the anchor point on the secondPhysicsBody
in world coordinates.double
getAngularTarget()
Returns the desired angle between the bodies.double
getCorrectionFactor()
Returns the correction factor.Vector2
getLinearTarget()
Returns the desired linear distance along the x and y coordinates from body1's world center.double
getMaximumForce()
Returns the maximum force this constraint will apply in newtons.double
getMaximumTorque()
Returns the maximum torque this constraint will apply in newton-meters.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.void
initializeConstraints(TimeStep step, Settings settings)
Performs any initialization of the velocity and position constraints.void
setAngularTarget(double target)
Sets the desired angle between the bodies.void
setCorrectionFactor(double correctionFactor)
Sets the correction factor.void
setLinearTarget(Vector2 target)
Sets the desired linear distance along the x and y coordinates from body1's world center.void
setMaximumForce(double maximumForce)
Sets the maximum force this constraint will apply in newtons.void
setMaximumTorque(double maximumTorque)
Sets the maximum torque this constraint will apply in newton-meters.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()
-
Methods inherited from class Joint
getBody1, getBody2, getOtherBody, getOwner, getUserData, isActive, isCollisionAllowed, isEnabled, isOnIsland, setCollisionAllowed, setOnIsland, setOwner, setUserData
-
Methods inherited from interface DataContainer
getUserData, setUserData
-
-
-
-
Constructor Detail
-
MotorJoint
public MotorJoint(T body1, T body2)
Minimal constructor.- Parameters:
body1
- the firstPhysicsBody
body2
- the secondPhysicsBody
- Throws:
NullPointerException
- if body1 or body2IllegalArgumentException
- if body1 == body2
-
-
Method Detail
-
toString
public String toString()
- Overrides:
toString
in classJoint<T extends PhysicsBody>
-
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 classJoint<T extends PhysicsBody>
- Parameters:
step
- the time step informationsettings
- 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 classJoint<T extends PhysicsBody>
- Parameters:
step
- the time step informationsettings
- 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 classJoint<T extends PhysicsBody>
- Parameters:
step
- the time step informationsettings
- the current world settings- Returns:
- boolean true if the position constraints were solved
-
getAnchor1
public Vector2 getAnchor1()
Returns the anchor point on the firstPhysicsBody
in world coordinates.Not applicable to this joint. Returns the first body's world center.
- Specified by:
getAnchor1
in classJoint<T extends PhysicsBody>
- Returns:
Vector2
-
getAnchor2
public Vector2 getAnchor2()
Returns the anchor point on the secondPhysicsBody
in world coordinates.Not applicable to this joint. Returns the second body's world center.
- Specified by:
getAnchor2
in classJoint<T extends PhysicsBody>
- Returns:
Vector2
-
getReactionForce
public Vector2 getReactionForce(double invdt)
Description copied from class:Joint
Returns the force applied to thePhysicsBody
s in order to satisfy the constraint in newtons.- Specified by:
getReactionForce
in classJoint<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 thePhysicsBody
s in order to satisfy the constraint in newton-meters.- Specified by:
getReactionTorque
in classJoint<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.
-
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:
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:
IllegalArgumentException
- if maxForce is less than zero
-
-