Audio Collision + Swipe System

I’m trying to create an audio system that will react to collisions with and swiping/dragging of objects. Thus far I’ve managed a basic system, but it kinda lack consistency. Meaning I get sound on collisions and on swipe, but the collision sound plays to often and the swipe plays whenever the object moves when it has any contact points on another object(ex. ground).

What I’ve done is:

Collision: Whenever there are added contact points I play a sound. I know this is one of the causes of the problem, but I’m not sure how to overcome it.

Current Collision Code:

// If the number of contact points have been increased, there most likely occured a collision.
if (numberOfContactPoints < hit.contacts.Length)
{
	numberOfContactPoints = hit.contacts.Length;
	OnObjectCollision(hit.contacts, hit.relativeVelocity.magnitude, true);
	if (hit.relativeVelocity.magnitude > 0.15f)
	{
		float volume = (magnitude > 10 ? 1 : magnitude * 0.1f);
		PlayCollision(collisionSound, volume);
	}
}
// If the number of collision points have decreased, update the collision contactpoints.
else if (numberOfContactPoints > hit.contacts.Length)
{
	numberOfContactPoints = hit.contacts.Length;	
}

_

Swipe: Here I play a sound whenever the object is touching another object. So if any of the contact points move I play a sound. And here is my second problem, it doesn’t matter if the movement actually was touching anything for the sound to play. If anyone have a good solution to this, it would be greatly appreciated.

Current Swipe Code:

// The minimum amount of magnitude needed for swipe to play sound.
float minMagnitudeBeforeReact = 0.15f;
// Current magnitude check variable.
float magnitude = 0;

// If touching anything.
if (currentContactPoints != null && currentContactPoints.Count > 0)
{
	// Count to check how many times it have been looped.
	float count = 0;

	for (int i = (previousContactPoints.Count - 1); i >= 0; --i)
	{
		++count;

		// If the current amount of contactpoints is less then i + 1 set i to the highest available value.
		if (currentContactPoints.Count < (i + 1))
			i = (currentContactPoints.Count - 1);

		if ((previousContactPoints.Count - 1) > i && (currentContactPoints.Count - 1) > i)
		{
			if (!MathUtils.ApproximatelyEqual(previousContactPoints_, currentContactPoints*, 0.001f))*_

* {*
* // Add the current magnitude difference to the magnitude.*
magnitude += (currentContactPoints - previousContactPoints*).magnitude;*
* }*
* }*

* // If this is the last round.*
* if (i == 0)*
* {*
* // Avg. magnitude.*
* magnitude = magnitude / count;*
* // Multiply to a more usable number.*
_ magnitude = magnitude * 10;
* }
}*_

* // If the magnitude is wihtin desired range and it is time for swipe to update.*
* if (magnitude > minMagnitudeBeforeReact || swipeTimeReset < Time.time)*
* {*
* swipeTimeReset = Time.time + 0.2f;*
* previousContactPoints = currentContactPoints;*
* }*
}

// Update the current contactpoint list.
currentContactPoints = ContactPointsToVector3List(contacts);

// Play a swipe sound if the conditions are correct.
if (magnitude > minMagnitudeBeforeReact && velocity > 0.05f)
{
* PlaySwipe(swipeSound, velocity);*
}
-
Any help here would be greatly appreciated. :smiley:
Or if anyone know of a great Audio Collision/Swipe system let me know and I’ll check it out. :smiley:

In the very first instance, just a basic technique you will need to know,

when you play a sound just do this … here’s some pseudocode …

bool audioBusy

function Play A Sound
  {
  if audioBusy == true, return (do nothing)
  audioBusy = true;
  .. play the sound ..
 Invoke( "soundReadyAgain", 1.5f );
 }

function soundReadyAgain
  { audioBusy = false; }

Just tune the factor of 1.5 seconds until it works good for you.

This is an absolutely basic technique you use for all sorts of things, where you get “continuous notification” of some thing happening. Hope it helps.