Godot Nature of Code  1.2.0
Nature of Code implemented in Godot Engine
Public Member Functions | List of all members
VerletPhysics.VerletChainBuilder Class Reference

Simple verlet chain builder. Uses a PointConfiguratorFunc to configure VerletPoints on creation. More...

Public Member Functions

delegate void PointConfiguratorFunc (VerletPoint point)
 Point configuration function definition More...
 
 VerletChainBuilder (VerletWorld world, bool pinFirst=true, bool pinLast=false, bool drawIntermediatePoints=false)
 Create a verlet chain builder. More...
 
VerletChainBuilder AddPointAtPosition (float x, float y, PointConfiguratorFunc configurator=null)
 Add chain point at a specific position. More...
 
VerletChainBuilder AddPointAtPosition (Vector2 position, PointConfiguratorFunc configurator=null)
 Add chain point at a specific position. More...
 
VerletChainBuilder AddPointWithOffset (float x, float y, PointConfiguratorFunc configurator=null)
 Add chain point with offset from previous point position. If not already set, the first point will be offset from (0, 0). More...
 
VerletChainBuilder AddPointWithOffset (Vector2 offset, PointConfiguratorFunc configurator=null)
 Add chain point with offset from previous point position. If not already set, the first point will be offset from (0, 0). More...
 
VerletChainBuilder AddPointsWithOffset (int pointCount, float x, float y, PointConfiguratorFunc configurator=null)
 Add multiple chain points with offset from previous points positions. If not already set, the first point will be offset from (0, 0). More...
 
VerletChainBuilder AddPointsWithOffset (int pointCount, Vector2 offset, PointConfiguratorFunc configurator=null)
 Add multiple chain points with offset from previous points positions. If not already set, the first point will be offset from (0, 0). More...
 
void Build (float restingDistance=50, float tearSensitivity=100, float stiffness=1)
 Build the verlet chain. More...
 

Detailed Description

Simple verlet chain builder. Uses a PointConfiguratorFunc to configure VerletPoints on creation.

Definition at line 14 of file VerletChainBuilder.cs.

Constructor & Destructor Documentation

◆ VerletChainBuilder()

VerletPhysics.VerletChainBuilder.VerletChainBuilder ( VerletWorld  world,
bool  pinFirst = true,
bool  pinLast = false,
bool  drawIntermediatePoints = false 
)
inline

Create a verlet chain builder.

Parameters
worldVerlet world
pinFirstPin first chain point
pinLastPin last chain point
drawIntermediatePointsDraw chain intermediate points

Definition at line 32 of file VerletChainBuilder.cs.

33  {
34  this.world = world;
35  this.pinFirst = pinFirst;
36  this.pinLast = pinLast;
37  this.drawIntermediatePoints = drawIntermediatePoints;
38  points = new List<VerletPoint>();
39  }

Member Function Documentation

◆ AddPointAtPosition() [1/2]

VerletChainBuilder VerletPhysics.VerletChainBuilder.AddPointAtPosition ( float  x,
float  y,
PointConfiguratorFunc  configurator = null 
)
inline

Add chain point at a specific position.

Parameters
xX coordinate
yY coordinate
configuratorPoint configurator
Returns
Builder

Definition at line 48 of file VerletChainBuilder.cs.

49  {
50  return AddPointAtPosition(new Vector2(x, y), configurator);
51  }
VerletChainBuilder AddPointAtPosition(float x, float y, PointConfiguratorFunc configurator=null)
Add chain point at a specific position.

◆ AddPointAtPosition() [2/2]

VerletChainBuilder VerletPhysics.VerletChainBuilder.AddPointAtPosition ( Vector2  position,
PointConfiguratorFunc  configurator = null 
)
inline

Add chain point at a specific position.

Parameters
positionPosition vector
configuratorPoint configurator
Returns
Builder

Definition at line 59 of file VerletChainBuilder.cs.

60  {
61  var point = world.CreatePoint();
62  point.MoveToPosition(position);
63 
64  if (points.Count == 0 && pinFirst)
65  {
66  point.PinToCurrentPosition();
67  }
68  else if (!drawIntermediatePoints)
69  {
70  point.Visible = false;
71  }
72 
73  configurator?.Invoke(point);
74  points.Add(point);
75  return this;
76  }
void MoveToPosition(Vector2 position)
Move point to position.
Definition: VerletPoint.cs:63
VerletPoint CreatePoint(Vector2? initialPosition=null, float? mass=null, float? gravityScale=null, float? radius=null, Color? color=null, bool? visible=null)
Create a verlet point.
Definition: VerletWorld.cs:38

◆ AddPointsWithOffset() [1/2]

