Einzelnen Beitrag anzeigen

Sinderion

Registriert seit: 23. Nov 2007
Ort: Oberösterreich
19 Beiträge
 
Delphi XE7 Architect
 
#34

AW: Andorra 2d Billardspiel in 2d

  Alt 7. Jul 2011, 16:27
Endlich gefunden, wusste doch dass ich so was auch schon mal programmiert habe:

Ist allerdings etwas umständlich gemacht, da es ursprünglich für Polygone gedacht war und anschließend auf einzelne Linien reduziert wurde (das ganze orientiert sich entfernt am Separating Axis Theorem)
Code ist zwar nicht wunderbar, sollte aber funktionieren.

Funktionen:
E liefert den Einheitsvektor
Invert den Normalvektor
Norm den Betrag(=Länge)

Delphi-Quellcode:
function TBall.LineIntersect(const A:TLine):boolean;
var axis,aa,b,vquer:TVector;
p_poly:array[0..1]of extended;
p_circle,M11,M22,M12,M21,pmin,pmax,winkel:extended;
begin
  result := false;
  //Projektion berechnen
  axis := E(Invert(A.Coords[1] - A.Coords[0]));
  p_poly[0] := axis * A.Coords[0];
  p_circle := axis * M;
  //keine Überschneidung wenn ...
  if abs(p_circle - p_poly[0]) > r then exit;

  //Projektion berechnen, zweite Achse
  axis := E(A.Coords[1] - A.Coords[0]);
  p_poly[0] := axis * A.Coords[0];
  p_poly[1] := axis * A.Coords[1];
  p_circle := axis * M;
  pmin := min(p_poly[0], p_poly[1]) - r;
  pmax := max(p_poly[0], p_poly[1]) + r;

  //keine Überschneidung wenn ...
  if not((pmin <= p_circle)and(p_circle <= pmax)) then begin
    result := false;
    exit;
  end;

  result := true;
  //ansonsten Kollision ...
  //Drehmatrix berechnen
  //v anpassen
  //zurückdrehen
  b := A.Coords[1] - A.Coords[0];

  aa.x := 1;
  aa.y := 0;

  winkel := aa * b / Norm(aa) / Norm(b);

  M11 := winkel; M21 := sqrt(1 - sqr(winkel));
  M12 := -M21; M22 := M11;

  vquer.x := v.x * M11 + v.y * M21;
  vquer.y := v.x * M12 + v.y * M22;

  vquer.y := -1 * vquer.y;

//-Matrix mehr oder weniger ...., alpha = -alpha;
  M21 := -M21;
  M12 := -M12;

  v.x := vquer.x * M11 + vquer.y * M21;
  v.y := vquer.x * M12 + vquer.y * M22;
end;
Daniel

Geändert von Sinderion ( 7. Jul 2011 um 16:29 Uhr)
  Mit Zitat antworten Zitat