Jump to content
  • Advertisement
Sign in to follow this  
Darrn

Unity vector based collision detection

This topic is 5219 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!