package engine 
{
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author Fiodar Paulavets
	 * @version 1.5
	 * @since 06.05.2012
	 */
	public class Collisions 
	{
		/* 
		 * x и y - центр фигуры 
		 * углы в градусах 
		*/
		
		public static function intersectCircleAABB(cx: Number, cy: Number, cr: Number, aabbx: Number, aabby: Number, aabbw: Number, aabbh: Number): Boolean
		{
			return (cx + cr >= aabbx - aabbw / 2) && (cx - cr <= aabbx + aabbw / 2) && (cy + cr >= aabby - aabbh / 2) && (cy - cr <= aabby + aabbh / 2);
		}
		
		public static function intersectCircleCircle(x1: Number, y1: Number, r1: Number, x2: Number, y2: Number, r2: Number): Boolean
		{
			return (Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) <= r1 + r2);
		}
		
		public static function intersectCircleLine(x: Number, y: Number, r: Number, x1: Number, y1: Number, x2: Number, y2: Number, lineWidth: Number = 1): Boolean
		{
			var dx: Number = x - (x1 + x2) / 2;
			var dy: Number = y - (y1 + y2) / 2;
			
			var a: Number = MathExt.GetAngle(x1, y1, x2, y2);
			
			var cx: Number = dx * Math.cos(a) + dy * Math.sin(a);
			var cy: Number = dy * Math.cos(a) - dx * Math.sin(a);
			
			var hw: Number = MathExt.Distance(x1, y1, x2, y2) / 2;
			var hh: Number = lineWidth / 2;
				
			return cx <= (hw + r) && cx >= -(hw + r) && cy <= (hh + r) && cy >= -(hh + r);
		}
		
		public static function intersectLineLine(ax1: Number, ay1: Number, ax2: Number, ay2: Number, bx1: Number, by1: Number, bx2: Number, by2: Number): Boolean
		{
			var denominator: Number = (by2 - by1) * (ax2 - ax1) - (bx2 - bx1) * (ay2 - ay1);
			if (denominator == 0)
				return false;
			
			var numinator1: Number = (bx2 - bx1) * (ay1 - by1) - (by2 - by1) * (ax1 - bx1);
			var numinator2: Number = (ax2 - ax1) * (ay1 - by1) - (ay2 - ay1) * (ax1 - bx1);
				
			if ((numinator1 / denominator <= 1 && numinator1 / denominator >= 0) && (numinator2 / denominator <= 1 && numinator2 / denominator >= 0))
				return true;
			return false;			
		}
		
		public static function intersectLineAABB(x1: Number, y1: Number, x2: Number, y2: Number, aabbx: Number, aabby: Number, aabbw: Number, aabbh: Number): Boolean
		{
			aabbw /= 2;
			aabbh /= 2;
			var ltx: Number = aabbx - aabbw, lty: Number = aabby - aabbh;
			var rtx: Number = aabbx + aabbw, rty: Number = aabby - aabbh;
			var lbx: Number = aabbx - aabbw, lby: Number = aabby + aabbh;
			var rbx: Number = aabbx + aabbw, rby: Number = aabby + aabbh;
			return (intersectLineLine(x1, y1, x2, y2, ltx, lty, rtx, rty) || intersectLineLine(x1, y1, x2, y2, rtx, rty, rbx, rby) ||
					intersectLineLine(x1, y1, x2, y2, rbx, rby, lbx, lby) || intersectLineLine(x1, y1, x2, y2, lbx, lby, ltx, lty));
		}
		
		public static function intersectAABBAABB(x1: Number, y1: Number, w1: Number, h1: Number, x2: Number, y2: Number, w2: Number, h2: Number): Boolean
		{
			return (x1 + w1 / 2 >= x2 - w2 / 2) && (x1 - w1 / 2 <= x2 + w2 / 2) && (y1 + h1 / 2 >= y2 - h2 / 2) && (y1 - h1 / 2 <= y2 + h2 / 2);
		}
		
		public static function intersectCircleOBB(x: Number, y: Number, r: Number, rx: Number, ry: Number, rw: Number, rh: Number, ra: Number): Boolean
		{
			var dx: Number = x - rx;
			var dy: Number = y - ry;
				
			var a: Number = ra * Math.PI / 180;
				
			var cx: Number = dx * Math.cos(a) + dy * Math.sin(a);
			var cy: Number = dy * Math.cos(a) - dx * Math.sin(a);
			
			var hw: Number = rw / 2;
			var hh: Number = rh / 2;
				
			return cx <= (hw + r) && cx >= -(hw + r) && cy <= (hh + r) && cy >= -(hh + r);
		}
		
	}

}