开发者

Collision with shapes other than rectangles..?

I'm so used to working just with rectangles for collision detection that I'm a bit stumped right now. I'm working with diamond-like shapes, and for the past few hours, have been trying to figure out how to check for collision.

I tried checking to see if the first objects four points are inside the points of the second object, but that just makes a box (I think)

The reason why I feel like I'm having dif开发者_JS百科ficulty with this is because of the angles.


You're trying to collide a moving convex polygon (your "diamond") with another moving convex polygon, is that right? Something like this:

Collision with shapes other than rectangles..?

Your first step should be to transform the problem to an equivalent one in which one of the polygons is stationary:

Collision with shapes other than rectangles..?

Then you can transform the moving polygon into a "shaft" that covers the area swept by the moving polygon. This is straightforward to do: if the original polygon has n sides, then the shaft has n + 2 sides, with the extra two sides being the same length and direction as the movement vector. You find where to insert these new sides by sorting the vertices according to their component that's orthogonal to the movement vector, and inserting new sides at the maxima.

Collision with shapes other than rectangles..?

Now you've reduced the problem to static polygon against static polygon. Taking a look at the handy table of collision algorithms courtesy of realtimerendering.com, and following the references, we can see that we need to use the separating axis test, for example as described in section 3 of this paper by David Eberly.

In two dimensions, two convex polygons fail to intersect if we can find a separating axis, a line such that one polygon falls on one side of the line, and the other polygon on the other:

Collision with shapes other than rectangles..?

If we are given a direction, we can easily discover if there exists a separating axis that runs in that direction, by projecting the two polygons onto a line running perpendicular to that direction, and looking to see whether the projections are disjoint:

Collision with shapes other than rectangles..?

How do we know which direction the separating axis will run in? Well, if any separating axis exists, then there's one that runs parallel to one of the sides of one of the convex polygons (see Eberly, page 3). So there's only a small set of directions to check, and if you've checked them all without finding a separating axis, then the two polygons intersect (and hence the original moving objects collide).

There are lots of refinements and optimizations you can make, certainly not limited to these:

  1. Before doing the full moving polygon/polygon test, do a simpler test like circle/circle so that you can reject easy cases quickly.
  2. Use some kind of spatial partitioning scheme like quadtrees so that you only test objects that are close enough that they might collide.
  3. "Caching witnesses" — if a line separates two objects at time t, it's likely that it continues to separate them at time t + δ, so it can pay to remember the separating axis you found and try it first next time (see Rabbitz, "Fast Collision Detection of Moving Convex Polyhedra" in Graphics Gems IV).

But don't worry too much about optimizing: get it right first in the confidence that you'll be able to speed it up later.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