Autor Beitrag
monty.ms
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 47



BeitragVerfasst: Do 04.10.07 20:36 
Hallo

Ich versuche gerade eine Art Verfolgung zu schreiben. Also ein Objekt (Shape oder Image z.B) verfolgt ein Anderes. Ich bin zwar schon recht weit, allerdings hab ich das Gefühl ich bin da etwas zu kompliziert ran gegangen und es gibt auch noch ein paar Fehler. (z.B. bleibt er ohne Grund stehen)

Die Verfolgung soll nicht nur über eine Gerade Strecke geschehen. Hier mal mein bescheidener Versuch :)

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
{ In einem Timer ruf ich diese Prozedur auf. Point1 enthält X und Y Werte des "Jägers" und Point2 die Koordinaten des "Gejagten". k entspricht einfach nur die Schrittweite, also wie schnell sich der Jäger bewegen soll. }
procedure TForm1.SetNewPosition(Point1:TPoint; Point2:TPoint; k:Integer);
VAR x,y,y2,y2tmp,x2: Integer;
    a,l: Real;
begin
// Länge der Gerade x (Gegenkathete)
x := Point1.X - Point2.X;
If x < 0 Then x := -x;

// Länge der Gerade y (Ankathete)
y := Point1.Y - Point2.Y;
If y < 0 Then y := -y;

// Länge der Gerade l (Hypothenuse)
l := sqrt( sqr(x) + sqr(y) );
l := l-k;

// Winkel
a := ( arctan(x/y) );
a := RadToDeg(a);

{ Wenn die X Werte oder Y Werte ungleich sind, wird neue Position berechnet. Ansonsten: Fertig! }
If (Point1.X <> Point2.X) or (Point1.Y <> Point2.Y) Then
 begin
  y2 := round(cos(DegToRad(a)) * l);
  y2tmp := y2;

  // y2 ist hier der neue Punkt und nicht die Länge der Gerade
  If (Point1.Y < Point2.Y) Then
   y2 := round(Point1.Y + (y-y2));
  If (Point1.Y > Point2.Y) Then
   y2 := round(Point1.Y - (y-y2)); 

  // x2 ist hier der neue Punkt und nicht die Länge der Gerade
  x2 := round(tan(DegToRad(a)) * y2tmp);
  If (Point1.X > Point2.X) Then
   x2 := round(Point1.X - (x-x2));
  If (Point1.X < Point2.X) Then
   x2 := round(Point1.X + (x-x2));

  Shape1.Left := x2;
  Shape1.Top := y2;
 end;


Wäre nett wenn mir einer helfen könnte :)

Danke schonmal.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Fr 05.10.07 14:52 
Fass das ganze doch so zusammen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
{ In einem Timer ruf ich diese Prozedur auf. Point1 enthält X und Y Werte des "Jägers" und Point2 die Koordinaten des "Gejagten". k entspricht einfach nur die Schrittweite, also wie schnell sich der Jäger bewegen soll. }
procedure TForm1.SetNewPosition(Point1:TPoint; Point2:TPoint; k:Integer);
VAR
  x,y,x2,y2: Integer;
  l: Real;
begin
  x := Point1.X - Point2.X;
  y := Point1.Y - Point2.Y;

  l = sqrt(sqr(x) + sqr(y));

  if l<k then
  Begin
    x2 = x;
    y2 = y;
  end
  else
  begin
    x2 = round(x * k/l);
    y2 = round(y * k/l);
  end;

  point1.x = Point1.x + x;
  point1.y = Point1.y + y;

  Shape1.Left := point1.x;
  Shape1.Top := point1.y;
end;


Sollte so funzen, ist aber ungetestet.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
monty.ms Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 47



BeitragVerfasst: Sa 06.10.07 02:59 
Danke erstmal für die Hilfe :)

Ich hatte es mit meiner Variante doch noch hinbekommen, aber deine ist weit aus... einfacher :). Habe nur noch zwei Fehler korrigiert:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
{ In einem Timer ruf ich diese Prozedur auf. Point1 enthält X und Y Werte des "Jägers" und Point2 die Koordinaten des "Gejagten". k entspricht einfach nur die Schrittweite, also wie schnell sich der Jäger bewegen soll. }  
procedure TForm1.SetNewPosition(Point1:TPoint; Point2:TPoint; k:Integer);  
VAR  
  x,y,x2,y2: Integer;  
  l: Real;  
begin
// P2 - P1 statt P1 - P2 oder alternativ unten x2 und y2 nicht dazu addieren, sondern subtrahieren.
  x := Point2.X - Point1.X;
  y := Point2.Y - Point1.Y; 

 
  l := sqrt(sqr(x) + sqr(y));  

 
  if l<k then  
  Begin  
    x2 := x;  
    y2 := y;  
  end  
  else  
  begin  
    x2 := round(x * k/l);  
    y2 := round(y * k/l);  
  end;  

 
  point1.x := Point1.x + x2;  // x2 statt x
  point1.y := Point1.y + y2;  

 
  Shape1.Left := point1.x;  
  Shape1.Top := point1.y;  
end;


Also Danke nochmal.