Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
pathfinding [2020/11/06 10:01] – [Pathfinding] admin | pathfinding [Unknown date] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Pathfinding ====== | ||
- | {{:: | ||
- | |||
- | Pathfinding is of course a very popular mechanism used in many games. Developers very often use it in controlling enemies or opponents. | ||
- | Gorilla3D provides an easy to use component based on AStar algorithm. | ||
- | The TGorillaPathfindingAStar component helps you manage those automated movements. It abstracts your 3D world and obstacles into a 2D-Plane to compute a path from a starting point to a destination. | ||
- | |||
- | 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. | ||
- | |||
- | ===== Setup ===== | ||
- | |||
- | <file pascal> | ||
- | FPathFinder := TGorillaPathfindingAStar.Create(nil); | ||
- | |||
- | // apply the agent used as source (any TControl3D) | ||
- | FPathFinder.Agent := FAgent; | ||
- | </ | ||
- | |||
- | ===== 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. | ||
- | |||
- | <file pascal> | ||
- | // add a dynamic obstacle - needs update | ||
- | FPathFinder.AddObstacle(FObstacle1, | ||
- | |||
- | // add a static obstacle | ||
- | FPathFinder.AddObstacle(FObstacle2); | ||
- | </ | ||
- | |||
- | When adding an obstacle to the pathfinder you can define a margin. Those margins are useful if space between to obstacles is very tight. To prevent the agent to run into a narrow area, where it looks like he won't fit. | ||
- | |||
- | <file pascal> | ||
- | FPathFinder.ObstacleMargin := TPoint3D.Create(1, | ||
- | </ | ||
- | |||
- | ===== Dimensions ===== | ||
- | |||
- | <file pascal> | ||
- | var LGridSize : TPoint; | ||
- | LSize3D : TPointF; | ||
- | |||
- | [...] | ||
- | |||
- | // set dimensions of grid and used 3D space | ||
- | LGridSize := TPoint.Create(PATHFINDING_GRID_X, | ||
- | FPathFinder.GridDimensions := LGridSize; | ||
- | | ||
- | LSize3D := TPointF.Create(PATHFINDING_3DSIZE_X, | ||
- | FPathFinder.Size3D := LSize3D; | ||
- | </ | ||
- | |||
- | ===== Computation ===== | ||
- | |||
- | The TGorillaPathfindingAStar component is just a logical instance. No movement will be performed by default. | ||
- | |||
- | <file pascal> | ||
- | FPath : TGorillaPath3D; | ||
- | |||
- | [...] | ||
- | |||
- | // compute a path around all obstacles in given area | ||
- | FPathFinder.FindPath(Point3D(-10, | ||
- | |||
- | FPath := FPathFinder.ToNewPath3D(FGorilla); | ||
- | FPath.Parent := FGorilla; | ||
- | </ | ||
- | |||
- | 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(); | ||
- | </ |