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

BorisTheBrave wrote:
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.

That is a damn good point. I think I'll go with the cached AS3 values route, so that...
Code:
b2body.m_linearVelocity.v2 = b2body.m_linearVelocity.v2.add(someV2);

will become...
Code:
b2body.m_linearVelocity.read().add(someV2).write();

Using proper getters / setters will remain the same...
Code:
b2body.SetLinearVelocity(b2body.GetLinearVelocity().add(someV2));

All fancy vector operations (normalize, dot products, etc.) will take place AS3 side. read() and write() will sync the two.

Vlad: Yes, CS4, Flash 10 only. http://wiki.github.com/jesses/wck/
Hey this is a bit off topic but I thought I'd share the beta version of the project I'm working on which utilizes this box2d port, now that I have released it. I have seen really good performance so far, at least in my estimation.

http://shock-value.deviantart.com/art/O ... -156120812
Wow! This is crazy good.

I've been working on a Flash game for the past four months using Boris's Box2D port. I really appreciate all the hard work Boris has put into his port. However, after seeing this I'm thinking about making the switch. Has anyone else made the switch? If so, how drastic is the performance increase? I probably won't end up using WCK because I've already built an AIR based level editor. I'm more interested in the C++ library.

Great job on this! You just made my day.
OK I'm having some trouble getting this working. The demo works fine, but I can't seem to get anything working in a fresh custom file using Flash IDE CS4.

This is what I've done (I'll be very explicit):

  • Open Flash CS4. Good start I think.
  • File-> New -> Flash File (Actionscript 3.0)
  • File -> Save As
  • Create new folder for project
  • Save .fla file
  • (In Windows Explorer)
    Create a new folder in project folder called wck-lib
  • Go to my cloned wck repo and copy the following folders into wck-lib:

    • Box2D
    • Box2DAS
    • extras
    • gravity
    • misc
    • shapes
    • wck
  • Switch back to Flash IDE
  • File -> Publish Settings -> Flash
  • Check Export SWC
  • Actionscript 3.0 Settings
  • Add ./wck-lib to Source path
  • Document class: wck.WCK
  • Check definition found for the wck.WCK class (Success!)
  • Add ./wck-lib/Box2DAS/Box2D.swc to Library path.
  • Click OK to close AS3 Settings dialog
  • Click OK Publish Settings dialog
  • Select Rectangle Tool
  • Draw a rectangle on the stage
  • Select the rectangle we just drew
  • Convert to symbol (F8)
  • Change Symbol name to MyRectangle
  • Check Export for ActionScript
  • Change Base class to shapes.Box
  • Check definition found for the shapes.Box class (Success!)
  • Click Ok to close Convert to Symbol dialog
  • Close warning for no class found on MyRectangle
  • Save
  • Test Movie

Square appears, nothing happens and clicking on it produces:

Quote:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at wck::BodyShape/handleDragStart()


What am I doing wrong?!

I'm running Flash player 10.1 on Windows 7 with Flash CS4 10.0.2
You need to create a world. Create a symbol that extends wck.World and then drop your rectangle in that. Then drop your world on the stage.

Thanks for being explicit 😉
Thanks for quick reply.

Awesome. Gravity working. Getting the hang of this. For some reason I had assumed that the WCK class automatically created a world that fit the stage or something.

Perhaps it should throw some "hey wtf, you need to create a world" exception, cause the null pointer error is somewhat cryptic due to no line numbers or anything.

Also it should be pointed out that the resulting body definition does not take into consideration x/y offset of your drawn graphic. Your graphic must be perfectly centred for the physics body to actually fit over the same shape.
Step-by-step WCK Quickstart in Flash CS4 for noobs: Create 4 Walls and a Box

Setting up the Project: Creating the files
  • Open Flash CS4.
  • File-> New -> Flash File (Actionscript 3.0)
  • File -> Save As
  • Create new folder for project
  • Save .fla file
  • (In Windows Explorer)
    Create a new folder in project folder called wck-lib
  • copy the following WCK folders into wck-lib:

    • Box2D
    • Box2DAS
    • extras
    • gravity
    • misc
    • shapes
    • wck

Setting up the Project: Project Settings
  • Switch back to Flash IDE
  • File -> Publish Settings -> Flash
  • Check Export SWC
  • Actionscript 3.0 Settings
  • Add ./wck-lib to the Source path
  • Document class: wck.WCK
  • Check definition found for the wck.WCK class
  • Add ./wck-lib/Box2DAS/Box2D.swc to Library path.
  • Click OK to close AS3 Settings dialog
  • Click OK Publish Settings dialog

Creating our World: Creating a Physics-Affected Box
  • Select Rectangle Tool
  • Draw a rectangle on the stage (approx. 100px by 100px)
  • Select the rectangle we just drew
  • Convert to symbol (F8)
  • Change Symbol name to MyRectangle
  • Change registration to centre (Important for physics to be generated correctly)
  • Check Export for ActionScript
  • Change Base class to shapes.Box
  • Check definition found for the shapes.Box class (Success!)
  • Click Ok to close Convert to Symbol dialog
  • Close warning for no class found on MyRectangle
  • Right Click on MyRectangle in the library and select 'Component Definition'
  • Change class to wck.BodyShape
  • Click OK to close Component Definition dialog

Creating our World: Creating the World Instance
  • Select the MyRectangle instance on the stage (the one we drew)
  • Convert to symbol (F8)
  • Change Symbol name to MyWorld
  • Ensure Registration is centered
  • Check Export for ActionScript
  • Change Base class to wck.World
  • Check definition found for the shapes.Box class (Success!)
  • Click Ok to close Convert to Symbol dialog

Creating our World: Creating 4 Walls and a Box
  • Edit our new MyWorld instance by double clicking on it.
  • We're going to make the world boundary now. Resize the MyRectangle to act as a floor, eg it's as wide as the stage, and 40px tall & move to the bottom of the stage.
  • Open Component Inspector (Shift-F7)
  • Change 'Type' to 'Static' (right down the bottom of the list)
  • Copy your floor to create walls and a ceiling now, doesn't matter if they overlap.
  • Drag a new 'MyRectangle' from the Library to the stage

Experiencing our World:
  • Save
  • Test Movie

And if everything went AOK, you should have a draggable, physics-affected box inside 4 walls!

🙂
secoif: Awesome write up! I replaced my poor quick start guide on the wiki with yours: http://wiki.github.com/jesses/wck/
For the record, you can turn on debug output in order to get line numbers when errors pop up. Its under publish settings->flash->permit debugging.
Its a great right up, very clear and concise.
Thanks guys.

Also, I'm blown away by this tool, it's amazing. You get 200 Points. Just needs some more docs/tutorials and the classes packaged up more, um, semantically. Eg you have a 'misc' package 😉

@VengantMjolnir Ahh that's right! I haven't worked in the Flash IDE for a few years so memory a bit hazy.

Anyway, love this, will be working with it a lot methinks, I just recreated a week of work screwing about manually typing in box2d coordinates for joints and polys, in just a few hours. Very pleased. 😉

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