February 26, 2007 on 8:59 pm | In Actionscript | No Comments
I am currently looking for a simpler and faster framework for my physics engine. The present framework uses events all over the place - even at a very low level for broadcasting internal collision/contact information. With this in mind I tried to figure out how it compares in terms of speed to just invoking the event handler method on the listeners.
If for example 10 objects are constantly in contact, then about 100 events are dispatched every frame, so I thought using events produces a hefty slowdown (registering/removing listeners, creating and populating event object instances…). After running some benchmarks it turned out that the direct invocation is just twice as fast - far less than I thought, which is of course a good thing :-).
By the way, in AS2 the fastest way to dispatch events is using the AsBroadcaster class, and the more listeners you add, the faster it gets compared to direct method invocation.
February 17, 2007 on 11:08 pm | In Actionscript | No Comments
Computing the smallest bounding circle from a set of points is a tricky task and it’s not as straightforward as creating a bounding box. Admittedly I can’t think of many applications for it - maybe you could use it for collision culling of soft-bodies, where you mostly get a tighter bounding representation than with a box. Now to the algorithms:
- A rough, but very fast approximation. It basically just computes the minima/maxima points (like for a bounding box) and takes the average of all points to compute the circles center (red circle).
- A near-optimal bounding circle [1], which produces a closer bounding circle than the first one, but also with twice the cost (green circle).
- An exact bounding circle [2]. This is the slowest of the three (roughly 20x slower), but results in a perfect bounding circle. Ideal for precomputing static geometry (blue circle).
(press space for random data)
Download
All three algorithms are implemented in AS3. Grab them here.
Usage
Usage is very simple. Just create an array of flash.geom.Point instances and pass them to the bounding circle function to retrieve a circle instance.
import flash.geom.Point;
var points:Array = [new Point(5, 5), new Point(23, -7),...];
var circle:Circle = BoundingCircle.boundingCircle1(points);
trace("position=" + [circle.x, circle.y] + ", radius=" + circle.radius);
Works cited
[1] An Efficient Bounding Sphere, Jack Ritter, Graphics Gems
[2] An Easy Bounding Circle, Jon Rokne, Graphics Gems II
February 15, 2007 on 3:28 pm | In Actionscript, Physics | 2 Comments
Last week I have finally resumed work on my physics engine, so you can expect more frequent updates in the future :-) First thing I did was rewriting and optimizing the portion of the code that handles and resolves collisions, because I was still not satisfied with the overall performance and felt that I can further improve it.
The third revision is not only more robust, but also runs 3-4x faster - collisions between convex polygons are even 9x faster. I am very excited about the latest improvements, and the engine is now ready to handle massive polygon counts. This was done with some radical design changes:
- Collisions are split into specialized classes to handle an individual object pair whereas the former version quite often used a generic polygon vs polygon collision approach.
- Collisions now coexist in two packages: discrete and continuous (or static and dynamic). The idea is, that you define a speed threshold value to blend between both versions.
For example, if you know in advance that the objects will not move at insane speeds and the chance that collisions are missed is rare or it doesn’t matter at all the fast static test is used, and if objects are moving very fast (like bullets) the dynamic collision system kicks in (which is roughly half as fast). This should give a higher frame rate, especially when frame coherence is high.
- The code was further optimized to drastically decrease execution time. The downside is that the code is now quite obfuscated and barely readable - but without pain no gain ;-)
Next I’ll try to optimize contact point calculations, followed by a version which has different strategies for collision culling.