very simple 2D collision detection?
I'm looking to write a collision detection system for a cheap 2D scroller game. So far, I've got an entity that has a bounding box which consists of ints for top, left, bottom, and right. Rectangular bounding boxes are all I'm going to need.
And thats about as far as I've made it so far.
I need help with the actual "collision" part now.
Hmmmm? I think collision between two axis oriented bounding boxes (that is: rectangles) is the simplest thing on earth :-)
In my last techdemo I've done it that way:
// single pointstruct TScrPoint { int x, y; };// rectangle defined by:// v1: left down vertex// v2: right up vertex struct TRectangle { TScrPoint v1, v2; }; // returns true if they overlap// you may wish to change ">" and "<" to ">=" and "<="// you should be able to figure out how does it work :-)// just draw two rectangles on paper...bool RectanglesOverlap(const SC :: TRectangle & _first, const SC :: TRectangle & _second) { return ( (_first.v1.x < _second.v2.x) && (_first.v2.x > _second.v1.x ) && (_first.v2.y > _second.v1.y) && (_first.v1.y < _second.v2.y) ); }
IMHO the hardest part is collision response...
Anyway, I would suggest visiting:
2d game tutorial
Jump n run
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement