Game Development Reference
In-Depth Information
class RectangleCollider
{
public:
RectangleCollider() : Enabled(true),
Position(0,0), Size(0,0) {};
bool Enabled;
XMFLOAT2 Position;
XMFLOAT2 Size;
RectangleCollider(XMFLOAT2 topLeft, XMFLOAT2
size);
bool CollidesWith(RectangleCollider *b);
float X1();
float X2();
float Y1();
float Y2();
};
Most of this is straightforward, but we also have some helper methods down the bot-
tom, which provide the top-left and bottom right-corners of the rectangle as their in-
dividual components for use in the algorithm.
The main method we need to implement is the RectangleCol-
lider::CollidesWith method.
bool
RectangleCollider::CollidesWith(RectangleCollider
*b)
{
bool test1 = X1() < b->X2();
bool test2 = X2() > b->X1();
bool test3 = Y1() < b->Y2();
bool test4 = Y2() > b->Y1();
return test1 && test2 && test3 && test4;
}
Search WWH ::




Custom Search