[SHOW] Collision Detection/Geometry Lib
Every time I start on a game (which is way more often than when I finish one), I always end up rewriting the game element framework, the geometrical representation of the view, the relationship between the 2, collision detection, etc.
So I wanted to write a geometry library that offers a framework that handles this, as automatically and independently as possible.
My key goals were/are:
- Make collision detection easy
- Make the framework easily usable with or without the DisplayObject hierarchy
- Make the geometry bindable to DisplayObjects and vice versa
- Automatically generate convex geometry based on DisplayObject hierarchy
- Offer simple collision detection queries for use with or without the geometry library
This is a totally lame demo, but it illustrates a few things. Here's what's going on.
I create a couple container DisplayObjects as children to each other to prove transformation working. Then I add a bitmap (a spaceship .png), and 2 shapes. 1 shape, I use the graphics api to draw a triangle, and the other a circle.
So at that point, I've done nothing at all with the library. Then I call Geometrer.createHierarchy and pass the DisplayObjectContainer that holds everything else. This method recurses over all children and populates an array with each container or shape's geometrical counterpart from the library (a triangle, a circle and the convex hull (polygon) of the spaceship graphic).
The demo is drawing all of the strokes (circle, triangle, polygon and AABBs) at the global level. Because the shapes are bound to the DisplayObjects, I'm simply updating the orientation and position of a few of these DisplayObjects each frame and the geometry binds accordingly.
The geometry is determined by drawing each DisplayObject into bitmap data, doing some analysis to find edge pixels, and the convex hull of these points. Then the points are reduced to hopefully find a neat, tidy geometry fit from the library. If not, an algorithm, tries to determine if it's a circle. Otherwise the n-dimensional convex hull is returned.
here's what the code involving the library looks like in the demo:
Code:
//maps DisplayObjects to geometrical counterparts
var map:Dictionary = new Dictionary();
//holds a list of all constructed shapes
var shapes:Vector.<ITransformable> = new Vector.<ITransformable>();
var shapeRoot:ITransformable = Geometrer.createHierarchy( base, shapes, map );
//then i can access a shape/container via the map
var myElement:ITransformable = map[ myDisplayObject ];
fp10 demo:
http://lab.generalrelativity.org/glib/hierarchy/
I'll be releasing everything under the MIT license once the collision detection is built up more. I'm curious in hearing level of interest etc. Thanks!