Box2D Forums

It is currently Fri May 24, 2013 10:12 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Sun Jun 24, 2012 4:56 am 
Offline

Joined: Tue May 22, 2012 2:37 am
Posts: 31
Howdy!
Im about to finish my first box2d game but i have a serious problem though. Im trying to add some sound when 2 bodies hit eachother.
The problem is that im using my contact listener and override the functions there. The problem is that the contact listener is based on points so whenever 2 points from 2 bodies are hiting eachother the sound play so if more points form those 2 bodies hit the sound will play over and over and im not sure what to do.
I just want the sound to play once when the bodies collide even if more points from those 2 objects get contact.

Here's my contact listener code:

Code:

override public function Add(point:b2ContactPoint):void
      {
         if(point.shape1.IsSensor() || point.shape2.IsSensor())
         {
            soundChannel1.stop();
         }
         if(point.shape1.GetBody().GetMass()==0 || point.shape2.GetBody().GetMass()==0)
         {
            soundChannel1=soundContact1.play();
            soundChannel1.soundTransform=Volume5;
         }
         if(point.shape1.GetBody().GetMass()>0 && point.shape2.GetBody().GetMass()>0)
         {
            soundChannel1=soundContact3.play();
            soundChannel1.soundTransform=Volume5;
         }
         
         
         
      }
      override public function Persist(point:b2ContactPoint):void
      {
         soundChannel1.stop();
         
         
      }
      override public function Remove(point:b2ContactPoint):void
      {
         soundChannel1.stop();
         
         super.Remove(point);
      }
      override public function Result(point:b2ContactResult):void
      {
         
         
      }



Trying to stop the sound if the contact "Persists" but not geting much effect!
So any suggestion how i should do this? I would appreciate.


Top
 Profile  
 
PostPosted: Sun Jun 24, 2012 1:21 pm 
Offline

Joined: Tue May 22, 2012 2:37 am
Posts: 31
help!
:(


Top
 Profile  
 
PostPosted: Sun Jun 24, 2012 7:34 pm 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 242
This seems more like a general programming question than a Box2D question. x]

Anyway, you'll have to keep track of the collision of both bodies. You can use an array to keep track of collision pairs and a contact count, then check against that array (and the contact count) to see if you need to create a sound.


Top
 Profile  
 
PostPosted: Mon Jun 25, 2012 4:15 am 
Offline

Joined: Tue May 22, 2012 2:37 am
Posts: 31
i have no idea how to do that :(


Top
 Profile  
 
PostPosted: Sat Jun 30, 2012 5:47 am 
Offline

Joined: Tue May 22, 2012 2:37 am
Posts: 31
I have managed to solve it with linearVelocity kinda like Force of Impact.
Dont know if its the greatest way but works for now :D

Code:

if(point.shape1.GetBody().GetLinearVelocity().x>8 || point.shape1.GetBody().GetLinearVelocity().x<-8) // and also on Y axis
{
     playSound
}



This way theres no sound spamming on little impacts.


Top
 Profile  
 
PostPosted: Sat Jun 30, 2012 2:44 pm 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 242
Heh, that's actually better than my suggestion. I was thinking the sound portion was a key game element, not a simple "bump" sound, so I suggested something a little complicated. Using linear velocity to determine if it should make a sound is actually the right approach. -]


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group