How do i detect speed?

I want to detect the speed at which a ball hits a wall in order to play a different sound depending on the speed it hits. So for instance if it hits the wall hard/fast then i want a sound to be played. But if the ball hits the wall at a slow speed then i want a different sound to be played. For movement the ball uses the accelerometer.

Can this be done? 😕

I'm using the BoxMan class, so i suppose it would be something like:
Code:
var w:Wall = e.other.m_userData as Wall;
         if(w) {
         //code here to detect speed at which the ball hits the wall.

         var snd:PlaySound = new PlaySound();  //this part plays the sound
         snd.play();
         }
Use object.b2body.GetLinearVelocity().x and b2body.GetLinearVelocity().y

I'm not sure off the capitalization, could be getLinearVelocity()
I think he wants to judge how much 'impact' the hit caused. If the ball hits the wall at a shallow angle, it will have less impact than if it hits perpendicularly into the wall at the same speed, so simply using the velocity is only a rough guess without more information.

Another way of thinking of 'impact' is how large an impulse needs to be applied to the two bodies to make them bounce apart. According to the manual you can get this info in the PostSolve function of a contact listener.

But having said that, it's quite likely that the using just the velocity is good enough for most cases 🙂
Thanks guys, and yes i want to detect how much impact the hit causes. Because a different sound needs to be played depending on the level of impact, just like if you was to throw a ball at a wall in real life you would hear a loader sound the harder the impact is.

How can this be implemented into the code i originally posted? I'm not that great with AS3 so i've no idea how i would use something like getLinearVelocity, but if someone could show me how to detect a high impact and then play a sound if the impact is high enough, then i should be able to use that and go from there.
You have to listen for the ContactEvent.POST_SOLVE contact event. This contact event is unique in that it also sets the "impulses" property on the event. You can use this to determine how hard the contact hit.
Mayo, can it be used within the BoxMan class? I feel like i've already asked too much from you but would you be able to give a code example of how i could implement it in that class? My AS3 isn't great which is why i'm also using WCK because it makes things so much easier.
Ok i think i've found what i want to do but it's usings the AS3 Box2D port, so it wont work with WCK, but heres the code:

Code:
   {
      // Should a sound be played?
      var count:int = contact.GetManifold().m_pointCount;
     
      var maxImpulse:Number = 0.0;
      for (var i:int = 0; i < count; i++)
      {
         maxImpulse = b2Math.Max(maxImpulse, impulse.normalImpulses[i]);
      }
      if (maxImpulse > 50)
      {
         var snd:PlaySound = new PlaySound();  // This plays the sound
         snd.play();
      }
   }

Can someone please convert that to work with WCK? I dont know how to use WCK's ContactEvent.POST_SOLVE as Mayo suggests.
Anyone? or any suggestions? 🙁
In your create function just make sure to set this:

reportPostSolve = true;

That will collect the impact data and attach it to the contact event. Then in your update / control loop where you pick apart the contact events, you should be able to read the "impulses" property of the ContactEvent (your e variable). So this should give you a pretty good estimate of how strong the hit is:

e.impulses.normalImpulse1 + e.impulses.normalImpulse2;
Mayo, i've done what you say above and is this looking correct so far?

Code:
public override function create():void {
         reportPostSolve = true;
         reportBeginContact = true;
         reportEndContact = true;
         fixedRotation = true;
         super.create();
         listenWhileVisible(world, StepEvent.STEP, parseInput, false, 10);
         listenWhileVisible(this, ContactEvent.BEGIN_CONTACT, handleContact);
         contacts = new ContactList();
         contacts.listenTo(this);
      }
      
      public function handleContact(e:ContactEvent):void {
         var w:Wall = e.other.m_userData as Wall;
                if(w) {
            e.impulses.normalImpulse1 + e.impulses.normalImpulse2;
            var snd:SoundA = new SoundA(); // plays the sound
            snd.play();
         }

Now i think i need to add some code after e.impulses.normalImpulse1 + e.impulses.normalImpulse2; to play SoundA for a slow impact, or play SoundB for a high impact. No idea how to do this though?...

Or am i doing it all wrong?

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