• Advertisement
Sign in to follow this  

Separating Axis Problem(not working on non-AABB)

This topic is 3028 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have 2 collision poly's as defined as such:
package
{
	import flash.geom.Point;
	public class CollisionPoly
	{
		public var points:Array;
		public var edges:Array;
		
		public function CollisionPoly(vertexes:Array):void
		{
			this.points = vertexes;
			this.edges = new Array();
			for(var i:Number = 0; i < points.length; i++)
			{
				var p:Point = points;
				var nextPoint:Point = points[(i + 1) % points.length];
				edges[edges.length] = p.subtract(nextPoint);
			}
		}
		
		public function projectToAxis(axis:Point):Projection
		{
			var min:Number = int.MAX_VALUE;
			var max:Number = -int.MAX_VALUE;
			
			for(var i:Number = 0; i < points.length; i++)
			{
				var p:Point = points;
				var dotProduct:Number = (p.x * axis.x) + (p.y * axis.y);
				if(dotProduct < min)
					min = dotProduct;
				if(dotProduct > max)
					max = dotProduct;
			}
			
			return new Projection(min,max);
		}
		
		public function intersects(c:CollisionPoly):Boolean
		{
			var allEdges:Array = new Array();
			for(var i:Number = 0; i < edges.length; i++)
			{
				allEdges = edges;
			}
			for(var j:Number = 0; j < c.edges.length; j++)
			{
				allEdges[j + i] = c.edges[j];
			}
			for(i = 0; i < allEdges.length; i++)
			{
				var edge:Point = allEdges;
				var axis:Point = edge.clone();
				axis.normalize(1);
				//Make perpendicular
				var tempx:Number = -axis.y;
				axis.y = axis.x;
				axis.y = tempx;
				
				var projection:Projection = projectToAxis(axis);
				var cProjection:Projection = c.projectToAxis(axis);
				if(!projection.intersects(cProjection))
					return false;
			}
			return true;
		}
	}
}


I have 2 collision poly's for isometric diamonds that this code is incorrectly identifying as intersecting the first poly is at [[84,0],[124,20],[84,40],[44,20]] and the second is at [[40,25],[80,45],[40,65],[0,45]] as shown in this picture it's showing as colliding when it's not I've stepped through the code and I can't find where in the intersects function it's going wrong, so I'm crying halp here link to the demo if it helps(click & drag the shapes)

Share this post


Link to post
Share on other sites
Advertisement
Solved it, turns out the flash point class's x & y are read only and don't pipe up when you try to change them, so by changing:

//Make perpendicular
var tempx:Number = -axis.y;
axis.y = axis.x;
axis.y = tempx;

to:

//Make perpendicular
axis = new Point(-axis.y,axis.x);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement