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

Hi, i feel rather silly for asking this, but does anyone know the "correct" way of working out the strength of a collision with the new 2.1 collision system?

I want to set the volume of impact sounds based on the strength of impact.

It seems like it should be something very obvious but after searching for ages i cant find an answer.
@PaulGene:
heres how i have done it in 2.1

some of the ways variables are accessed here are probably WCK or Alchemy port specific (but that is what this particular thread is about anyway)

Code:
public override function beginContact(e:ContactEvent):void{
   var impulse:b2ContactImpulse = e.impulses
   var normalLength:Number = Math.sqrt( (impulse.normalImpulse1*impulse.normalImpulse1) + (impulse.normalImpulse2*impulse.normalImpulse2) );
   trace("impulse: "+normalLength);
}


hopefully this helps.
personally i just add normalImpulse1 to normalImpulse2 even though its not accurate, its faster than finding the length of the hypotenuse they create. and it still gives me a range of small to big, to judge impacts on.
Interesting, at the moment i'm doing the following, which seems to give very good results, i have no idea which is more accurate / efficient

I extend b2World (using the Alchemy port)

Code:
      public function BeginContact(c:int, a:b2Fixture, b:b2Fixture, bias:int):void
      {
         var volume:Number = a.m_body.m_linearVelocity.v2.distance(b.m_body.m_linearVelocity.v2) / FlxBox2dG.SOUNDSCALE; // SOUNDSCALE is set to about 60
         if (volume > 0.05)
         {
            play(volume );
         }
      }



I will try out your method.
@sketchbookgames I managed to get your method working without WCK by using the impulse passed in PostSolve like this:

Code:
public function PostSolve(c:int, a:b2Fixture, b:b2Fixture, bias:int, i:int):void
{
   var impulse:b2ContactImpulse = new b2ContactImpulse(i);
   var normalLength:Number = Math.sqrt( (impulse.normalImpulse1*impulse.normalImpulse1) + (impulse.normalImpulse2*impulse.normalImpulse2) );
   trace("impulse: "+normalLength);
}


And it does indeed seem better, so thank you very much! ... i'm not sure how to get it working in begin contact, which would probably be better because it gets called far less than PostSolve.

@mayobutter

First, it would be interesting to know how you tackle the above problem, or even just how i could get sketchbookgames version working in beginContact without the WCK contact system.

Second, think i found some bugs!

I updated to the latest version and got a null object reference error, i noticed b2Base.initialize() missing in the constructor of the new version of b2World, i put that back in and everything seems to work fine now.

Contacts, m_reportBeginContact etc don't seem to work, all the events get called no matter what they are set too, you can see this if you put some traces in the BeginContact / EndContact / PreSolve / PostSolve methods of b2World, i get a stream of PreSolve and BeginContact's even though all reports are turned off. At the moment to make things work properly i am manualy checking the report flags before calling the relevent function.

Not sure if this one is a bug but Weld and Friction joints cant be created using world.CreateJoint(jointDef), you have to call jointDef.create(world) instead, also debug draw causes an error when using Weld joints because of null v2's in DrawSegment()

I changed it to:

Code:
public function DrawSegment(p1:V2, p2:V2):void {
   if ((p1 != null) && (p2 != null))
   {
      graphics.moveTo(p1.x * scale, p1.y * scale);
      graphics.lineTo(p2.x * scale, p2.y * scale);
   }
}
@PaulGene
Quote:
i'm not sure how to get it working in begin contact, which would probably be better because it gets called far less than PostSolve.


if i recall correctly impulse may not be a part of BeginContact.
@MayoButter

There is something i struggled with for a while yesterday.
creating a Joint on contact.

the current plan is to shoot a bullet, when it hits a body, create a revolute joint target1 = contact-body target2 = hero.
-you could think of it as a grappling hook

the following code creates a joint (i use an instance of the library item (the white X's from your demo) so i can see where it is created)
the joint however ends up with target1 = bullet, and target2 = ? hero is not affected.
*this code is in bullet, on BeginContact
Code:
private function onContact(ce:ContactEvent):void{
   var otherShape:BodyShape = ce.other.m_userData
   var otherBody:BodyShape = otherShape.body;
   
   var thisRevJoint = new revJoint;
   thisRevJoint.x = this.x;
   thisRevJoint.y = this.y;
   
   thisRevJoint.target1Object = dev.world;
   thisRevJoint.target2Object = dev.hero;
   
   //////the following commented items cause errors when i try them ///////
   //thisRevJoint.b2body1 = dev.world.b2world.m_groundBody;
   //thisRevJoint.b2body2 = dev.hero.body.b2body;
   //thisRevJoint.createRevoluteJoint();
   //thisRevJoint.type = "Revolute";
   
   dev.world.addChild(thisRevJoint);
}


perhaps I'm missing something obvious about the way joints work?
maybe i would be better off to extend the joint class and override a certain function(s)?
-i will end up doing that anyway, once i get this part working i will be adding some unique functionality to the Joint.
@MayoButter

i made another change to wck locally. and im curious if you would find it a valuable improvement for wck.

you have: applyGravity on BodyShape.
and on world
Code:
if(!b2 || b2.applyGravity) {
   b.m_linearVelocity.x += timeStep * g.x;
   b.m_linearVelocity.y += timeStep * g.y;
}


i added: modifyGravity on BodyShape
and on world
Code:
if(!b2 || b2.modifyGravity!=0) {
   b.m_linearVelocity.x += timeStep * g.x * b2.modifyGravity;
   b.m_linearVelocity.y += timeStep * g.y * b2.modifyGravity;
}


allowing specific bodies to have different intensities of gravity.
i added it to make my flame sensor bodies accelerate upward -0.20 gravity. (from a flame thrower)
just using linearVelocityY looked bad, but gradual acceleration looked good, i realize there are other ways to do it, however i think things that rise up like fire and balloons would always be the opposite direction of gravity. and there are probably many applications for various gravity values... imitating wind resistance on items etc... and it does't seem like a change that adds much (if any) weight.

to be backwards compatable, you could make a setter for applyGravity, when set to false, modifyGravity gets set to 0.

looking forward to your opinion either way.
Re: grappling hook.

today i extended Joint and overrode create, its a bit cleaner this way. however the errors would still flow in when created onContact, but not when existing on the stage. i discovered that adding a 1 frame delay before calling my create function got rid of the errors. so this appears to be one of the few lingering cases where wck doesn't automatically wait for world to be unlocked.

perhaps world on add-child it should queue, and wait to create until next enter frame?
wck raycast quesiton here:
viewtopic.php?f=8&t=4973&p=23470#p23470

has attached simple TestBed example of the problem.
I'm getting very odd behavior, any ideas why?

1. Open demo.fla
2. Goto to frame 2, move, for example, world14 by 1 pixel left (everything will do, it is just to make sure * appears and file is saved as new).
3. Compile and run. All runs perfectly! But,
4. Close demo.fla, open demo.fla again. Compile, run, goto frame 2 - BoxMan is not moving and is not affected by gravity.

I tried many different approaches. To summarize:

After changing something in demo.fla, and re-opening file, something gets corrupted?

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