Godot Nature of Code  1.2.0
Nature of Code implemented in Godot Engine
Public Member Functions | Public Attributes | Protected Member Functions | List of all members
Agents.SimpleVehicle Class Reference

Vehicle with force and steering. Tracks a target. More...

Inheritance diagram for Agents.SimpleVehicle:
Forces.SimpleMover Agents.RoundVehicle Agents.SimpleBoid

Public Member Functions

 SimpleVehicle ()
 Create a default vehicle. More...
 
- Public Member Functions inherited from Forces.SimpleMover
 SimpleMover ()
 Create a simple wrapping mover with a circle mesh. More...
 
 SimpleMover (WrapModeEnum wrapMode)
 Create a simple mover with a custom wrap mode and a circle mesh. More...
 
virtual void ApplyForce (Vector2 force)
 Apply force on mover. More...
 
virtual void ApplyAngularForce (float force)
 Apply angular force on mover. More...
 
virtual void ApplyFriction (float coef)
 Apply friction on mover. More...
 
virtual void ApplyAngularFriction (float coef)
 Apply angular friction on mover. More...
 
virtual void ApplyDrag (float coef)
 Apply drag on mover. More...
 
virtual void ApplyDamping (float coef)
 Apply damping on mover. More...
 

Public Attributes

SimpleMover Target
 Target More...
 
SimpleFlowField TargetFlow
 Flow target More...
 
SimplePath TargetPath
 Path target More...
 
float MaxForce = 0.1f
 Max force More...
 
float ArriveDistance = -1
 Arrive distance. Use -1 to disable. More...
 
float DetectionScanLength = 25
 Detection scan length More...
 
float DetectionTargetOffset = 25
 Detection target offset More...
 
float DetectionAlignmentRadius = 50
 Detection alignment radius More...
 
bool DebugDraw
 Debug draw More...
 
bool SeparationEnabled
 Enable separation group behavior More...
 
bool CohesionEnabled
 Enable cohesion group behavior More...
 
bool AlignmentEnabled
 Enable alignment group behavior More...
 
bool LateralMoveEnabled
 Enable lateral move group behavior More...
 
List< SimpleVehicleVehicleGroupList
 Vehicle group list More...
 
float SeekForceFactor = 1
 Seek force factor More...
 
float SeparationForceFactor = 1
 Separation force factor More...
 
float CohesionForceFactor = 1
 Cohesion force factor More...
 
float AlignmentForceFactor = 1
 Alignment force factor More...
 
float LateralMoveForceFactor = 1
 Lateral move force factor More...
 
- Public Attributes inherited from Forces.SimpleMover
Vector2 Velocity = Vector2.Zero
 Current velocity More...
 
Vector2 Acceleration = Vector2.Zero
 Current acceleration More...
 
float AngularVelocity
 Current angular velocity More...
 
float AngularAcceleration
 Current angular acceleration More...
 
float MaxVelocity = 10
 Max velocity More...
 
float MaxAngularVelocity = 0.1f
 Max angular velocity More...
 
float Mass = 1
 Mass More...
 
WrapModeEnum WrapMode
 Wrapping mode More...
 
bool DisableForces
 Disable forces More...
 
bool SyncRotationOnVelocity
 Synchronize rotation on velocity More...
 
SimpleMesh Mesh
 Mesh More...
 

Protected Member Functions

Vector2 Separate (List< SimpleVehicle > vehicles)
 Separate from other vehicles. More...
 
Vector2 Regroup (List< SimpleVehicle > vehicles)
 Regroup with other vehicles. More...
 
virtual Vector2 Seek (Vector2 position)
 Drive and steer towards target position. More...
 
Vector2 FollowFlow ()
 Drive and steer following a flow field. More...
 
Vector2 FollowPath ()
 Drive and steer following a path. Always follow the path from A to B. More...
 
virtual Vector2 Align (List< SimpleVehicle > vehicles)
 Align with other vehicles. More...
 
virtual Vector2 MoveLaterally (List< SimpleVehicle > vehicles)
 Move laterally from other vehicles. More...
 
override void UpdateAcceleration ()
 Update acceleration value. More...
 

