开发者

Collision detection on a rectangle, how to know which side was hit

I want to do a collision detection system for a rectangle to use some physics, so there will be a normal for each of the sides. How do I determine which 开发者_如何学JAVAside of the square was hit by the object. The other object would be a circle.

Thank you.;


It's hard to know exactly without knowing the library you're using (or seeing your code, if you wrote it yourself), but presumably you're simply looking for overlap in the bodies of the two objects.

The algorithm has to do some sort of math to find out if there are coordinates of one object inside the boundaries of the other object. Assuming that you're just working on an x-y plane, you can compare the center of one object to the overlapping area. Positive delta x, right; negative delta x, left. The y axis would work the same way. This wouldn't necessarily give a guaranteed answer if the corners collided.


I know this is late but it is possible to take the point colliding into the rectangle, subtract it from the the center of the of the rectangle it collided with. Take the new vector from the subtraction and compare it to the four normals of the rectangle, whichever its closest to is the side that was hit. A quick method when you only have a few sides on the but when you have lots of sides to compare it with you will slow down. And I agree this question has nothing to do with code, he didn't ask us to code it for him. He asked for an algorithm.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