Alchemy , AS3 and Box2d?

BorisTheBrave wrote:
But still, I'm not sure I like the idea of hacking on Alchemy. Take controllers. They were a breeze in AS3. A bit worse in C++, as I was obliged to use the definition-factory method of allocation. Now I want to add another feature to controllers - you specify an iterator of bodies for them, instead of a manual list. There are 4 iterators initially available - manual, all, inside an AABB, and touching a sensor. It's mainly completed in AS3, and quickly, but I would be hard pushed to write it at all in C++ without introducing some of the higher level features of the language. I could keep that all in the AS3 layer, but you've repeatedly said that it's bad to go frequently between the layers, which that would be doing a lot.


Actually, the controllers I included are in AS3 🙂. They are a slightly different implementation than your own though because I had my own ideas I wanted to try out. I'm really interested in the iterators idea though - especially linking to a sensor. Do you have them in SVN yet?

One idea I'm pondering: Making the "time step" be an AS3 event, and controllers would just listen for that event. That way they could use listener priority to define the order they're called, and even specify to be called before or after the time step. A lot of the high level stuff I'm working on could then be ported to controllers.

My primary purpose for this port is selfish: I'm working on it for my own game. The Box2D community comes second 🙂. So I may end up putting some weird stuff in it...

BorisTheBrave wrote:
BTW, amongst the things on my todolist, there is making BodyDef and FixtureDef optional. That probably goes double for Alchemy, if you think think the definitions are a pain. They have really little benefit in AS3.


I explored this idea at some point but ended up failing. I think it was because the definition properties don't really map 1 to 1 to the body / fixture / etc. properties. Probably could have worked with more thought though.
It's in SVN now. It's a combined controllers/events patch. I opted not to hook b2Controller.Step in via an event, because I decided making the world fully "crawlable" was more important than making controllers a purely user side implementation. (by crawlable, I mean you can access every object from just a reference to the world). But they still listen for the removeBody event, so that you cannot get invalid references in your controller. I might put in an event for when controllers fire, I'm not sure.

None the less, I've put in a truckload of events, and a general dispatching system. Having had a brief look at your higher level components, I should imagine many, if not all, could be hooked in.
Hey this is neat. A couple of thoughts:

1. It doesn't look like the pre-and-post steps are dispatched? Also these could be rolled into one event - Pre-step = listeners with a priority > 0, post step = listeners with a priority < 0. The actual step would have to be a listener at priority zero though. Just an idea - I used to have pre and post step events myself.

2. I don't think recycling events works. I'm pretty sure if you redispatch an event Flash will clone that event no matter what. Plus I find it handy to keep events around for a while - especially contact events, since you can just buffer them and process them post time step. In my implementation, the contact events work as a simplified interface for the contact. They're also biased towards whatever fixture they were dispatched from. So for example, if you take the normal from of the contact event, it will always be pointing away from whatever fixture it was dispatched from.

3. Iterators are a great idea, although I'm having some trouble wrapping my head around your implementation. In my version there is a flag on controllers that will make them use all bodies on the world. This is much better though. I have a "ContactList" class that buffers contact events and then processes them in bulk. This is extended for implementing contact responses. I'm thinking I may be able to move this code into a controller and use the "touching" iterator or something similar.

4. Linked lists (next / prev pointers): I find it more succinct to just throw objects into a dictionary keys. Not sure which is faster though.

It's going to be difficult reconciling this with the code I've already developed, but I'm having a blast looking through this.
1) That's an oversight, will fix. But (like controllers), I want the box2d functionality to remain under the old system, not an event. Retains compatibility + events remain an optional extra. Box2D is meant to be fairly readable as projects go, so that's another reason not to muck with established flow. I'm not sure most programmers are as happy with priorities as you are, either.

2) Hmm, ok. I'll have to investigate the clone thing. That would kinda suck - Flash doesn't have the performance for events of that frequency. But at least it would mean users don't have to worry about persisting events. Evil thought: you could just make MyEvent.clone return itself... Users who want to keep events should be prepared to call clone manually, this is already necessary in some contact events.

3) Iterators themselves are standard. Just gives a list of bodies, using HasNext and Next, like Java used to. The rest of the model is copied from python: iterators can only be gone through once - if you want multiple iterators (for multiple passes) you need an iterable, which can generate arbitrary amounts of the same iterator, using GetIterator(). I guess it could be GetAnotherIterator(). ResetIterator is just an optimization on that, now I'm learning how vital it is to recycle objects. Check out MyController in TestSensor.as - it shows how using the things is pretty minimal.

But none of that matters to the user. They just need to know that instead of using AddBody/RemoveBody, they can instead construct an iterable corresponding to the set of bodies they want with appropriate arguments, and pass it in to their controller. Which is simple enough, I think, many AS3 frameworks have similar setups. Iterators are only important when implementing controllers, something most users won't do. If I could make protected interfaces, then I would have declared every iterable like that.

4) Linked lists are a Box2D standard. They work fine. polygonal.de seems to suggest they are the fastest, too, when used bare.

Please let me know if you are unsure how proceed on anything. If you cannot fit what you've done into this scheme, I'm probably missing something. I'd think carefully before using touching information instead of contact events. There are subtleties to do with when contacts are evaluated.
Damn.
Just wanted to weigh in that I frequently use all 3 versions of Box2D for projects, and given Apple's recent Adobe hating, will continue to use the C++ version for the iPad port of the games. The level of innovation around Box2D is stunning, and I look forward especially to seeing the various mature (well documented!) layout tools that will eventually become a part of Box2D...

Original thread: https://box2d.org/forum/viewtopic.php?t=4345