Additional Inherited Members

- Public Types inherited from Forces.SimpleMover
enum class  WrapModeEnum { Wrap , Bounce , None }
 Wrapping mode enum. More...
 
- Protected Attributes inherited from Forces.SimpleMover
CollisionShape2D collisionShape2D
 Collision shape More...
 
- Properties inherited from Forces.SimpleMover
bool Drawing [get, set]
 Draw mesh More...
 
Vector2 MeshSize [get, set]
 Mesh size More...
 
float Radius [get, set]
 Mesh radius More...
 

Detailed Description

Vehicle with force and steering. Tracks a target.

Definition at line 15 of file SimpleVehicle.cs.

Constructor & Destructor Documentation

◆ SimpleVehicle()

Agents.SimpleVehicle.SimpleVehicle ( )
inline

Create a default vehicle.

Definition at line 81 of file SimpleVehicle.cs.

82  {
84  Mesh.MeshSize = new Vector2(40, 20);
85 
87  MaxVelocity = 4;
88  MaxForce = 0.4f;
89 
90  Name = "SimpleVehicle";
91 
92  // Disable physics logic
93  collisionShape2D.Disabled = true;
94  }
float MaxForce
Max force
Simple "mesh" drawing. Can be a circle, square, custom drawing or custom texture.
Definition: SimpleMesh.cs:9
TypeEnum MeshType
Mesh type
Definition: SimpleMesh.cs:35
Vector2 MeshSize
Mesh size
Definition: SimpleMesh.cs:32
TypeEnum
Mesh type enum value.
Definition: SimpleMesh.cs:14
bool SyncRotationOnVelocity
Synchronize rotation on velocity
Definition: SimpleMover.cs:54
SimpleMesh Mesh
Mesh
Definition: SimpleMover.cs:57
CollisionShape2D collisionShape2D
Collision shape
Definition: SimpleMover.cs:90
float MaxVelocity
Max velocity
Definition: SimpleMover.cs:39

Member Function Documentation

◆ Align()

virtual Vector2 Agents.SimpleVehicle.Align ( List< SimpleVehicle vehicles)
inlineprotectedvirtual

Align with other vehicles.

Parameters
vehiclesOther vehicles
Returns
Steer force

Definition at line 273 of file SimpleVehicle.cs.

274  {
275  var sum = Vector2.Zero;
276  int count = 0;
277  foreach (var vehicle in vehicles)
278  {
279  var d = GlobalPosition.DistanceSquaredTo(vehicle.GlobalPosition);
281  {
282  sum += vehicle.Velocity;
283  count++;
284  }
285  }
286 
287  if (count > 0)
288  {
289  sum = (sum / count).Normalized() * MaxVelocity;
290  return (sum - Velocity).Clamped(MaxForce);
291  }
292  else
293  {
294  return Vector2.Zero;
295  }
296  }
float DetectionAlignmentRadius
Detection alignment radius
Vector2 Velocity
Current velocity
Definition: SimpleMover.cs:27

◆ FollowFlow()

Vector2 Agents.SimpleVehicle.FollowFlow ( )
inlineprotected

Drive and steer following a flow field.

Returns
Steer force

Definition at line 187 of file SimpleVehicle.cs.

188  {
189  var tgtDirection = TargetFlow.Lookup(GlobalPosition);
190  if (tgtDirection == Vector2.Zero)
191  {
192  // Ignore empty lookup
193  return Vector2.Zero;
194  }
195 
196  var target = tgtDirection * MaxVelocity;
197  return (target - Velocity).Clamped(MaxForce);
198  }
Vector2 Lookup(Vector2 position)
Lookup direction from flow.
SimpleFlowField TargetFlow
Flow target

◆ FollowPath()

Vector2 Agents.SimpleVehicle.FollowPath ( )
inlineprotected

Drive and steer following a path. Always follow the path from A to B.

Returns
Steer force

Definition at line 205 of file SimpleVehicle.cs.

