11,934,443 members (55,312 online)

274.8K views
150 bookmarked
Posted

# 2D Polygon Collision Detection

, 20 Sep 2006 MIT
An article on polygon collision detection. Can be used to implement collision between sprites in a 2D game. The algorithm can also be extended to 3D.
 polygoncollision_demo.zip PolygonIntersection (Intersection).application PolygonIntersection (Intersection).exe PolygonIntersection (Intersection).exe.manifest polygoncollision_src.zip obj Debug PolygonIntersection (Intersection).exe.manifest Refactor TempPE Release PolygonIntersection (Intersection).exe.manifest Refactor TempPE Properties Settings.settings using System; using System.Collections.Generic; using System.Text; using System.Drawing; namespace PolygonIntersection { public struct Vector { public float X; public float Y; static public Vector FromPoint(Point p) { return Vector.FromPoint(p.X, p.Y); } static public Vector FromPoint(int x, int y) { return new Vector((float)x, (float)y); } public Vector(float x, float y) { this.X = x; this.Y = y; } public float Magnitude { get { return (float)Math.Sqrt(X * X + Y * Y); } } public void Normalize() { float magnitude = Magnitude; X = X / magnitude; Y = Y / magnitude; } public Vector GetNormalized() { float magnitude = Magnitude; return new Vector(X / magnitude, Y / magnitude); } public float DotProduct(Vector vector) { return this.X * vector.X + this.Y * vector.Y; } public float DistanceTo(Vector vector) { return (float)Math.Sqrt(Math.Pow(vector.X - this.X, 2) + Math.Pow(vector.Y - this.Y, 2)); } public static implicit operator Point(Vector p) { return new Point((int)p.X, (int)p.Y); } public static implicit operator PointF(Vector p) { return new PointF(p.X, p.Y); } public static Vector operator +(Vector a, Vector b) { return new Vector(a.X + b.X, a.Y + b.Y); } public static Vector operator -(Vector a) { return new Vector(-a.X, -a.Y); } public static Vector operator -(Vector a, Vector b) { return new Vector(a.X - b.X, a.Y - b.Y); } public static Vector operator *(Vector a, float b) { return new Vector(a.X * b, a.Y * b); } public static Vector operator *(Vector a, int b) { return new Vector(a.X * b, a.Y * b); } public static Vector operator *(Vector a, double b) { return new Vector((float)(a.X * b), (float)(a.Y * b)); } public override bool Equals(object obj) { Vector v = (Vector)obj; return X == v.X && Y == v.Y; } public bool Equals(Vector v) { return X == v.X && Y == v.Y; } public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); } public static bool operator ==(Vector a, Vector b) { return a.X == b.X && a.Y == b.Y; } public static bool operator !=(Vector a, Vector b) { return a.X != b.X || a.Y != b.Y; } public override string ToString() { return X + ", " + Y; } public string ToString(bool rounded) { if (rounded) { return (int)Math.Round(X) + ", " + (int)Math.Round(Y); } else { return ToString(); } } } }

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

## Share

 Software Developer Pogopixels Ltd United Kingdom
Pogopixels is a London based software company specialising in the development of widgets, Flash and internet-based software.

It delivers innovative software solutions to companies using the latest technologies including Adobe AIR, Yahoo Widgets, or Google Desktop.