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

mayobutter wrote:
Tiblanc: Windows with Cygwin? Yikes, not familiar with that environment. I'm compiling on OS X myself. Had to play around with the command line flags sent to g++ a lot to get it to work. I'd recommend poking around in the Alchemy documentation or playing around with the flags. For example the Alchemy docs recommend compiling like this:

gcc stringecho.c -O3 -Wall -swc -o stringecho.swc

But I ended up with this:

g++ Box2D.c -I.. -O3 -Wall -swc -DOSX -o Box2D.swc

So maybe try dropping the -DOSX flag?


Dropping -DOSX worked. Thanks!

Do you know if there's a way to profile the step call? Performance isn't quite where I'd like it and before I tone down the game, I'd like to know where that bottleneck is to see if I can speed things up.
Tiblanc wrote:
Dropping -DOSX worked. Thanks!

Good to know. I added that to the wiki.
Tiblanc wrote:
Do you know if there's a way to profile the step call? Performance isn't quite where I'd like it and before I tone down the game, I'd like to know where that bottleneck is to see if I can speed things up.

You could put a timer around the b2World step call and see how long it's taking. Rendering could be the bottleneck as well - Flash performance quickly degrades with fancy graphics. I stick to simple flash shapes w/ bitmap fills and that seems to work well.
mayobutter wrote:
You could put a timer around the b2World step call and see how long it's taking. Rendering could be the bottleneck as well - Flash performance quickly degrades with fancy graphics. I stick to simple flash shapes w/ bitmap fills and that seems to work well.


That was actually what I did 😛 I wanted to see how long step() would take with over 500 shapes all moving at the same time before working on the rendering. At the moment, it's somewhere around 20ms per step() call. I used to profile Box2D with the C++ version of the game and find some tweaks, but I'm at a loss with the Alchemy port. I'm guessing it's a Flash limitation since the same scenario in C++ would take 3~4 ms, but I was still curious.
Tiblanc wrote:
That was actually what I did 😛 I wanted to see how long step() would take with over 500 shapes all moving at the same time before working on the rendering. At the moment, it's somewhere around 20ms per step() call. I used to profile Box2D with the C++ version of the game and find some tweaks, but I'm at a loss with the Alchemy port. I'm guessing it's a Flash limitation since the same scenario in C++ would take 3~4 ms, but I was still curious.


Yeah, the C++ code natively compiled will always be faster.
[quote="mayobutter"]sketchbook: b2fixtures is a property of BodyShape that contains all the fixtures / shapes defined by that BodyShape (via calls to box(), circle(), etc.) So if your body defines no shapes itself, that array will be empty. Your child BodyShape will have the fixture / shape in its b2fixtures array.

my class extends BodyShape

it seems as if all the shape attributes fail to change at run time.

restitution=1; //no change
b2body.m_fixtureList.SetRestitution(1) //correct change

most of my bodies only have 1 shape so i didn't need to go through the whole array to test.

also
tracing b2fixtures seems to be blank.
Code:
public var b2fixtures:Vector.<b2Fixture> = null; /// An array of b2Fixtures - Get the shape by fixture.m_shape.

I'm not familiar with the new Vector class or the use of .<b2Fixture> for data typing.

the specific body I'm testing is a wck.BodyShape with a shapes.Box inside of it.

If this is still not productive, i will make sure to grab the latest version and create a simple project with one 4 walls and one box to and try to change attributes at run time and send you a test of it if it still doesn't work for me.

it would be nice to figure out for my current project. but is more important for my next project. thanks. I hope this report is more clear than my last report.
sketchbookgames wrote:
the specific body I'm testing is a wck.BodyShape with a shapes.Box inside of it.


This is exactly what I mean. The parent "wck.BodyShape" has a b2fixtures property (empty), and the "shapes.Box" also has a b2fixtures property (one shape). So if you set "restitution = ..." on your child Box it should work. It's not intended to.

