13,050,764 members (86,051 online)

#### Stats

316.5K views
153 bookmarked
Posted 13 Sep 2006

# 2D Polygon Collision Detection

, 20 Sep 2006
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.
 PolygonIntersection (Intersection).application PolygonIntersection (Intersection).exe PolygonIntersection (Intersection).exe.manifest obj Debug PolygonIntersection (Intersection).exe.manifest Refactor TempPE Release PolygonIntersection (Intersection).exe.manifest Refactor TempPE Properties ```using System; using System.Collections.Generic; using System.Text; using System.Drawing; namespace PolygonIntersection { public class Polygon { private List points = new List(); private List edges = new List(); public void BuildEdges() { Vector p1; Vector p2; edges.Clear(); for (int i = 0; i < points.Count; i++) { p1 = points[i]; if (i + 1 >= points.Count) { p2 = points[0]; } else { p2 = points[i + 1]; } edges.Add(p2 - p1); } } public List Edges { get { return edges; } } public List Points { get { return points; } } public Vector Center { get { float totalX = 0; float totalY = 0; for (int i = 0; i < points.Count; i++) { totalX += points[i].X; totalY += points[i].Y; } return new Vector(totalX / (float)points.Count, totalY / (float)points.Count); } } public void Offset(Vector v) { Offset(v.X, v.Y); } public void Offset(float x, float y) { for (int i = 0; i < points.Count; i++) { Vector p = points[i]; points[i] = new Vector(p.X + x, p.Y + y); } } public override string ToString() { string result = ""; for (int i = 0; i < points.Count; i++) { if (result != "") result += " "; result += "{" + points[i].ToString(true) + "}"; } return result; } } } ```

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.