Class SpringConstraint
- Namespace
- Jitter2.SoftBodies
- Assembly
- Jitter2.dll
Constrains two bodies to maintain a target distance between anchor points, applying spring-like forces. Removes one translational degree of freedom along the line connecting the anchors.
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 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 impulse applied by the spring.
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 0.001. Higher values allow more positional error but produce a softer spring. Scaled by inverse timestep during solving.
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()
Sets the Iterate and PrepareForIteration function pointer fields on this instance. Override this in derived classes to assign the correct solver methods. The pointers are later written into ConstraintData when the constraint is enabled.
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.
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
Computes local anchor offsets from the current body positions. Default values: Softness = DefaultLinearSoftness, Bias = DefaultLinearBias. The TargetDistance is set to the initial distance between the anchors.
IterateSpringConstraint(ref ConstraintData, float)
Performs one iteration of the spring constraint solver.
public static void IterateSpringConstraint(ref ConstraintData constraint, float idt)
Parameters
constraintConstraintDataThe constraint data reference.
idtfloatThe inverse substep duration (1/dt).
PrepareForIterationSpringConstraint(ref ConstraintData, float)
Prepares the spring constraint for iteration.
public static void PrepareForIterationSpringConstraint(ref ConstraintData constraint, float idt)
Parameters
constraintConstraintDataThe constraint data reference.
idtfloatThe inverse substep duration (1/dt).
SetSpringParameters(float, float, float)
Sets the spring parameters using physical properties. This method calculates and sets the Softness and Bias properties. It assumes that the mass of the involved bodies and the timestep size does not change.
public void SetSpringParameters(float frequency, float damping, float dt)