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

VengantMjolnir wrote:
Not sure with the WCK port but in the main one you need to enable the motor, and set the max motor torque.

Yep, the motor is enabled but I didn't set the torque but when I make the initial setting of motorSpeed 20 it just works.
Kenny:

I assume "frontjoint" is a wck.Joint object. In that case the "motorSpeed" is only set at creation time. You'll actually have to dig deeper into the actual Box2D object and set the motor speed there like so:

Code:
(_world.frontjoint.b2joint as b2RevoluteJoint).m_motorSpeed = 20;


I've considered implementing the component properties as getters / setters that wrap the Box2D objects after they are created, but never got around to it.
Worked like a charm 🙂 Thanks! Again, this is great. I tried Box2D before but I was annoyed that it was all code based and I couldn't place stuff on the stage for fast and easy testing, this is genius!
I made some first steps with this tonight 😀


Flash Builder AS3 Project:


Main extends WCK

Go to the build path settings and add src/Box2DAS3 as a SWC folder, also add src/ to your build path as i created a fla there.

Under src/ folder create a new assets.fla and make sure its publish settings export a SWC. In this case both assets.swc and Box2D.swc will be available in Flex. Not including the Box2D.swc was why i was getting CLIB errors at compile time.

Flash CS4 Project:

Inside assets.fla create a new MovieClip called TestWorld which a class name of TestWorld whose base class is wck.World. I also dragged over a few other items from the test bed. Box20 and Circle20.

I dragged these items into the TestWorld and used the component inspector to set properties. I also gave important objects instance names such as leftWheel.

Then i publish the swf to generate assets.swc which will be picked up by Flash Builder (project > Clean to make sure).




Back in Flash Builder you can call your world up:

var world:World = new TestWorld()
world.x = stage.stageWidth/2;
world.y = stage.stageHeight/2;
addChild(world)


And it runs fine!.

The next thing i tried to do was add a spring joint and i before i got that far, i had an issue with the ConnectorSpring when specified inside the component inspector. Flex didnt seem to like that, also i cant remember well, but flex didnt like WorldClickAdd either. Something about getDefinitionByName is throwing null references but this is no doubt some issue on my behalf.

As a side note, world.leftWheel returns a Circle20 which i have cast as a Sprite which is probably wrong. This is cool however because i can just use the API in flash to draw lines using the Circle20 co-ordinates instead of using the ConnectorSpring movieclip.

The alarm bell was ringing though because i dont know how to reference the actual box2d object. If Circle20 is a DisplayObject (im not even sure what) then where is all the m_properties for that object?

I think this is my final hurdle because if i can access the box2d side then i can continue on like i was only using flash for layout. I have no idea if i am approaching this correctly however.

Anyways ill keep sticking with it and see what i can do, any advice would be great, though no rush.

Cheers.
clark wrote:
The next thing i tried to do was add a spring joint and i before i got that far, i had an issue with the ConnectorSpring when specified inside the component inspector. Flex didnt seem to like that, also i cant remember well, but flex didnt like WorldClickAdd either. Something about getDefinitionByName is throwing null references but this is no doubt some issue on my behalf.


If at compile time the only reference you have to ConnectorSpring is in the component inspector, then it wont compile that class into the project, since that reference is just a string. When it uses "getDefinitionByName" then, it can't find it. There is a symbol in the demos, "ConnectorSpring" that satisfies this reference requirement.

clark wrote:
The alarm bell was ringing though because i dont know how to reference the actual box2d object. If Circle20 is a DisplayObject (im not even sure what) then where is all the m_properties for that object?


(world.leftWheel as BodyShape).b2body
Thanks Mayo your a Gentleman!

Yeah about getDefinitionByName, it was strange because i added ConnectorSpring to the library with a class name of ConnectorSpring but still got the reference error dispite it being available directly via the SWC. var cs:ConnectorSpring = new ConnectorSpring() but getDefinitionByName("ConnectorSpring") throws me a runtime.

It really does not matter though in the slightest and is merely a error on my behalf no doubt.

Thanks for clearing up the b2Body references.

I might have a go at writing up how to kick things off from a Flex perspective and not from the Flash perspective. I know the Testbed uses the Alchemy port but to benefit from WCK via Flex is a really good thing.
clark, if you are still having issues with getDefinitionByName
try referencing your class before using that function.

Code:
trace("ConnectorSpring: "+ConnectorSpring);
var springDef:Class = getDefinitionByName("ConnectorSpring")
trace("springDef: "+springDef);


if this actually helps, you could take it out of the trace.

Code:
ConnectorSpring
var springDef:Class = getDefinitionByName("ConnectorSpring")


im not into flex my self, but i have experience with getDefinitionByName and swf encryption software causing conflicts.

also you could check if the linkage class is set to export on first frame.
I've been thinking about ways to merge the V2 / b2Vec2 classes together. In case you don't know: b2Vec2 represents a C++ vector, and a V2 represents an AS3 vector. Since b2Vec2 has to read / write from Alchemy memory, there is a bit of overhead compared to the AS3 V2 class. But how much overhead?

Code:
79 milliseconds to get / set a V2 1000000 times
1041 milliseconds to get / set a b2Vec2 1000000 times


Those are the results I found. Clearly, V2 is faster. But, b2Vec2 is still fast. Reading and writing the x property a million times take about a second. Is this "premature optimization?" Would the completely minuscule speed boost be missed if these classes were merged into one to provide a cleaner API? Thoughts?

Another idea I'm pondering is creating a merged vector class with 2 sets of coordinates, <x, y> (read / write to alchemy) and <_x, _y> (cached AS3 side values) with "read" and "write" methods for syncing the two. That way you could use the AS3 variables if you absolutely need that speed boost. I'm not sure if this would be even more awkward though.
Sorry to ask here but couldn't find information in other places...

I downloaded the code and tried to open... pretty much everything. Nothing worked on my computer, from swf to fla and swc. Nothing worked. I'm guessing this is for CS4?

Is there any documentation about how to implement this port as a substitute for Box2DFlash? Or any documentation I might have missed, because I really can't find anything besides the download of the source.

Thanks in advance.
Mayo:
You've miscounted the cost of conversion. If the api uses V2 consistently, and internally you use b2Vec2, that means every getter must constructing a new vector each time, which will badly hurt garbage collection. The alternative of using b2Vec everywhere is impossible, users cannot cope with destroying vectors manually.

I've got a better suggestion for vectors: Double up the interface with accessors for both.

So, e.g. in b2Body

public function get linearVelocity():b2Vec2Ref
{
return m_linearVelocity;
}

public function GetLinearVelocity():b2Vec2
{
return m_linearVelocity.ToVec2();
}

where I've given the object names that are more consistent (with my own code, that is).
This also resolves the problem of users getting confused over which return values are safe to edit (references) and which ones aren't (const references): if it's a Get* method, you cannot, if it is a getter, you can.

I've mentioned this before, I think, but your API and documentation is not really great (sorry). And my speed and up-to-dateness are not. I'm still keen to find some way to join forces.

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