I just want to save you some coding time by pointing out this is exactly what's expected. I think maybe having "guide" shapes add their fixtures to their parent's list might be a good solution in the future though.
I've been digging in the source code and was wondering if there was a reason why operations on bodies were done in AS3 using memory access to C++? The issue I have is I apply force and torque every step to my bodies. That causes memory get/set which seems to be the bottleneck. I have no experience with Alchemy, but would there be a way to call the C++ functions instead to bypass AS3<->C++ memory copying and benefit from C++ speed?

Also, I noticed there was a lot of new V2 objects being created in the add, substract, etc functions. That's not as bad as the memory access as far as performance is concerned, but some functions can create a lot of V2 objects because they chain these operations. I added a temp V2 parameter in substract to remove the new which seemed to help the applyForce calls a bit(about 5~10%).
Tiblanc wrote:
I've been digging in the source code and was wondering if there was a reason why operations on bodies were done in AS3 using memory access to C++? The issue I have is I apply force and torque every step to my bodies. That causes memory get/set which seems to be the bottleneck. I have no experience with Alchemy, but would there be a way to call the C++ functions instead to bypass AS3<->C++ memory copying and benefit from C++ speed?


I just tried exporting the C++ ApplyForce function so it could be called directly from AS3. Here's the results:

Code:
96 milliseconds to call ApplyForce() in AS3 10000 times
777 milliseconds to call ApplyForce() in C++ 10000 times
12 milliseconds to directly set m_force 10000 times


The small overhead of any memory reading / writing is nothing compared to the overhead of calling a C++ function from AS3. That is exactly why many of the simple methods are implemented in AS3.

There is still hope though! Notice the third test. This was done directly setting the force via:

Code:
var m_force = body.m_force;
m_force.x = 123; m_force.y = 123;


So you may be able to optimize by directly setting the force rather than calling the ApplyForce function.

Quote:
Also, I noticed there was a lot of new V2 objects being created in the add, substract, etc functions. That's not as bad as the memory access as far as performance is concerned, but some functions can create a lot of V2 objects because they chain these operations. I added a temp V2 parameter in substract to remove the new which seemed to help the applyForce calls a bit(about 5~10%).


Some methods could be improved by inlining things. It's really hard to determine where this would be worth the effort though.
mayobutter wrote:
Tiblanc wrote:
I've been digging in the source code and was wondering if there was a reason why operations on bodies were done in AS3 using memory access to C++? The issue I have is I apply force and torque every step to my bodies. That causes memory get/set which seems to be the bottleneck. I have no experience with Alchemy, but would there be a way to call the C++ functions instead to bypass AS3<->C++ memory copying and benefit from C++ speed?


I just tried exporting the C++ ApplyForce function so it could be called directly from AS3. Here's the results:

Code:
96 milliseconds to call ApplyForce() in AS3 10000 times
777 milliseconds to call ApplyForce() in C++ 10000 times
12 milliseconds to directly set m_force 10000 times


The small overhead of any memory reading / writing is nothing compared to the overhead of calling a C++ function from AS3. That is exactly why many of the simple methods are implemented in AS3.

There is still hope though! Notice the third test. This was done directly setting the force via:

Code:
var m_force = body.m_force;
m_force.x = 123; m_force.y = 123;


So you may be able to optimize by directly setting the force rather than calling the ApplyForce function.


Thanks for testing it out! These are interesting results though. I figured alchemy would compile into optimized AS3 code which would be faster with no overhead, but I guess I was wrong. I'll have to try setting the forces directly. Since they are always at the center of mass, it should speed things up without any issue.

I wonder if there would be a way to batch these calls. Creating a applyForces function which would take bodies and their forces and update them all in 1 call. That might get the speed of C++ without the overhead...
Alchemy port and WCK are really great.
But there is one big problem that keeps this API from being used by programmers.
At this moment wck is binded directly to API and not it a nice way. That means if I just want to create some arc shape, I can't really do it without WCK.

I think it would be best to divide alchemy port from wck. Partially it is like that, but most util classes (like mentioned arc shapes) are binded to WCK. WCK should use util classes that are inside alchemy port. That way programmer without flash IDE could still benefit from these util classes.

Simply said: allow programmers without IDE to use all nice features like arc shapes easily. (now it seems I would need to copy and alter functions from WCK classes to do this)

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