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
1.0.0:pathfinding [2023/03/21 15:33] – [Auto-Rotate an animated character] admin1.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;
 </file> </file>
 +
 +
 +===== Auto-Rotate an animated character =====
 +
 +<file pascal>
 +procedure TForm1.FormCreate(Sender: TObject);
 +begin
 +[...]
 +
 +  // Register callback events
 +  FPathAnim.OnProcess  := DoOnPathAnimProcess;
 +  FPathAnim.OnFinish   := DoOnPathAnimFinished;
 +  
 +  // 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; const T : Single;
 +  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, FPathAnim.NormalizedTime, P1, P2);
 +
 +  dx := P2.X - P1.X;
 +  dz := P1.Z - P2.Z;
 +  LAngleY := arctan2(dx, dz);
 +
 +  GorillaModel1.RotationAngle.Y := RadToDeg(LAngleY);
 +  GorillaModel1.AnimationManager.PlayAnimation('run-forward.dae');
 +end;
 +
 +procedure TForm1.DoOnPathAnimFinished(ASender : TObject);
 +begin
 +  GorillaModel1.AnimationManager.PlayAnimation('idle.dae');
 +end;
 +</file>
 +
  
 Next: [[renderpass|Render Pass]] Next: [[renderpass|Render Pass]]