Very impressive job setting this up. It's a bit of a pain figuring out Alchemy but now I have a cygwin terminal via puttycyg, and a bash script to build and copy the swc to my project folder. I can make a change in C and see it in my running SWF in under a minute. Very sweet, works perfectly, and is much faster than Boris's fine port. I'd say this has made the need for the AS port of box2d obsolete, for my needs at least. It took me 4 hours to convert a large project (
http://www.jetpackhq.com/blog/ - new swf not posted yet) to use this, it was a relatively easy task because I had already wrapped most of box2d for portability.
Changes I had to make to get this working in Flex/FlashDevelop:* I added helper functions to V2 and M22 to make the transition smoother, like Set, SetZero, SetV, NegativeSelf, Length, Add, etc
* note: new CircleShape() no longer takes radius as a parameter
* SetAsOrientedBox() => SetAsBox()
* SetAsVector() => Set() - I changed Set() to take an optional length parm for speed & simplicity - constantly resizing a vector / reallocating elements is slow
* SetPositionAndAngle() => SetTransform()
* SetAngle() => SetTransform()
* SetLocalPosition() => shape.m_p.x = 123
* ApplyLinearImpulse() => ApplyImpulse()
* manifold.m_normal => manifold.normal
* b2world.DrawDebugData() => b2debugdraw.Draw()
* on all my fixtures: m_reportBeginContact = m_reportEndContact = true;
Notes:* The lack of customizable contact filter was a problem for me, what I need is additional complex contact filtering (run in AS) AFTER the regular filter determines there is a contact. To do this, I added ShouldCollide() to the WorldListener in box2d.h and had it call ShouldCollide in b2World.as, only when collide==true.
* I like to initialize a b2BodyDef/b2FixtureDef before each use, so I can set only non-default values. I give each a custom Init() function, but when setting b2FixtureDef.shape=null, there's an error in the setter. I changed it as follows. Not sure if this is valid:
public function set shape(v:b2Shape):void { mem._mw32(_ptr + 4, (v? v._ptr : 0)); _shape = v; }
* In b2World, you are calling getTimer() - the world simulation should not know anything about real time, only the time it is given. This was causing my world step to occasionally skip. I changed the line:
stepTime = getTimer(); to: stepTime += timeStep;
* The documentation on Alchemy is awful, but have you tried AS3_Shim? I wonder if it would make calls from C any faster. Or for that matter AS3_Proxy
* Dynamic objects (Object()) are extremely slow - I'm referring to b2Filter.filter. In my testing, get/set of a declared object member takes 1ns. For a dynamic member: 40ns get, 80ns set
* bug: in b2WorldManifold.Initialize(), if there are no points it returns and the normal may be null. In the C, the normal exists before that call so that doesn't happen. If you create the normal in the constructor (like the C), you can do math directly on it without allocating new V2's all the time (when reusing the manifold).
> If you instantiate a b2Vec2 and manipulate it, you're basically writing over random memory.
That seems like something that should have some protection built around it!
> I've been thinking about ways to merge the V2 / b2Vec2 classes together.
Instead of extending b2Base, why don't you give it a b2Base member (private via a custom namespace), and only communicate with the C++ when the base var is not null?