vector based collision detection

Started by
2 comments, last by oliii 19 years, 8 months ago
i want to make a 2d game and i'm stuck at time-independet collision detection. i read about "vector based collision detection" here: Time-based logic / collision detection. it sounds like a good solution and i wrote this code:

bool Sprite::hasSpriteColl(Sprite *testObj)
{
    Sprite *leftObj, *rightObj, *upperObj, *lowerObj;

    // which is the left object and which the right?
    if( this->posX < testObj->posX)
    {
    	leftObj = this;
    	rightObj = testObj;
    }
    else
    {
    	leftObj = testObj;
    	rightObj = this;
    }

    // which is the upper object and which the lower?
    if( this->posY < testObj->posY)
    {
    	upperObj = this;
    	lowerObj = testObj;
    }
    else
    {
    	upperObj = testObj;
    	lowerObj = this;
    }

    // if the left object crosses the way of the right one and
    // at the same time
    // if the upper object crosses the way of the lower one
    if( leftObj->posX + leftObj->width + leftObj->velX >= rightObj->posX + rightObj->velX &&
    	upperObj->posY + upperObj->height + upperObj->velY >= lowerObj->posY + lowerObj->velY)
    	return true;
    else
    	return false;
}





i didn't test this code, but i noticed that it only works in one dimension. i drew the following image: 1, 2 and 3 are checked correct. 4 is also detected (both conditions are true), but 5 is wrong. the pass each other, but don't collide. what have i forgetten or does someone have a good tutorial about vectorbased collision detection? i only need collision detection for rectangles, no circles, polygons or stuff like that.
Advertisement
Well, I didn't read the article you've mentioned, but I think that problem you encountered is the same as explained in Gems #2 weak side of RDC algorithm. You may want to look at that book (chapter 2.7) or try this:

1. Start from the X: do what your algorithm is doing, but only for the X axis.
2. Now, do the same for the Y: do what your algorithm is doing, but only for the Y axis.
3. If you find out that objects collide, then you must once again check for the collision in X axis.

If that won't make sense to you (and probably won't, couse even for me it looks unlogical ;-) then just google for "Recursive Dimensional Clustering", I'm sure you will find good explanation of this problem.
RDC seems to be very different from vectorbased collision detection, because it checks for collision only at timepoints, not timespans. RDC therefore wouldn't recognize case 4.
btw. it makes sense for RDC algorithm to re-check the x dimension, but not for the vectorbased.
maybe this can help you.
http://www.gamedev.net/community/forums/topic.asp?topic_id=251638

it's close to what you are doing. basically, it falls down to doing several 1D tests, like you demonstrated, along potential 'separation axes'.

in polygons, separation axes are the normals of every edges of the polygon. Hence for axis aligned boxes, you can simplify it to 2 axes only. In dymanic tests (when boxes are moving), you also have to consider the displacement axis.

you can also derive the normal of collision, time of collision, points of collision, but that's other topics, explain in the tutorial.

Everything is better with Metal.

This topic is closed to new replies.

Advertisement