2D-Kollisionserkennung

Aus Spieleprogrammierer-Wiki
(Unterschied zwischen Versionen)
Wechseln zu: Navigation, Suche
[gesichtete Version][unmarkierte Version]
K (Kollision zwischen zwei Strecken)
(Kollision zwischen zwei Strecken)
Zeile 242: Zeile 242:
 
bool lineSegmentCollision(const Vector& a1, const Vector& a2,
 
bool lineSegmentCollision(const Vector& a1, const Vector& a2,
 
  const Vector& b1, const Vector& b2)
 
  const Vector& b1, const Vector& b2)
 +
{
 +
const float denom = (b2.y - b1.y) * (a2.x - a1.x) -
 +
(b2.x - b1.x) * (a2.y - a1.y);
 +
if (abs(denom) < EPSILON) return false;
 +
 +
const float ua = ((b2.x - b1.x) * (a1.y - b1.y) -
 +
  (b2.y - b1.y) * (a1.x - b1.x)) / denom;
 +
const float ub = ((a2.x - a1.x) * (a1.y - b1.y) -
 +
  (a2.y - a1.y) * (a1.x - b1.x)) / denom;
 +
return ua >= 0 && ua <= 1 && ub >= 0 && ua <= 1;
 +
}
 +
</sourcecode>
 +
}}
 +
 +
{{Spoiler|
 +
<xh4>Implementierung in C#</xh4>
 +
|
 +
<sourcecode lang="csharp" tab="4">
 +
bool lineSegmentCollision(Vector a1, Vector a2,
 +
  Vector b1, Vector b2)
 
{
 
{
 
const float denom = (b2.y - b1.y) * (a2.x - a1.x) -
 
const float denom = (b2.y - b1.y) * (a2.x - a1.x) -

Version vom 5. November 2011, 14:00 Uhr

Klicke hier, um diese Version anzusehen.

Meine Werkzeuge
Namensräume
Varianten
Aktionen
Navigation
Werkzeuge