Physics and sounds and effects

razer
Posts: 82
Joined: Sun Apr 04, 2010 10:08 pm

Physics and sounds and effects

Post by razer »

When bodies bounce in game some audio effect must be played and visual effects like sparks must be generated.

What is the best way to do that?

I have defined and registered

bool myContactAddedCallback(btManifoldPoint& cp, const btCollisionObject* colObj0,int partId0,int index0,const btCollisionObject* colObj1,int partId1,int index1){
}
chrozz
Posts: 8
Joined: Fri Mar 12, 2010 10:21 pm

Re: Physics and sounds and effects

Post by chrozz »

Hey razer,
I hope I can help you, sound effects sure are important to a game. To do this you have to set the

Code: Select all

someBody->setCollisionFlags(someBody->getCollisionFlags() |
								 btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
	someOtherBody->setCollisionFlags(someOtherBody->getCollisionFlags() |
								 btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
	gContactProcessedCallback = (ContactProcessedCallback)HandleContacts;
and in your implementation file declar the extern bullet funct

Code: Select all

extern ContactProcessedCallback gContactProcessedCallback;
In your class implement the ContactProcessedCallback as a static private c++ function

Code: Select all

		static bool HandleContacts(btManifoldPoint& point, btCollisionObject* body0, btCollisionObject* body1);
For which an implementation could vary. The bad thing about this ContactProcessed callback is that bullet seems to call it a random number of times for each collision... not good for sound effects or special effects. So Here is a gift, HandleContacts that enters the collision handling only if enough time has passed between calls.

Code: Select all

static struct timeval start, end;
bool veryFirst = true;
bool first = true;
bool CShell::HandleContacts(btManifoldPoint& point, btCollisionObject* body0, btCollisionObject* body1) {
	btRigidBody* rigidbody0 = dynamic_cast<btRigidBody*>(body0);
	btRigidBody* rigidbody1 = dynamic_cast<btRigidBody*>(body1);
	
	
	float timeThreshold = 350;
	
	float passedTime = 0;
	if ( first){
		first = false;
		if (veryFirst){
			veryFirst = false;
			passedTime = timeThreshold;
		}
		gettimeofday(&start, NULL);
	}
	else {
		long mtime, seconds, useconds;  
		gettimeofday(&end, NULL);
		seconds  = end.tv_sec  - start.tv_sec;
		useconds = end.tv_usec - start.tv_usec;
		
		mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
		passedTime = mtime;
		
		if (passedTime >= timeThreshold)
			first = true;
	}
	// ignore calls until enough time has passed. 
	if (passedTime >= timeThreshold)
		if(rigidbody0 && rigidbody1){
			//NSLog(@"past time: %f",passedTime);
			// here you handle your globalBody & otherGlobalBody sound effect or what not derpa
			if ((rigidbody0==globalBody && rigidbody1 == otherGlobalBody) ||(rigidbody1==otherGlobalBody && rigidbody0 == globalBody))
			{
			
					playRandomBounceSfxFromList();
					return true;
				}
				else {
					return false;
				}
			}	
		}
	
	return false;
	
}
razer
Posts: 82
Joined: Sun Apr 04, 2010 10:08 pm

Re: Physics and sounds and effects

Post by razer »

Thank You! it is basically what I did

http://www.bulletphysics.org/mediawiki- ... _Callbacks

As I understand that these callbacks might be called many times for 1 hit.

So I can start playing many sounds concurrently and how to detect volume (energy of hit)

btManifoldPoint has m_appliedImpulse scalar value.

The problem (as I read) is that there could be many contact points and may be impulse is shared between all contact points.
So I must group all contact points related to one hit, sum impulses and play sounds of such volume. But it could be late.
So I must start playing some sound and then make it louder when more contact points are processed or change audio file at all because there are different types of hits.

It looks like "sound physics". When I play sound I can create body and if new sound is colliding then these sounds are grouped and played as one sound. but it is a bit more tricky then I wish to code right now.

May be there is some hit ID that is part of all contact points that belong to one hit. Or something like that.
hendra65
Posts: 1
Joined: Thu Aug 05, 2010 2:13 am

Re: Physics and sounds and effects

Post by hendra65 »

hi chrozz,

thank you, your code is really useful.