Class SpringConstraint
- Namespace
- Jitter2.SoftBodies
- Assembly
- Jitter2.dll
Applies a spring force between two anchors to maintain a target distance.
public class SpringConstraint : Constraint<SpringConstraint.SpringData>, IDebugDrawable
- Inheritance
-
SpringConstraint
- Implements
- Inherited Members
Remarks
This constraint is designed for soft body vertices, which act as mass points. Angular velocities of the connected bodies are not taken into account. The spring behavior is controlled by Softness and Bias, which can be set directly or computed from physical parameters using SetSpringParameters(float, float, float).
Properties
Anchor1
Gets or sets the anchor point on the first body in world space.
public JVector Anchor1 { get; set; }
Property Value
Anchor2
Gets or sets the anchor point on the second body in world space.
public JVector Anchor2 { get; set; }
Property Value
Bias
Gets or sets the bias factor (error correction strength) of the spring constraint.
public float Bias { get; set; }
Property Value
- float
Default is 0.2. Higher values correct distance errors more aggressively.
Distance
Gets the current distance between the anchor points.
public float Distance { get; }
Property Value
Impulse
Gets the accumulated solver impulse from the last solved substep.
public float Impulse { get; }
Property Value
Softness
Gets or sets the softness (compliance) of the spring constraint.
public float Softness { get; set; }
Property Value
- float
Default is DefaultLinearSoftness. Higher values allow more positional error but produce a softer spring.
TargetDistance
Gets or sets the target resting distance of the spring.
public float TargetDistance { get; set; }
Property Value
- float
Units: meters. Default is the initial distance between anchors at initialization.
Methods
Create()
Called when the world creates this constraint. Override to set DispatchId to the ID returned by registering the constraint's solver methods.
protected override void Create()
DebugDraw(IDebugDrawer)
Draws a debug visualization of this constraint.
public override void DebugDraw(IDebugDrawer drawer)
Parameters
drawerIDebugDrawerThe debug drawer to receive visualization primitives.
Exceptions
- NotImplementedException
Thrown if the derived class does not override this method.
DebugDraw(IDebugDrawer, in DebugDrawSettings)
Draws this constraint using the supplied visualization settings. Custom constraints that only override DebugDraw(IDebugDrawer) use their default visualization.
public override void DebugDraw(IDebugDrawer drawer, in DebugDrawSettings settings)
Parameters
drawerIDebugDrawersettingsDebugDrawSettings
DebugDraw(IDebugDrawer, ref JVector, in DebugDrawSettings)
Draws the spring with a caller-owned coil reference. Retain this direction between frames to keep the coil continuous through arbitrary motion, including a reversal of the spring axis.
public void DebugDraw(IDebugDrawer drawer, ref JVector reference, in DebugDrawSettings settings)
Parameters
drawerIDebugDrawerreferenceJVectorsettingsDebugDrawSettings
Initialize(JVector, JVector)
Initializes the constraint from world-space anchor points.
public void Initialize(JVector anchor1, JVector anchor2)
Parameters
anchor1JVectorAnchor point on the first rigid body, in world space.
anchor2JVectorAnchor point on the second rigid body, in world space.
Remarks
Default values: Softness = DefaultLinearSoftness, Bias = DefaultLinearBias. The TargetDistance is set to the initial distance between the anchors.
Exceptions
- ArgumentException
Thrown when
anchor1oranchor2contains a non-finite value.
IterateSpringConstraint(ref ConstraintData, in TimeStep)
Performs one iteration of the spring constraint solver.
public static void IterateSpringConstraint(ref ConstraintData constraint, in TimeStep timeStep)
Parameters
constraintConstraintDataThe constraint data reference.
timeStepTimeStepThe full step and substep timing information.
PrepareForIterationSpringConstraint(ref ConstraintData, in TimeStep)
Prepares the spring constraint for iteration.
public static void PrepareForIterationSpringConstraint(ref ConstraintData constraint, in TimeStep timeStep)
Parameters
constraintConstraintDataThe constraint data reference.
timeStepTimeStepThe full step and substep timing information.
ResetWarmStart()
Clears impulses saved from the previous solve, without changing constraint settings.
public override void ResetWarmStart()
Remarks
Use after restoring a saved state or making a sudden change to the bodies, when impulses from their previous state should no longer be reused.
SetSpringParameters(float, float, float)
public void SetSpringParameters(float frequency, float damping, float dt)
Parameters
frequencyfloatThe frequency in Hz.
dampingfloatThe damping ratio (0 = no damping, 1 = critical damping).
dtfloatThe timestep passed to Step(float, bool).
Remarks
Call again if the bodies' masses, translation locks, anchor direction, timestep, or SubstepCount change. When the anchors coincide, the calculation uses the translation direction in which the bodies respond most easily to a force.
Exceptions
- ArgumentOutOfRangeException
Thrown when
frequencyordtis less than or equal to zero, whendampingis negative, or when any value is not finite.
ToString()
Returns a short description of the constraint's ID, connected bodies, and state.
public override string ToString()