206  {
207  var minDist = float.MaxValue;
208 
209  var predict = Velocity.Normalized() * DetectionScanLength;
210  var predictPos = GlobalPosition + predict;
211 
212  Vector2? target = null;
213  Vector2? targetNormalPoint = null;
214  var pointCount = TargetPath.Points.Count;
215 
216  for (int i = 0; i < pointCount; ++i)
217  {
218  // Ignore last iteration if not looping
219  if (!TargetPath.Looping && i == pointCount - 1)
220  {
221  break;
222  }
223 
224  var a = TargetPath.Points[i];
225  var b = TargetPath.Points[(i + 1) % pointCount];
226  var normalPoint = predictPos.GetNormalPoint(a, b);
227  var dir = b - a;
228 
229  // Out of bounds
230  if (normalPoint.x < Mathf.Min(a.x, b.x) || normalPoint.x > Mathf.Max(a.x, b.x)
231  || normalPoint.y < Mathf.Min(a.y, b.y) || normalPoint.y > Mathf.Max(a.y, b.y))
232  {
233  normalPoint = b;
234 
235  if (TargetPath.Looping || i != pointCount - 2)
236  {
237  // Get two next points
238  var a2 = b;
239  var b2 = TargetPath.Points[(i + 2) % pointCount];
240  dir = b2 - a2;
241  }
242  }
243 
244  var dist = normalPoint.DistanceTo(predictPos);
245  if (dist < minDist)
246  {
247  minDist = dist;
248  targetNormalPoint = normalPoint;
249  target = targetNormalPoint + (dir.Normalized() * DetectionTargetOffset);
250  }
251  }
252 
253  // Set debug values
254  debugNormalPoint = targetNormalPoint;
255  debugPredictPos = predictPos;
256  debugTargetPoint = target;
257 
258  if (target.HasValue)
259  {
260  return Seek(target.Value);
261  }
262  else
263  {
264  return Vector2.Zero;
265  }
266  }
List< Vector2 > Points
Points
Definition: SimplePath.cs:12
bool Looping
Looping
Definition: SimplePath.cs:18
SimplePath TargetPath
Path target
float DetectionTargetOffset
Detection target offset
float DetectionScanLength
Detection scan length
virtual Vector2 Seek(Vector2 position)
Drive and steer towards target position.

◆ MoveLaterally()

virtual Vector2 Agents.SimpleVehicle.MoveLaterally ( List< SimpleVehicle vehicles)
inlineprotectedvirtual

Move laterally from other vehicles.

Parameters
vehiclesOther vehicles
Returns
Steer force

Definition at line 303 of file SimpleVehicle.cs.

304  {
305  var sum = Vector2.Zero;
306  int count = 0;
307 
308  // Use an arbitrary target position on the radius circle
309  var detectedPos = GlobalPosition + new Vector2(DetectionAlignmentRadius, 0).Rotated(GlobalRotation);
310 
311  foreach (var vehicle in vehicles)
312  {
313  var dp = detectedPos.DistanceSquaredTo(vehicle.GlobalPosition);
314 
315  // Compare the distance between the target position and the neighbor
316  if (dp > 0 && dp < (DetectionAlignmentRadius * DetectionAlignmentRadius) / 2)
317  {
318  var diff = vehicle.GlobalPosition - GlobalPosition;
319  sum += diff.Rotated(Mathf.Pi / 2);
320  count++;
321  }
322  }
323 
324  if (count > 0)
325  {
326  sum = (sum / count).Normalized() * MaxVelocity;
327  return (sum - Velocity).Clamped(MaxForce);
328  }
329  else
330  {
331  return Vector2.Zero;
332  }
333  }

◆ Regroup()

Vector2 Agents.SimpleVehicle.Regroup ( List< SimpleVehicle vehicles)
inlineprotected

Regroup with other vehicles.

Parameters
vehiclesOther vehicles
Returns
Steer force

Definition at line 133 of file SimpleVehicle.cs.

