Swept AABB collisions and the crack problem

Started by
4 comments, last by DekuTree64 10 years, 10 months ago

Hello! Copying this over from a post I made on tigsource, hopefully I can articulate my troubles sufficiently.. I've been working on a tile based game that might end up involving high velocities and decided to go for a continuous collision detection sort of model, due to some nasty looking behaviour where the character approaches the edge of a tile too fast. I've done some searching around at various implementations of CCD, and it seems to vary from example to example.
Having looked at ThemsAllTook's implementation and guide for swept AABB collisions here I understood the theory but found the actual intersection check a little hard to follow, particularly with the use of previous positions as opposed to velocities - but I suppose they're equivalent. I've ended up settling for an amalgam of that and a GameDev article on swept collisions (which I found a bit more intuitive).

The trouble is, I'm finding it difficult to overcome the surface area 'stubbed toe' problem. I figured I could find out minimum translation distances from new positions and intersection positions, but sorting by the surface area calculated in that fashion (technically just on one axis in the case of my implementation) doesn't seem to quite work and I get edge 'stopping' even in the case of just one collision.

Here is a demo of it in action, you can control it with arrow keys or the mouse - http://www.filz.us/view/1e940c5a-764/. The most noticable effect is if you slide up against one side, then slide off it on that axis and try to slide back - you'll get stuck on the corner.

Another thing I wasn't quite clear on was the fact that I found myself having to set the object's position to its corrected one, is that standard for a CCD simulation? I wasn't sure if I'd only be dealing with negating velocities and so on, it just struck me as odd. Without the positional correction in the collision resolution, I'd get issues with persistent forces acting on the body causing it to sink through other AABBs.

This is the simplified code, if it's at all decipherable I'd really appreciate it if somebody could point out how to go about solving the problem, and if my implementation of CCD even makes sense - http://pastebin.com/xWFsuxWm

Advertisement
With an exact time of impact for known colliding features there should be no need for "corrected" positions and other hacks: just apply forces and torques and continue the simulation. If you paid the price of exact CCD calculations, you should reap the benefit of really correct physics.

Omae Wa Mou Shindeiru

Of course one is left wondering what "exact time of impact" means since timing is one of the first monumental issues to solve when dealing with realtime physics. We could go on noticing we're talking about a logic-controlled object for which standard physics often has limited meaning. Or ask ourselves what "really correct physics" even is. And what about accurate computing on FPU?

Personally I'm bold on this topic. All those dudes telling you can do physics yourself (especially by doing canonical math) are doing a disservice to the community.

The problem OP notes is a clear demonstration of how canonical math work. Let me show you visually what happens:

[attachment=16042:2013-05-31 10_27_08-Greenshot.png]

The small box here is being pulled to the mouse pointer. Small box upper left corner, when unconstrained will match cursor hotspot.

Expected behaviour: slide to a nerby position (probably on right box lower edge).

What happens: got stuck over the corner between the two boxes to the left (as shown).

Observation: as soon as the pointer moves down a few pixels, crossing right box bounduary, the small box is able to reach the cursor.

This behaviour is mathematically sound and correct to a certain degree. How exactly doing more accurate math solves the problem is unclear to me.

What a physics library would do at this point is to put aside the math for a second and consider the two collision primitives are really meant to be contiguous and filter collision accordingly. This does not always work however. That's an indication on how difficult this problem is.

So my suggestion is: screw all that. Get some physics library you can use in flash and leverage the effort of someone focusing to this problem specifically. BTW, I've been told flash is going to have rough times ahead.

But supposing you cannot do that, I'd get the hit, and check all other collision primitives for being nearby the one I hit and if a match is found, ignore the collision.

Hopefully this will allow the box to slide a few frames and, provided your de-encroaching works it will be correctly pulled "down" and there should be no problem from now on.

Previously "Krohm"

@ LorenzoGatti That was my instinct, too, which made me wonder as to the specifics of my implementation, and where I'd.. gotten it wrong. With constant forces such as the mouse joint, it would bounce off on an initial swing (if the response was made elastic) then sink straight through. Is it to do with the ordering of resolving collisions, applying outside forces / impulses, and the integration itself?

@Krohm I understand your point fully about tools and libraries already existing to tackle this problem (box2d suffers from this also, you have to use edge chains I believe for static objects in sequence). However I do also view this as a learning experience and I value the process of solving the problem. I don't have a tight deadline or anything, I quite enjoy getting lost in the intricacies of physical simulation!

The main issue I have at this point is that I've seen from SacredSoftware's implementation that it is possible to get a more general solution, through the use of sorting collision priority based on which collision has a greater surface area of penetration in a given timeslice (or rather, time step with a threshold, as of course CCD would only really solve one at a time so you have to step forward and test a little). What puzzles me is the two facts that a) It does not function correctly without positional correction, and b) It gets stuck on an edge even in the case of a single box. I suppose if it's correcting it to the EXACT edge position, when it moves back down it would maybe get snagged on a corner? But is that correct behaviour?.. It's not common to many others, which makes me think the penetration resolution isn't quite correct, in terms of initial contact times.

Upfront, I am not an expert. But I was thinking, if your goal is to find the collision point closest to the desired destination, given that you're working with AABBs there are only two directions you can take to get closer to the destination. Find the initial collision point, then try going in one of the two cardinal directions from that collision point. If you travel right from the "crack" in the diagram above there should be no collision.

I've always done 2D collision detection as a two step process. Usually X first, then Y, so in platformer type games you have a better shot of getting your toe onto a ledge before moving down. Really easy and 100% accurate and free of problems like this, at least until you start adding slopes, where horizontal movement causes vertical movement, which can either get you over a hill, or cause your head to run into something. It is possible to solve with 100% accuracy, but it's easier to just put restrictions on your levels (e.g. don't even put slopes where they'll wedge you against a ceiling). Also, an overhead game where moving up/down into slopes causes horizontal movement, will usually require a somewhat different approach than a platformer.

Having objects stand on top of eachother and push eachother around is the other main way to add difficulty... the update order becomes important if you don't want anything to lag by one frame from where it should be.

And for high velocity, just divide the velocity by the tile size, and call the collision function multiple times.

Another fun system I did one time was a billiards game where I'd test every pair of balls against eachother, calculating the time of collision with both their velocities accounted for... then update the whole simulation to the time of the first collision, and repeat until the full frame time elapsed. Really slow, but certainly solves the update order problem :)

But none of that compares to the pain that is 3D collision. Unless you keep all your levels axis aligned, you will have numerical accuracy problems. 99% of the time everything is fine, but every once in a while you do a sweep test, it says everything is fine, but after moving, doing an intersection test with the plane returns true. Or small velocities letting you creep up on a wall until you go through it. Or calculating a slide vector causing you to run into the same wall again. Near impossible to be 100% fall-through-proof, and usually requires various hacks checking for the various ways things can go wrong and attempting to correct the situation in a way that doesn't jar the player.

Anyway, I don't think there's a totally straightforward solution to the corner snagging problem in the system you're using, but you can try to understand why it's a problem, and devise a solution. IMO, the root of the problem is that you're considering each object individually, but it's only together that they create a continuous surface. The X velocity only gets cut because you're allowed to move inside of one rectangle just a tiny bit while checking against the other. The surface area test in that first article is what I would call a hack to deal with this particular situation that comes up. I'm sure there are others, but I'm not sure if there's a really clean and natural way to solve it.

This topic is closed to new replies.

Advertisement