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

I just implemented b2Body::GetContactList(). There's a simple example in the HelloWorld project that outputs the contact normals. Not exactly a thorough test... so proceed with caution.

The contact callback system is a better way to get at contact data, but it works a bit different than the C++ version:
1. Rather than extend b2ContactListener, just extend b2World - all the contact callbacks have been rolled up into the world class.
2. You have to set reportBeginContact, reportEndContact, etc. on the fixtures you are interested in. This is because there's some overhead in making the callback from C++ to AS3, so you don't want it to fire for every single fixture.
3. If you use WCK, the contact callbacks fire AS3 native events off of the BodyShape that you can listen for via addEventListener

Sensors work just like they do in the C++ version. The pinball demo actually uses sensors to teleport the ball back to the beginning when it falls off the screen.

Let me know what you need next 🙂 otherwise I'm off to buoyancy.
Hmm... seems like something is off. In Hello.as if you switch 'lastBody' to 'kine' in the for loop, you only get a handful of contacts traced, instead of the ~hundreds I think it should get as it moves through that field of static and dynamic circles. Also in my project I am trying to get a list of bodies within a box shaped sensor, and it's only ever returning at most one contact (contact.next is always null). Not sure if these are related problems or not.

EDIT: Ran it again a few times, and twice I actually only got ONE contact on 'kine' even as I watched it collide with many static and dynamic circles, so something is definitely going wrong 🙁

EDIT2: OK now I've realized you are limiting the traced callbacks in relation to their normals. Tracing all of them gives the number I would have expected. Still have to see what is going on with my sensor... probably a mistake on my part. I'll keep working on it.
Just had a play with your demo's and this is really stunning work MayoButter. I'm intrigued by the Alchemy + Box2D combination. Do you feel that using Alchemy makes a significant speed improvement to Box2D?
shock-value: You are correct, "next" was not functioning properly. I'm uploading some patched code right now. If you don't want too re-download, just change line 43 of b2ContactEdge to:

Code:
var p:int = mem._mr32(_ptr + 12);


Just keep in mind the contact events are much more accurate - contacts can begin and end within a timestep and then wont be in the contact list.

Also... it sounds like you're working on some code for drawing shape primitives. Care to share? This could really use a debug drawer. The built in C++ one is unworkable.

andrew2110: It is definitely faster. I've been able to double the complexity of my game levels after building this port. I think BorisTheBrave is working on a native port of version 2.1 though - he might have some tricks up his sleeve.
Well here's the section that draws a sprite, given a body...

Code:
var body:b2Body
var scale:Number
var sprite:Sprite = new Sprite()

var fixture:b2Fixture = body.m_fixtureList
var type:int = fixture.GetType();
while (fixture != null) {
   if (type == b2Shape.e_polygon) {
      var pshape:b2PolygonShape = fixture.m_shape as b2PolygonShape
      var numvertices:int = pshape.GetVertexCount()
      var vertex:V2 = pshape.GetVertex(0)
      var overtex:V2 = vertex;
      sprite.graphics.lineStyle(1,0xFFFFFF)
      sprite.graphics.moveTo(vertex.x*scale,vertex.y*scale)
      sprite.graphics.beginFill(0xA0A0A0)
      for (var i:int = 1; i < numvertices;i++){
         vertex = pshape.GetVertex(i)
         sprite.graphics.lineTo(vertex.x*scale,vertex.y*scale)
      }
      sprite.graphics.lineTo(overtex.x*scale, overtex.y*scale)
      sprite.graphics.endFill()
   } else if (type == b2Shape.e_circle) {
      sprite.graphics.lineStyle(1,0xFFFFFF)
      sprite.graphics.beginFill(0xA0A0A0)
      sprite.graphics.drawCircle(0,0,fixture.m_shape.m_radius*scale)
      sprite.graphics.endFill()
   }
   fixture = fixture.m_next
}


And to update the associated sprite based on position...

Code:
var body:b2Body
var scale:Number
var sprite:Sprite

var pos:v2 = body.GetPosition()
sprite.x = pos.x*scale
sprite.y = pos.y*scale
sprite.rotation = body.GetAngle() * 57.2957795130


Not sure how you want to integrate it into a debug draw system--if you give me more specifics on how you'd like it to work I can try to work something up later this week. Or if you want to put it into place yourself if that's easier, that's fine.

EDIT: Fixed code
I'm finally getting around to giving this a try for myself. From the Demo it looks you have created Component in the library for pretty much everything. I'm starting with a World.

I added the Class path to my AS Settings.

Then I created a clip in the library to act as a world and set it's class to: extras.WorldClickAdd

In your example you have created parameters that were set via the Component Inspector. Do I need to add all of these manually. or I am I missing something?
After playing around for a few minutes I'm getting the following error:

VerifyError: Error #1014: Class cmodule.Box2D::CLibInit could not be found.

at global$init()
at global$init()
at wck::World/create()
at extras::WorldClickAdd/create()
at misc::Entity/ensureCreated()
at MethodInfo-1034()

Sounds to me like I might need something to get the Box2d C library thing working. The demo file seemed to work on my computer. I placed my test fla in the same folder with Demo.fla. So I figured it would find any necessary classes...
Btw, and you might already be aware of this, but you might want to ensure that all of the setters have an explicit return type of void. Kind of pointless, I know, but the Flex compiler complains when it's on strict mode (still lets you compile after re-running, but you get warnings the first time, at least using FlashDevelop). Looks like you are autogenerating from Box2D.c somehow in the probe folder like you mentioned so should be an easy fix, at least for generation (then they'll have to be integrated into the code, of course). There are also a few stray methods without return types, but not more than a handful (if your compiler isn't telling you I can post the list, just let me know--don't want you to have to go looking for them yourself).
I realized I was missing the Document Class. So I copied the Demo class. Now I'm getting this error:

VerifyError: Error #1014: Class cmodule.Box2D::CLibInit could not be found.

at wck::WCK()
at Test_01()

I think this must have something to do with Alchemy and the Box2d C Library. I haven't used Alchemy before so I'm not sure what to do to get it started. Have the Fla in the same folder as Demo and I linked to the same AS class path.

Just as I typed this I remember to check the Library Path and External Library Path in the AS Settings. I copied the path from the Demo fla.

Now I get the following, but I figure this is looking like I don't quite have things set up correctly, rather than the class library not operating properly.

ReferenceError: Error #1065: Variable WorldContainer is not defined.

ReferenceError: Error #1065: Variable Test_01 is not defined.
soggybag:

Make sure "Export SWC" is checked in "Publish Settings". Not sure why Flash requires this but for some reason it wont work if it is unchecked.

Now open the "ActionScript 3.0 Settings" window. Under "Source Path" make sure you've got a path that points to the "WCK" folder. Also under "Library Path" make sure you have a path that points to "Box2D.swc". In the demo file this path is:

../Box2DAS/Box2D.swc

To add the component properties, just right click one of your symbols in the library and select "Component Definition". Enter the appropriate package / class (wck.BodyShape, wck.World, wck.Joint, etc.). It should pick up the properties from the classes automatically.

shock-value: Can you PM me the warnings? Flash doesn't care about them, apparently. Thanks for the basic drawing code too.

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