134  {
135  float separationLimit = Radius * 2;
136  var sum = Vector2.Zero;
137  int count = 0;
138 
139  foreach (var vehicle in vehicles)
140  {
141  float d = GlobalPosition.DistanceSquaredTo(vehicle.GlobalPosition);
142  if (d > 0 && d > separationLimit * separationLimit)
143  {
144  sum += (GlobalPosition - vehicle.GlobalPosition).Normalized() / Mathf.Sqrt(d);
145  count++;
146  }
147  }
148 
149  if (count > 0)
150  {
151  sum = (sum / count).Normalized() * -MaxVelocity;
152  return (sum - Velocity).Clamped(MaxForce);
153  }
154  else
155  {
156  return Vector2.Zero;
157  }
158  }
float Radius
Mesh radius
Definition: SimpleMover.cs:81

◆ Seek()

virtual Vector2 Agents.SimpleVehicle.Seek ( Vector2  position)
inlineprotectedvirtual

Drive and steer towards target position.

Parameters
positionTarget position
Returns
Steer force

Definition at line 165 of file SimpleVehicle.cs.

166  {
167  var targetDiff = position - GlobalPosition;
168  var targetDist = targetDiff.Length();
169  targetDiff = targetDiff.Normalized();
170 
171  if (ArriveDistance > 0 && targetDist < ArriveDistance)
172  {
173  targetDiff *= MathUtils.Map(targetDist, 0, ArriveDistance, 0, MaxVelocity);
174  }
175  else
176  {
177  targetDiff *= MaxVelocity;
178  }
179 
180  return (targetDiff - Velocity).Clamped(MaxForce);
181  }
float ArriveDistance
Arrive distance. Use -1 to disable.
Math utility functions
Definition: MathUtils.cs:7
static float Map(float value, float istart, float istop, float ostart, float ostop)
Map a value from one bound to another.
Definition: MathUtils.cs:17

◆ Separate()

Vector2 Agents.SimpleVehicle.Separate ( List< SimpleVehicle vehicles)
inlineprotected

Separate from other vehicles.

Parameters
vehiclesOther vehicles
Returns
Steer force

Definition at line 101 of file SimpleVehicle.cs.

102  {
103  float desiredSeparation = Radius * 4;
104  var sum = Vector2.Zero;
105  int count = 0;
106 
107  foreach (var vehicle in vehicles)
108  {
109  float d = GlobalPosition.DistanceSquaredTo(vehicle.GlobalPosition);
110  if (d > 0 && d < desiredSeparation * desiredSeparation)
111  {
112  sum += (GlobalPosition - vehicle.GlobalPosition).Normalized() / Mathf.Sqrt(d);
113  count++;
114  }
115  }
116 
117  if (count > 0)
118  {
119  sum = (sum / count).Normalized() * MaxVelocity;
120  return (sum - Velocity).Clamped(MaxForce);
121  }
122  else
123  {
124  return Vector2.Zero;
125  }
126  }

◆ UpdateAcceleration()

override void Agents.SimpleVehicle.UpdateAcceleration ( )
inlineprotectedvirtual

Update acceleration value.

Reimplemented from Forces.SimpleMover.

Definition at line 335 of file SimpleVehicle.cs.

336  {
337  var forces = Vector2.Zero;
338 
339  if (Target != null)
340  {
341  forces += Seek(Target.GlobalPosition) * SeekForceFactor;
342  }
343 
344  if (TargetFlow != null)
345  {
346  forces += FollowFlow() * SeekForceFactor;
347  }
348 
349  if (TargetPath != null)
350  {
351  forces += FollowPath() * SeekForceFactor;
352  }
353 
354  if (VehicleGroupList?.Count > 0)
355  {
356  if (SeparationEnabled)
357  {
359  }
360 
361  if (AlignmentEnabled)
362  {
364  }
365 
366  if (CohesionEnabled)
367  {
369  }
370 
371  if (LateralMoveEnabled)
372  {
374  }
375  }
376 
377  ApplyForce(forces);
378  }
float AlignmentForceFactor
Alignment force factor
Vector2 Separate(List< SimpleVehicle > vehicles)
Separate from other vehicles.
SimpleMover Target
Target
Vector2 Regroup(List< SimpleVehicle > vehicles)
Regroup with other vehicles.
float SeekForceFactor
Seek force factor
float CohesionForceFactor
Cohesion force factor
float LateralMoveForceFactor
Lateral move force factor
bool SeparationEnabled
Enable separation group behavior
virtual Vector2 MoveLaterally(List< SimpleVehicle > vehicles)
Move laterally from other vehicles.
bool CohesionEnabled
Enable cohesion group behavior
float SeparationForceFactor
Separation force factor
bool AlignmentEnabled
Enable alignment group behavior
bool LateralMoveEnabled
Enable lateral move group behavior
List< SimpleVehicle > VehicleGroupList
Vehicle group list
virtual Vector2 Align(List< SimpleVehicle > vehicles)
Align with other vehicles.
Vector2 FollowFlow()
Drive and steer following a flow field.
Vector2 FollowPath()
Drive and steer following a path. Always follow the path from A to B.
virtual void ApplyForce(Vector2 force)
Apply force on mover.
Definition: SimpleMover.cs:114

