Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
1.0.0:pathfinding [2023/03/21 15:33] – [Auto-Rotate an animated character] admin | 1.0.0:pathfinding [2023/03/21 15:34] (current) – [Click to Start Pathfinding] admin | ||
---|---|---|---|
Line 218: | Line 218: | ||
// For correct rotation of our character, we need the polygon to detect the | // For correct rotation of our character, we need the polygon to detect the | ||
- | // next node to adjust to | + | // next node to adjust to - |
+ | // NOTICE: READ THE SECTION BELOW | ||
FPath.Path.FlattenToPolygon(FPolygon); | FPath.Path.FlattenToPolygon(FPolygon); | ||
Line 226: | Line 227: | ||
end; | end; | ||
</ | </ | ||
+ | |||
+ | |||
+ | ===== Auto-Rotate an animated character ===== | ||
+ | |||
+ | <file pascal> | ||
+ | procedure TForm1.FormCreate(Sender: | ||
+ | begin | ||
+ | [...] | ||
+ | |||
+ | // Register callback events | ||
+ | FPathAnim.OnProcess | ||
+ | FPathAnim.OnFinish | ||
+ | | ||
+ | // For correct rotation of our character, we need the polygon to detect the | ||
+ | // next node to adjust to | ||
+ | // NOTICE: If the path changes, this has to be called again! | ||
+ | FPath.Path.FlattenToPolygon(FPolygon); | ||
+ | end; | ||
+ | |||
+ | procedure TForm1.DoOnPathAnimProcess(ASender : TObject); | ||
+ | |||
+ | function GetKeyValues(APolygon : TGorillaPolygon3D; | ||
+ | out P1, P2 : TPoint3D) : Single; | ||
+ | var LDeltaPos : Single; | ||
+ | LSglEnt : Integer; | ||
+ | begin | ||
+ | if System.Length(APolygon) <= 0 then | ||
+ | begin | ||
+ | P1 := TPoint3D.Zero; | ||
+ | P2 := TPoint3D.Zero; | ||
+ | Exit; | ||
+ | end; | ||
+ | |||
+ | // get current key position | ||
+ | LDeltaPos := T * High(APolygon); | ||
+ | // key value | ||
+ | LSglEnt := Floor(LDeltaPos); | ||
+ | // key offset | ||
+ | Result := LDeltaPos - LSglEnt; | ||
+ | |||
+ | // key coordinate | ||
+ | if (LSglEnt <= High(APolygon)) then | ||
+ | P1 := APolygon[LSglEnt] | ||
+ | else | ||
+ | P1 := APolygon[High(APolygon)]; | ||
+ | |||
+ | inc(LSglEnt); | ||
+ | if (LSglEnt <= High(APolygon)) then | ||
+ | P2 := APolygon[LSglEnt] | ||
+ | else | ||
+ | P2 := APolygon[High(APolygon)]; | ||
+ | end; | ||
+ | |||
+ | var dx, dz : Single; | ||
+ | LAngleY : Single; | ||
+ | P1, P2 : TPoint3D; | ||
+ | begin | ||
+ | // Get the currently relevant point to compute rotation | ||
+ | GetKeyValues(FPolygon, | ||
+ | |||
+ | dx := P2.X - P1.X; | ||
+ | dz := P1.Z - P2.Z; | ||
+ | LAngleY := arctan2(dx, dz); | ||
+ | |||
+ | GorillaModel1.RotationAngle.Y := RadToDeg(LAngleY); | ||
+ | GorillaModel1.AnimationManager.PlayAnimation(' | ||
+ | end; | ||
+ | |||
+ | procedure TForm1.DoOnPathAnimFinished(ASender : TObject); | ||
+ | begin | ||
+ | GorillaModel1.AnimationManager.PlayAnimation(' | ||
+ | end; | ||
+ | </ | ||
+ | |||
Next: [[renderpass|Render Pass]] | Next: [[renderpass|Render Pass]] |