Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
pathfinding [2020/11/06 09:53] – [Dimensions] adminpathfinding [2020/11/06 10:20] – [Configuration] admin
Line 9: Line 9:
 You can update obstacles at runtime and recompute the map to simulate a dynamic environment. You can update obstacles at runtime and recompute the map to simulate a dynamic environment.
  
-Each Pathfinder is working on a single so called agent, which is the object to be moved on computed path.+Each pathfinder is working on a single so called agent, which is the object to be moved on computed path.
  
 ===== Setup ===== ===== Setup =====
Line 22: Line 22:
 ===== Obstacles ===== ===== Obstacles =====
  
-Obstacles are restricted areas on the pathfinding map, where an agent can't walk onto, so the algorithm will find a way around this obstacle. You can add static or dynamic obstacles to the system.+Obstacles are restricted areas on the pathfinding map, where an agent can't walk onto, so the algorithm will find a way around this obstacle. You can add static or dynamic obstacles to the system. Obstacles can be all TControl3D instances. The pathfinder will use the bounding box to compute restricted areas. So there may be differences to the visual feedback of the object.
  
 <file pascal> <file pascal>
Line 38: Line 38:
 </file> </file>
  
-===== Dimensions =====+===== Configuration ===== 
 + 
 +^Property ^Description ^ 
 +|GridDimensions|The number of columns and rows used by the A* algorithm. The more units the grid uses, the longer the algorithm takes to compute a path. The less columns and rows used, the faster the algorithm computes a path. The default value is [8, 8].| 
 +|Size3D|The area of 3d space (X and Z) needs to be fixed to convert from grid coordinates to 3d coordinates and otherwise. The default value is [10.0, 10.0].| 
 +|Diagonals|Defines if diagonal paths are possible. Otherwise only horizontal and vertical paths are possible.| 
 +|Fallback|Defines if a fallback scenario should be computed in case not path|
  
 <file pascal> <file pascal>
 +const
 +  PATHFINDING_GRID_X   = 128;
 +  PATHFINDING_GRID_Z   = 128;
 +  PATHFINDING_3DSIZE_X = 20;
 +  PATHFINDING_3DSIZE_Z = 20;
 +  
 var LGridSize : TPoint; var LGridSize : TPoint;
     LSize3D : TPointF;     LSize3D : TPointF;
Line 55: Line 67:
  
 ===== Computation ===== ===== Computation =====
 +
 +The TGorillaPathfindingAStar component is just a logical instance. No movement will be performed by default.
  
 <file pascal> <file pascal>
 +FPath : TGorillaPath3D;
 +
 +[...]
 +
 +// compute a path around all obstacles in given area
 FPathFinder.FindPath(Point3D(-10, 0, 10)); FPathFinder.FindPath(Point3D(-10, 0, 10));
 +
 +FPath := FPathFinder.ToNewPath3D(FGorilla);
 +FPath.Parent := FGorilla;
 </file> </file>
  
 +To move the agent we need a TGorillaPath3DAnimation component and connect both instances.
 +
 +<file pascal>
 +FPathAnim := TGorillaPath3DAnimation.Create(FAgent);
 +
 +// the path animation need to manipulate position of the agent
 +FPathAnim.Parent := FAgent;
 +
 +// it should take 5 seconds and move straight on the computed path
 +FPathAnim.Duration := 5;
 +FPathAnim.SplineType := TGorillaSpline3DType.Linear;
 +
 +// here we need to apply the computed path data
 +FPathAnim.Path := FPath.Path;
 +
 +// make sure the agent is at same position as the pathfinder computed from
 +FAgent.Position.Point := FPathFinder.StartPosition;
 +
 +// start movemten on path
 +FPathAnim.Enabled := true;
 +FPathAnim.Start();
 +</file>
 +
 +===== Update =====
 +
 +In case you want to update the map and path at runtime, you can use the following code snippet.
 +
 +<file pascal>
 +// new computation of path
 +FPathFinder.FindPath(Point3D(5, 0, 15));
 +
 +// refresh map in our image
 +FPathFinder.Draw(FVerifyBmp);
 +Image1.Bitmap.Assign(FVerifyBmp);
 +
 +// stop the current animation
 +FPathAnim.Stop();
 +FPathAnim.Enabled := false;
 +
 +// apply computed path data to our existing TGorillaPath3D instance, instead for recreating each time
 +FPathFinder.ApplyToPath3D(FPath);
 +
 +// reset the used path data in our path animation
 +FPathAnim.Path := FPath.Path;
 +</file>
 +
 +
 +===== Visualize =====
 +
 +<file pascal>
 +LGridSize : TPoint;
 +FVerifyBmp : TBitmap;
 +
 +[...]
 +
 +// create a bitmap - to show map and path in a tiny image
 +// we use the adjusted grid size which includes agent width
 +LGridSize := FPathFinder.AdjustedGridSize;
 +FVerifyBmp := TBitmap.Create(LGridSize.X, LGridSize.Y);
 +
 +// an image component, we've placed inside of our TGorillaViewport
 +Image1.Width  := LGridSize.X * 4;
 +Image1.Position.X := Form1.Width - Image1.Width - 32;
 +Image1.Height := LGridSize.Y * 4;
 +
 +// draw pathfinder map
 +FPathFinder.Draw(FVerifyBmp);
 +// apply computed image to our visual feedback image in the viewport
 +Image1.Bitmap.Assign(FVerifyBmp);
 +</file>