Member Data Documentation

◆ AlignmentEnabled

bool Agents.SimpleVehicle.AlignmentEnabled

Enable alignment group behavior

Definition at line 51 of file SimpleVehicle.cs.

◆ AlignmentForceFactor

float Agents.SimpleVehicle.AlignmentForceFactor = 1

Alignment force factor

Definition at line 69 of file SimpleVehicle.cs.

◆ ArriveDistance

float Agents.SimpleVehicle.ArriveDistance = -1

Arrive distance. Use -1 to disable.

Definition at line 30 of file SimpleVehicle.cs.

◆ CohesionEnabled

bool Agents.SimpleVehicle.CohesionEnabled

Enable cohesion group behavior

Definition at line 48 of file SimpleVehicle.cs.

◆ CohesionForceFactor

float Agents.SimpleVehicle.CohesionForceFactor = 1

Cohesion force factor

Definition at line 66 of file SimpleVehicle.cs.

◆ DebugDraw

bool Agents.SimpleVehicle.DebugDraw

Debug draw

Definition at line 42 of file SimpleVehicle.cs.

◆ DetectionAlignmentRadius

float Agents.SimpleVehicle.DetectionAlignmentRadius = 50

Detection alignment radius

Definition at line 39 of file SimpleVehicle.cs.

◆ DetectionScanLength

float Agents.SimpleVehicle.DetectionScanLength = 25

Detection scan length

Definition at line 33 of file SimpleVehicle.cs.

◆ DetectionTargetOffset

float Agents.SimpleVehicle.DetectionTargetOffset = 25

Detection target offset

Definition at line 36 of file SimpleVehicle.cs.

◆ LateralMoveEnabled

bool Agents.SimpleVehicle.LateralMoveEnabled

Enable lateral move group behavior

Definition at line 54 of file SimpleVehicle.cs.

◆ LateralMoveForceFactor

float Agents.SimpleVehicle.LateralMoveForceFactor = 1

Lateral move force factor

Definition at line 72 of file SimpleVehicle.cs.

◆ MaxForce

float Agents.SimpleVehicle.MaxForce = 0.1f

Max force

Definition at line 27 of file SimpleVehicle.cs.

◆ SeekForceFactor

float Agents.SimpleVehicle.SeekForceFactor = 1

Seek force factor

Definition at line 60 of file SimpleVehicle.cs.

◆ SeparationEnabled

bool Agents.SimpleVehicle.SeparationEnabled

Enable separation group behavior

Definition at line 45 of file SimpleVehicle.cs.

◆ SeparationForceFactor

float Agents.SimpleVehicle.SeparationForceFactor = 1

Separation force factor

Definition at line 63 of file SimpleVehicle.cs.

◆ Target

SimpleMover Agents.SimpleVehicle.Target

Target

Definition at line 18 of file SimpleVehicle.cs.

◆ TargetFlow

SimpleFlowField Agents.SimpleVehicle.TargetFlow

Flow target

Definition at line 21 of file SimpleVehicle.cs.

◆ TargetPath

SimplePath Agents.SimpleVehicle.TargetPath

Path target

Definition at line 24 of file SimpleVehicle.cs.

◆ VehicleGroupList

List<SimpleVehicle> Agents.SimpleVehicle.VehicleGroupList

Vehicle group list

Definition at line 57 of file SimpleVehicle.cs.


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