The web site for Flash ActionScript 3.0 game developers

 
         
   

hitTestObject Not As Expected

John Writes:

I have a long rectangle shaped movie clip and a ball on my stage. I want to detect when the ball hits the rectangle. The rectangle is angled at 45 degrees using transform. hitTestObject appears to true based on a rectangular are that encompasses the entire rotated movieclip (Imagine drawing a rectangle around the object so that the object goes from the upper left to lower right corners of the rectangle).
I want to test when the ball is touching the actual movie clip - not the area around it - how do I do that?

You are right. The hitTestObject function just looks at the bounding boxes, not the actual shape of the objects. If you think about it, this makes sense. Can you imagine the complexities of looking at the shape of two Flash display objects and determining if they overlap? Such a function would be very slow, which is why I imagine it doesn't exist. I talk a bit about how hitTestObject and hitTestPoint work in chapter 2, in the section on Collision Detection.

So, to your problem. If the ball is small enough, then you can simply use hitTestPoint with the point being the center of the ball. But if the ball is too large, then you'll need to dig into deeper math to determine when the ball actually touches the side of the rectangle. You can do a Google search on "collision detection circle line" to see some articles on the subject. It is pretty complex if you don't have a background in math.



Copyright Gary Rosenzweig