Problem Solving: Another Simple Irregular Shape HitTest
Okay, we have nearly the same problem as before, except this time we are going to be adding some more complicated parts into the equation.
Last time, we had a single tetris shaped block and used rectangles to calculate the area for hitTesting. But as you know, when you rotate a rectangle, it no longer stays your hitTest rectangle. It becomes the bounding rectangle. In this, I am going to show you a new tactic, that of hitTesting two irregular objects
How am I going to do this? With extremely little math. In fact, all it requires is the Pythagorean theorem.
Here are our shapes.
I’m sure your wondering why there is red dots on them.( Are you trying to prevent us from stealing them?) Nope. I will explain in depth in this lesson.
Each one of those red dots represents a point that we are going to use to determine collision. How, you ask? Well, we are going to determine if the distange between any of the tetris block’s point and the curve’s point is less than a certain amount. That certain amount is the radius of one of the blocks that makes up a tetris piece. This will enable us to get a quick, simple, hit testing algorithm which looks pretty realistic and is much less of a hassle than the somewhat pixel perfect collision detecting.
Lets get started by diagramming the points on the tetris block.
var points:Array = []; points.push(new Point(x + width * 1 / 4, y + height * 1 / 6)); points.push(new Point(x + width * 1 / 4, y + height * 3 / 6)); points.push(new Point(x + width * 1 / 4, y + height * 5 / 6)); points.push(new Point(x + width * 3 / 4, y + height * 5 / 6));
Then we want to diagram the the wave’s critical points.
var hitPoints:Array = []; hitPoints.push(new Point(x + width / 2, y)); hitPoints.push(new Point(x, y + height / 2)); hitPoints.push(new Point(x + width, y + height / 2)); points.push(new Point(x + width * 3 / 4, y + height * 5 / 6));
Now, we need a simple double for loop that traverses each point and compares the distance
for (var outer:int = 0; outer < hitPoints.length; outer++)
{
var objPoint:Point = hitPoints[outer];
for (var inner:int = 0; inner < points.length; inner++)
{
var dis:Number = Math.sqrt( Math.pow(points[inner].x - objPoint.x , 2) + Math.pow(points[inner].y - objPoint.y, 2) );
return (dis < (width / 4));
}
}
What does this do? It loops through the wave’s and tetris’ points and uses the Pythagoream Theorem to retrieve the distance between the points. If the distance is less than the width of the tetris block/4, or the radius of the blocks that make up the tetris block, then it will return true.
Now, this test works for rotation, but only if you implement a way to redefine the hit points after rotating the object. I will leave that up to you to create.
Cheers,
UnknownGuardian
Also, please do let me know, if you find an error, or have a better way to hit test irregularly shaped objects.

Great Job.
Awesome.