AS3 2.1 Port (Alchemy), WCK Update and Kinematic Demos

dougk16 wrote:
However, I was wondering how the ActionScript interface classes are handled. Seeing as how Alchemy only makes the .swc, I assume those are written by hand. But stuff like "public function get m_flags():int { return mem._mru16(_ptr + 4); }" seems like it would be horribly tedious and error-prone to do manually. Is there a way that you have this code auto-generate?


Yes, they are generated automatically. Check out the alternate alchemy project in the "probe" folder:

https://github.com/jesses/wck/blob/master/probe/Box2D.c

It uses the "offsetof" C++ macro to figure out the memory offsets of variables in class instances, then uses that to generate getters / setters. Running the project, this is the output (in the Flash console):

https://github.com/jesses/wck/raw/maste ... output.txt

Not all of the getters / setters should be used verbatim though. For example, look at how b2Contact finds the AS3 fixture objects:

Code:
/// Address of b2Fixture + userData offset -> deref to AS3 = AS3 b2Fixture.
m_fixtureA = fA ? fA : deref(mem._mr32(mem._mr32(_ptr + 48) + 44)) as b2Fixture;
m_fixtureB = fB ? fB : deref(mem._mr32(mem._mr32(_ptr + 52) + 44)) as b2Fixture;


So it gets hairy in places.
AdamAdept wrote:
I tried it that way at first, however I have situations where a body needs to change solidity in the middle of contact. Right now I change the contact mask, then just flag the body involved for refiltering and the contact will automatically add or remove. It would be an improvement however, if I could set the contact to act as a sensor, so I could still reference it but it wouldn't collide, and later set it solid again. Is there a way to do that? And if you disable the contact, can you still get the "remove contact" event?


Check out how the "extras/Platform.as" class disables the contact for one-way platforms:

https://github.com/jesses/wck/blob/mast ... latform.as

So that's how you could ignore / disable a contact before it has any effect. Then after the timestep you could just turn it into a sensor or whatever. Not sure exactly what you're trying to make.

AdamAdept wrote:
I know, and if the step ran in under 1ms, and I was running 2 steps in a row, it would use the cached manifold from the previous step 🙂


Wow, you're right. Will change this.
AdamAdept wrote:
OK, I made an optimized version of QueryPoint which very rarely needs to callback to AS3 (in my use cases anyway). I do this by allowing exclusion of a body and filtering by category:With this code, most of the time I don't get a single callback to AS3. And the result? QueryPoint is still my bottleneck. So, I guess QueryPoint is just slow and the AS3 callback wasn't a big factor. Well that was fun.


Why are you calling QueryPoint so much? I think your original idea of pushing them to an array and returning all results at once might work but seriously, why do you need to call it so much?
mayobutter wrote:
dougk16 wrote:
However, I was wondering how the ActionScript interface classes are handled. Seeing as how Alchemy only makes the .swc, I assume those are written by hand. But stuff like "public function get m_flags():int { return mem._mru16(_ptr + 4); }" seems like it would be horribly tedious and error-prone to do manually. Is there a way that you have this code auto-generate?


Yes, they are generated automatically. Check out the alternate alchemy project in the "probe" folder:


Ah ok, slick stuff. So if I have it straight, you have to make those ActionScript classes by hand (which isn't really a big deal cause you only have to do it once), and then copy-paste the getters and setters into the appropriate .as files? Or I guess you could have some script inject them.
mayobutter wrote:
AdamAdept wrote:
I tried it that way at first, however I have situations where a body needs to change solidity in the middle of contact. Right now I change the contact mask, then just flag the body involved for refiltering and the contact will automatically add or remove. It would be an improvement however, if I could set the contact to act as a sensor, so I could still reference it but it wouldn't collide, and later set it solid again. Is there a way to do that? And if you disable the contact, can you still get the "remove contact" event?


Check out how the "extras/Platform.as" class disables the contact for one-way platforms:

https://github.com/jesses/wck/blob/mast ... latform.as

So that's how you could ignore / disable a contact before it has any effect. Then after the timestep you could just turn it into a sensor or whatever. Not sure exactly what you're trying to make.


Thanks, I'll look into doing this. I just need to get an end contact event, even when a contact is disabled. If that works, I should be able to ditch the filter.

mayobutter wrote:
AdamAdept wrote:
OK, I made an optimized version of QueryPoint which very rarely needs to callback to AS3 (in my use cases anyway). I do this by allowing exclusion of a body and filtering by category:With this code, most of the time I don't get a single callback to AS3. And the result? QueryPoint is still my bottleneck. So, I guess QueryPoint is just slow and the AS3 callback wasn't a big factor. Well that was fun.


Why are you calling QueryPoint so much? I think your original idea of pushing them to an array and returning all results at once might work but seriously, why do you need to call it so much?


I have a world with 30+ moving boxes, that can be conveyed up or down moving ladders. The ladders may have a thin cover over the top, in which case the box needs to see past the cover, to whether there is a down-ladder under it to grab. In this case there's no contact available, and I don't want to put a sensor sticking out the top of every ladder because there could be 100+ ladders in the world. So I was doing a QueryPoint just underneath each box, every step. I didn't realize that call was crazy-slow. I'm seeing 0.56ms per call in a production build, and that's using my optimized version! On one level, the entire box2d.step is only 2.48ms, so 5 QueryPoint()'s is equal to one box2d step. Crazy.

While we're on the subject, setting up a new b2Body is about 0.65ms, which is my next bottleneck. I'm trying to generate mass amounts of smoke that spawns/destroys very quickly, and uses a body to flow around objects, only the body creation is killing me. I really wish b2Body's were true objects that could be reused/created independent of a world. I think this is a limitation of the C version though.
AdamAdept wrote:
On one level, the entire box2d.step is only 2.48ms, so 5 QueryPoint()'s is equal to one box2d step.


You could avoid Box2D entirely, and use getObjectsUnderPoint to look for a ladder (unless you're not using display objects).

Another option is to rewrite QueryPoint entirely on the AS3 side, but I don't know how deep that function reaches into internal stuff.

AdamAdept wrote:
While we're on the subject, setting up a new b2Body is about 0.65ms, which is my next bottleneck. I'm trying to generate mass amounts of smoke that spawns/destroys very quickly, and uses a body to flow around objects, only the body creation is killing me. I really wish b2Body's were true objects that could be reused/created independent of a world. I think this is a limitation of the C version though.


You could have a pool of reusable b2Bodies just set to disabled = true then when the time comes to use them, set disabled = false, and reposition.
HEY!

If you're got a WCK / Alchemy port question, post it as a new topic and indicate in the subject if it is Alchemy Port/WCK related. This thread is getting really hard to browse. Thanks!
DEPRECATED (made a new topic instead..)

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