Güzel cevaplarınız için teşekkür ederim.
Yol bulma algoritmalarının çoğu @
engerex fikir yürüttüğünüz şekliyle çalışıyor. Benzer bir yapıyı aşağıya bırakıyorum. Biraz da olsa bir başarı sağladım.
@
mrmarman daha ilk adımı başarılı atamadığım için "ilk adımım doğru mu" sorusunu soramadım. Sorduğunuz sorulara sıra gelemedi

İhtiyaca göre şekillenecek bir başlangıç lazım dediğiniz gibi. Amacım sadece objeleri bir birine çarpmadan oradan oraya sürüklemek şimdilik.

ChatGPT ile zar zor oluşturuldu
unit PathfindingUnit;
interface
uses
System.SysUtils, System.Types, System.Generics.Collections, System.Classes, FMX.Objects, FMX.Graphics,
System.UITypes;
type
TNode = record
X, Y: Integer;
GCost, HCost, FCost: Integer;
ParentX, ParentY: Integer;
constructor Create(aX, aY: Integer);
end;
TPathfinding = class
private
FCharacter: TRectangle;
FTarget: TPoint;
FObstacles: TArray<TRectangle>;
FPathList: TStringList;
FGridSize: Integer;
procedure OptimizePath;
function GetValidNeighbors(CurrentNode: TNode): TArray<TNode>;
function IsCollision: Boolean;
public
constructor Create(Character: TRectangle; Target: TPoint; Obstacles: TArray<TRectangle>; GridSize: Integer = 20);
destructor Destroy; override;
procedure FindPath;
property PathList: TStringList read FPathList; function IsWalkable(X, Y: Integer): Boolean;
end;
implementation
{ TNode }
constructor TNode.Create(aX, aY: Integer);
begin
X := aX;
Y := aY;
GCost := 0;
HCost := 0;
FCost := 0;
ParentX := -1;
ParentY := -1;
end;
{ TPathfinding }
constructor TPathfinding.Create(Character: TRectangle; Target: TPoint; Obstacles: TArray<TRectangle>; GridSize: Integer);
begin
FCharacter := Character;
FTarget := Target;
FObstacles := Copy(Obstacles, 0, Length(Obstacles));
FPathList := TStringList.Create;
FGridSize := GridSize;
end;
destructor TPathfinding.Destroy;
begin
FPathList.Free;
inherited;
end;
function TPathfinding.GetValidNeighbors(CurrentNode: TNode): TArray<TNode>;
var
Neighbors: TList<TNode>;
dx, dy: Integer;
Neighbor: TNode;
begin
Neighbors := TList<TNode>.Create;
try
for dx := -1 to 1 do
for dy := -1 to 1 do
begin
if (dx = 0) and (dy = 0) then
Continue;
// Diyagonal hareketi kontrol et, engellerin kenarlarından geçmemesini sağlar
if (Abs(dx) + Abs(dy) = 2) and (not IsWalkable(CurrentNode.X + dx, CurrentNode.Y) or not IsWalkable(CurrentNode.X, CurrentNode.Y + dy)) then
Continue;
Neighbor := TNode.Create(CurrentNode.X + dx, CurrentNode.Y + dy);
if IsWalkable(Neighbor.X * FGridSize, Neighbor.Y * FGridSize) then
Neighbors.Add(Neighbor);
end;
Result := Neighbors.ToArray;
finally
Neighbors.Free;
end;
end;
function GetMinFCostNode(OpenList: TDictionary<Integer, TNode>): TNode;
var
Node: TNode;
MinNode: TNode;
MinCost: Integer;
begin
MinCost := MaxInt;
for Node in OpenList.Values do
begin
if Node.FCost < MinCost then
begin
MinCost := Node.FCost;
MinNode := Node;
end;
end;
Result := MinNode;
end;
function CheckBoundryCollision( R1, R2 : TRect; pR : PRect = nil; OffSetY : LongInt = 0; OffSetX : LongInt = 0): boolean;
begin
if( pR <> nil ) then
begin
with( R1 ) do
R1:=Rect( Left+OffSetX, Top+OffSetY, Right-(OffSetX * 2), Bottom-(OffSetY * 2));
with( R2 ) do
R2:=Rect( Left+OffSetX, Top+OffSetY, Right-(OffSetX * 2), Bottom-(OffSetY * 2));
Result:=IntersectRect( pR^, R1, R2 );
end
else begin
Result:=( NOT ((R1.Bottom - (OffSetY * 2) < R2.Top + OffSetY)
or(R1.Top + OffSetY > R2.Bottom - (OffSetY * 2))
or( R1.Right - (OffSetX * 2) < R2.Left + OffSetX)
or( R1.Left + OffSetX > R2.Right - (OffSetX * 2))));
end;
end;
procedure TPathfinding.FindPath;
var
StartNode, EndNode: TNode;
OpenList, ClosedList: TDictionary<Integer, TNode>;
CurrentNode, Neighbor: TNode;
GCost, HCost, FCost: Integer;
PathFound: Boolean;
function CalculateHCost(Node1, Node2: TNode): Integer;
begin
Result := Abs(Node1.X - Node2.X) + Abs(Node1.Y - Node2.Y); // Manhattan mesafesi
end;
begin
if IsCollision = True then
begin
FCharacter.Fill.Color := TAlphaColorRec.Red;
Exit;
end;
FCharacter.Fill.Color := TAlphaColorRec.Lightgreen;
StartNode := TNode.Create(Round(FCharacter.Position.X / FGridSize), Round(FCharacter.Position.Y / FGridSize));
EndNode := TNode.Create(Round(FTarget.X / FGridSize), Round(FTarget.Y / FGridSize));
OpenList := TDictionary<Integer, TNode>.Create;
ClosedList := TDictionary<Integer, TNode>.Create;
PathFound := False;
try
OpenList.Add(StartNode.X * 10000 + StartNode.Y, StartNode);
while OpenList.Count > 0 do
begin
CurrentNode := GetMinFCostNode(OpenList);
OpenList.Remove(CurrentNode.X * 10000 + CurrentNode.Y);
ClosedList.Add(CurrentNode.X * 10000 + CurrentNode.Y, CurrentNode);
// Hedefe ulaşıldıysa yolu oluştur
if (CurrentNode.X = EndNode.X) and (CurrentNode.Y = EndNode.Y) then
begin
PathFound := True;
while (CurrentNode.ParentX <> -1) and (CurrentNode.ParentY <> -1) do
begin
FPathList.Add(Format('%d,%d', [CurrentNode.X * FGridSize, CurrentNode.Y * FGridSize]));
CurrentNode := ClosedList[CurrentNode.ParentX * 10000 + CurrentNode.ParentY];
end;
FPathList.Add(Format('%d,%d', [StartNode.X * FGridSize, StartNode.Y * FGridSize]));
OptimizePath;
Break;
end;
// Geçerli düğümün komşularını incele
for Neighbor in GetValidNeighbors(CurrentNode) do
begin
// Çarpışmayı kontrol et
if ClosedList.ContainsKey(Neighbor.X * 10000 + Neighbor.Y) or not IsWalkable(Neighbor.X * FGridSize, Neighbor.Y * FGridSize) then
Continue;
GCost := CurrentNode.GCost + 1;
HCost := CalculateHCost(Neighbor, EndNode);
FCost := GCost + HCost;
var TempNeighbor := Neighbor;
if not OpenList.ContainsKey(TempNeighbor.X * 10000 + TempNeighbor.Y) then
begin
TempNeighbor.GCost := GCost;
TempNeighbor.HCost := HCost;
TempNeighbor.FCost := FCost;
TempNeighbor.ParentX := CurrentNode.X;
TempNeighbor.ParentY := CurrentNode.Y;
OpenList.Add(TempNeighbor.X * 10000 + TempNeighbor.Y, TempNeighbor);
end;
end;
end;
// Eğer yol bulunamadıysa uyarı göster
if not PathFound then
FCharacter.Fill.Color := TAlphaColorRec.Red;
finally
OpenList.Free;
ClosedList.Free;
end;
end;
function TPathfinding.IsCollision: Boolean;
var
tmpRect: TRectangle;
begin
Result := False;
tmpRect := TRectangle.Create(nil);
tmpRect.Height := FCharacter.Height;
tmpRect.Width := FCharacter.Width;
tmpRect.Position.X := FTarget.X;
tmpRect.Position.Y := FTarget.Y;
for var Obstacle in FObstacles do
begin
if CheckBoundryCollision(tmpRect.BoundsRect.Round,Obstacle.BoundsRect.Round) = True then
begin
Result := True;
Break;
end;
end;
FreeAndNil(tmpRect);
end;
function TPathfinding.IsWalkable(X, Y: Integer): Boolean;
var
CharRect, ObstacleRect: TRectF;
begin
CharRect := TRectF.Create(X, Y, X + FCharacter.Width, Y + FCharacter.Height);
Result := True;
for var Obstacle in FObstacles do
begin
ObstacleRect := TRectF.Create(Obstacle.Position.X, Obstacle.Position.Y, Obstacle.Position.X + Obstacle.Width, Obstacle.Position.Y + Obstacle.Height);
if CharRect.IntersectsWith(ObstacleRect) then
begin
Result := False;
Break;
end;
end;
end;
procedure TPathfinding.OptimizePath;
var
i: Integer;
CurrDir, NextDir: TPoint;
begin
i := 0;
while i < FPathList.Count - 2 do
begin
var Point1 := TPoint.Create(StrToInt(FPathList[i].Split([','])[0]), StrToInt(FPathList[i].Split([','])[1]));
var Point2 := TPoint.Create(StrToInt(FPathList[i + 1].Split([','])[0]), StrToInt(FPathList[i + 1].Split([','])[1]));
var Point3 := TPoint.Create(StrToInt(FPathList[i + 2].Split([','])[0]), StrToInt(FPathList[i + 2].Split([','])[1]));
CurrDir := Point2 - Point1;
NextDir := Point3 - Point2;
if (CurrDir.X = NextDir.X) and (CurrDir.Y = NextDir.Y) then
FPathList.Delete(i + 1)
else
Inc(i);
end;
end;
end.
Kendimce kullanımı;
procedure TForm1.Layout1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
var
Pathfinding: TPathfinding;
Obstacles: TArray<TRectangle>;
i: integer;
pdata: TPathData;
begin
// Engelleri diziye ekleyin
pa.Stop;
SetLength(Obstacles, 3);
Obstacles[0] := Rectangle4;
Obstacles[1] := Rectangle2;
Obstacles[2] := Rectangle3;
memo1.Lines.Clear;
Layout1.Repaint;
Application.ProcessMessages;
Pathfinding := TPathfinding.Create(Character, Point(Round(X), Round(Y)), Obstacles);
try
Pathfinding.FindPath;
//for var i := 0 to Pathfinding.PathList.Count - 1 do
for i := Pathfinding.PathList.Count - 1 downto 0 do
Memo1.Lines.Add(Pathfinding.PathList[i]); // Koordinatları gösterme
finally
Pathfinding.Free;
end;
pdata := TPathData.Create;
for i := 0 to Memo1.Lines.Count-1 do
begin
if Memo1.Lines.Strings[i] <> '' then
begin
if i = 0 then pdata.MoveTo(PointF(Round(memo1.Lines.Strings[i].Split([','])[0].ToSingle),Round(memo1.Lines.Strings[i].Split([','])[1].ToSingle)));
pdata.LineTo(PointF(Round(memo1.Lines.Strings[i].Split([','])[0].ToSingle),Round(memo1.Lines.Strings[i].Split([','])[1].ToSingle)));
end;
end;
pdata.ClosePath;
Layout1.Canvas.BeginScene;
try
Layout1.Canvas.Stroke.Color := TAlphaColorRec.Red;
Layout1.Canvas.Stroke.Thickness := 1;
Layout1.Canvas.Stroke.Dash := TStrokeDash.Solid;
Layout1.Canvas.DrawPath(pdata,100);
finally
Layout1.Canvas.EndScene;
end;
pdata.Free;
if Memo1.Lines.Count <= 0 then
exit;
ipath := 0;
pa.StartValue.X := Character.Position.X;
pa.StartValue.Y := Character.Position.Y;
pa.StopValue.X := Character.Position.X;
pa.StopValue.Y := Character.Position.Y;
pa.Start;
end;
pa komponenti FGX eklenti seti içerisindeki PositionAnimation.
GitHub - theilgaz/FGX: This is a repacked FGX Components for Delphi 10.4 Sydney. This pack is compatible with Delphi 10.3 Rio and earlier versions.