PDA

View Full Version : Rectangle Hit Test



DashMasterful
May 15th, 2009, 12:04 PM
I have a basic problem, I'm hit testing two rectangles. There is a static rectangle and one that moves, basically the moving rectangle can come in from any direction and hit the static one. I find that the moving box will actually get stuck to the static one, is there a way to push the moving box out once it hits the static box so that it is not technically hit testing anymore, I know there was a way to do this with circles, but I wasn't sure about rectangles. Please take a look at the temp .swf i created

http://ec2-72-44-50-95.z-1.compute-1.amazonaws.com/test.swf

Krilnon
May 15th, 2009, 02:26 PM
In whatever code you're using to move the moving rectangle, you should be able to predict the next location of the box, as long as the user doesn't click elsewhere. Since you can predict the collision, you can stop the box a pixel or two before it actually hits the rectangle.

Another approach would be to wait for the hitTest and then knock the block back to its previous position, or wherever you want it to go.

DashMasterful
May 15th, 2009, 03:18 PM
ok thanks, i will give that a try

Harito
May 16th, 2009, 04:50 AM
Seems to me like you are checking for collision after the move, so it misses the collision check in the first move.

All you gotta do is check for collision of the step is about to take before it takes it, in this case, check before the first step.