Click here to Skip to main content
15,886,556 members
Articles / Multimedia / GDI+

2D Polygon Collision Detection

Rate me:
Please Sign up or sign in to vote.
4.77/5 (53 votes)
20 Sep 2006MIT3 min read 452.9K   14.7K   156  
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.
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();
			}
		}


	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

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.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer Pogopixels Ltd
United Kingdom 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.

Have a look at pogopixels.com for more information.

On my spare time, I work on the Appetizer open source project: http://app.etizer.org It's a portable dock for Windows.

Comments and Discussions