VerletChainBuilder VerletPhysics.VerletChainBuilder.AddPointsWithOffset ( int  pointCount,
float  x,
float  y,
PointConfiguratorFunc  configurator = null 
)
inline

Add multiple chain points with offset from previous points positions. If not already set, the first point will be offset from (0, 0).

Parameters
pointCountPoint count
xX offset
yY offset
configuratorPoint configurator
Returns
Builder

Definition at line 118 of file VerletChainBuilder.cs.

119  {
120  return AddPointsWithOffset(pointCount, new Vector2(x, y), configurator);
121  }
VerletChainBuilder AddPointsWithOffset(int pointCount, float x, float y, PointConfiguratorFunc configurator=null)
Add multiple chain points with offset from previous points positions. If not already set,...

◆ AddPointsWithOffset() [2/2]

VerletChainBuilder VerletPhysics.VerletChainBuilder.AddPointsWithOffset ( int  pointCount,
Vector2  offset,
PointConfiguratorFunc  configurator = null 
)
inline

Add multiple chain points with offset from previous points positions. If not already set, the first point will be offset from (0, 0).

Parameters
pointCountPoint count
offsetOffset vector
configuratorPoint configurator
Returns
Builder

Definition at line 131 of file VerletChainBuilder.cs.

132  {
133  var builder = this;
134  for (int i = 0; i < pointCount; ++i)
135  {
136  builder = builder.AddPointWithOffset(offset, configurator);
137  }
138  return builder;
139  }

◆ AddPointWithOffset() [1/2]

VerletChainBuilder VerletPhysics.VerletChainBuilder.AddPointWithOffset ( float  x,
float  y,
PointConfiguratorFunc  configurator = null 
)
inline

Add chain point with offset from previous point position. If not already set, the first point will be offset from (0, 0).

Parameters
xX coordinate
yY coordinate
configuratorPoint configurator
Returns
Builder

Definition at line 86 of file VerletChainBuilder.cs.

87  {
88  return AddPointWithOffset(new Vector2(x, y), configurator);
89  }
VerletChainBuilder AddPointWithOffset(float x, float y, PointConfiguratorFunc configurator=null)
Add chain point with offset from previous point position. If not already set, the first point will be...

◆ AddPointWithOffset() [2/2]

VerletChainBuilder VerletPhysics.VerletChainBuilder.AddPointWithOffset ( Vector2  offset,
PointConfiguratorFunc  configurator = null 
)
inline

Add chain point with offset from previous point position. If not already set, the first point will be offset from (0, 0).

Parameters
offsetOffset vector
configuratorPoint configurator
Returns
Builder

Definition at line 98 of file VerletChainBuilder.cs.

99  {
100  Vector2 prevPosition = Vector2.Zero;
101  if (points.Count > 0)
102  {
103  prevPosition = points[^1].GlobalPosition;
104  }
105 
106  return AddPointAtPosition(prevPosition + offset, configurator);
107  }

◆ Build()

void VerletPhysics.VerletChainBuilder.Build ( float  restingDistance = 50,
float  tearSensitivity = 100,
float  stiffness = 1 
)
inline

Build the verlet chain.

Parameters
restingDistanceResting distance
tearSensitivityDistance required to break the chain. Use -1 to create an unbreakable chain.
stiffnessStiffness of the chain

Definition at line 147 of file VerletChainBuilder.cs.

148  {
149  if (points.Count < 2)
150  {
151  GD.PrintErr("Bad points length for chain. Need to be >= 2");
152  return;
153  }
154 
155  // Pin last
156  var lastPoint = points[^1];
157  if (pinLast)
158  {
159  lastPoint.PinToCurrentPosition();
160  }
161 
162  // Show last
163  lastPoint.Visible = true;
164 
165  VerletPoint prevPoint = points[0];
166  for (int i = 1; i < points.Count; ++i)
167  {
168  var currPoint = points[i];
169 
170  var link = world.CreateLink(prevPoint, currPoint);
171  link.RestingDistance = restingDistance;
172  link.TearSensitivity = tearSensitivity;
173  link.Stiffness = stiffness;
174 
175  prevPoint = currPoint;
176  }
177  }
VerletLink CreateLink(VerletPoint a, VerletPoint b, float? restingDistance=null, float? minimalDistance=null, float? maximalDistance=null, float? tearSensitivity=null, float? tearSensitivityFactor=null, float? stiffness=null, float? width=null, Color? color=null, bool? visible=null)
Create a verlet link.
Definition: VerletWorld.cs:73

◆ PointConfiguratorFunc()

delegate void VerletPhysics.VerletChainBuilder.PointConfiguratorFunc ( VerletPoint  point)

Point configuration function definition


The documentation for this class was generated from the following file: