2d Primitive collision detection

BlitzMax Forums/BlitzMax Programming/2d Primitive collision detection

matibee(Posted 2009) [#1]
(Hope it's not too big a post.. heck I've seen entire 3d engines posted.. maybe it should go to code archives, but surely it wants reviewing first.. it's late.. I'm rambling)

As the title states really. This is something I've done before in C++ a looong time ago.

It shows how to use different primitives (circles, rects and polygons) to represent objects and how to check for collisions against each other. Collisions may be penetrations or overlaps it'll detect either. There's some other useful 2d stuff in here too, like point-to-line distance, line-to-line intersection, point-in-polygon etc.

Obviously there's some performance benefits from using simpler objects; Rect vs. Rect is the fastest, Circle vs. Circle is next, but the code is there for all eventualities.

You will have to install the Mauft 2D Vector mod from http://mauft.com/open-source/blitzmax.

Questions? Comments?




Arowx(Posted 2009) [#2]
Cool, right now let's see how fast I can get AWOD to play now!


matibee(Posted 2009) [#3]
:) Let us know!

FWIW, use rects for the bullets.

I've been thinking of some advances too... if collisionCircle and collisionPoly both contain a collisionRect (defined as their bounding rect) a quick and easy first-option would be to see if their bounding rects overlap (can't believe I missed that one).

This would also allow me to make a dynamic quad-tree of all objects using their bounding rects.


Nate the Great(Posted 2009) [#4]
man this would have saved me some trouble... I just had to write something similar for my physics engine and it just hurts my brain... good job though


matibee(Posted 2009) [#5]
Nate, full 2d physics is maybe where I'll take this if I have the time and energy.

I've just recoded it so circle and poly objects derive from the rect base type and I'm about to try a dynamic quadtree with objects spacially sorted using their bounding rects.

More demo's to follow :)


matibee(Posted 2009) [#6]
Update - A thousand moving primitives with quadtree culling :)

I'll leave the original intact so you can see the progress.

Potential colliders are shown in blue, actual colliders in red. You can toggle bounding boxes[B], movement [M] (this avoids rebuilding the tree every frame so watch the fps difference), and the tree itself [T]. Space bar selects various primitive types.




Arowx(Posted 2009) [#7]
Wow that is cool matibee!

It looks like your rebuilding the quadtree each time, do you need to do that?


matibee(Posted 2009) [#8]
Thanks Merx.

It is necessary to rebuild the tree when the objects move. This is not the greatest example of the use of a quadtree because all the objects are limited to screen coords. There's so many objects, and the tree will stop subdividing when it's nodes are less than 20 units wide (See QT_MIN_NODE_SIZE) so the effect is that the tree always looks pretty similar.

If we allowed them to move over a much bigger 'world' we could even use the quadtree test to limit the potentially visible objects we needed to draw.

To see more of the QuadTree workings change the quadtree constants to
const QT_MAX_PER_NODE:Int = 1
Const QT_MIN_NODE_SIZE:Float = 5.0:Float


and limit the demo to just 10 objects (line 551). Keep pressing M and watch the tree shape. While it's paused you'll see the potential colliders are the ones in the same region (leaf node) of the tree.


Mauft(Posted 2013) [#9]
The 2D vector module is now officially available here:

http://retrocade.net/post/138/blitzmax-vector-library

(If the author or anyone else can fix the link in the first post, I'd be grateful)