Thursday, May 23, 2019

Secrets 1

2D box-box intersection:

So someone asked me to help, how a basic intersection looks like. The game uses boxes, so i posted my box-box intersection implementation. My post from derpibooru:

the 2d box collision is the following:

if box a maximum x smaller than box b minimum x
OR
if box a minimum x larger than box b maximum x
the boxes are away from each others, collision false;

if box a maximum y smaller than box b minimum y
OR
if box a minimum y larger than box b maximum y
the boxes are away from each others, collision false;

if we are here, we have only one case left, collision detected, because the minimum x or maximum x is between the minimum and maximum x and the minimum y or maximum y is between the minimum and maximum y

how this looks as code? (just quickly, i hope its correct xD)


class vec2
{
public:
float x;
float y;
};

class box
{
public:
vec2 pos;
vec2 vol;
};

bool interBoxes(box _a,box _b)
{
if( a.pos.x+a.vol.x<=b.pos.x-b.vol.x||a.pos.x-a.vol.x>=b.pos.x+b.vol.x )
return false;
if( a.pos.y+a.vol.y<=b.pos.y-b.vol.y||a.pos.y-a.vol.y>=b.pos.y+b.vol.y )
return false;

return true;
}

No comments:

Post a Comment