3 Replies Latest reply on Nov 3, 2009 6:39 AM by kglad

# Intersection of line segments vs. hitTest

I'm trying to improve upon the hitTest() function.

I have a game in development that involves square robots, rectangular wall shapes, and line segment shots rotating and moving around. Without the rotation, hitTest works fine, but with rotation, problems arise.

Walls at rotation 0, and square robots work fine. There is a small amount of funkyness with a rotated robot by a wall corner, but it's not very noticable.

The big issue is collision of two rotated square robots.

I can create a function to get the line segments that make up each robot, and the check to see if any intersect. This will mean finding the location of eight points (using height and width and some trig to deal with rotation), finding the slope of eight lines, and then testing lines for intersection up to 16 times. This would be needed to find the intersection of each pair of robots. Those are worst case numbers, some colisions can be detected or ruled out before getting that far in the algorithm, but this check would need to be run every frame against all robots and walls and shots (shots could be a single line segment, though)

Right now I'm talking about 8 robots max, maybe 6-20 walls, maybe 5-80 shots on screen @ 30 frames a second.

This is easy enough to create, but before I spend a day coding it all, I'm wondering if the computation requirements are going to make it prohibitive.

(If so, I'll probably have to switch to circular robots)

I feel like there should be a simple formula to find if two rectangles on a cartesian plane intersect, but I can't find one.

• ###### 1. Re: Intersection of line segments vs. hitTest

you can use a shape-based hittest.  gskinner has one that he's made public or, you can create your own using the bitmapdata class'es hittest.

• ###### 2. Re: Intersection of line segments vs. hitTest

Thanks!

Part of my problem is that, being new to actionscript, I don't know about these exsisting solutions.

And trying to search in google turns up a lot of chaff to wheat, due to 1) The prevalance of AS 3 and 2) Not really knowing the 'proper' terms to search for.

• ###### 3. Re: Intersection of line segments vs. hitTest

you're welcome.