Distance between two entities decide volume of a sound.

Started by
9 comments, last by Ludus 11 years, 1 month ago

I need help with an algorithm for my 2D Game.

Say entity AA is emitting a sound. The strength of the emitting sound is determined by the distance between AA and BB(this could be the main character).

The further away they are, the weaker the sound is. The max distance(pass this distance and the sound volume is 0) is of course customizable.

The volume setting can be from 0.0 to 1.0.

Where and when to do these checks is not a problem. I know my engine and I can resolve that. How to check it is the problem.

Advertisement

What's the question, exactly? How to check if the distance between AA and BB is over some threshold? Or are you wondering what the falloff function should be?

If you are talking to someone that is far away from you, you wont hear anything. As you move closer to him/her, the sounds becomes clearer and stronger.

I want to implement a logic like this in my game.

Some audio API's support this by default using spatial clues. For example, sfml provides a listener class which denotes where the "listener" is (basically, where a player is in the world, and where it's facing so surround sound will work as well). And the sound class provides a method for setting the position of the sound when it is played.

These combined causes things that happen far away to either be not heard, or dimly heard automatically, as well as provides the correct orientation (so if the player is facing North, and a sound comes from the East, only the right side would play the sound).

FWIW, sfml's Audio class is built upon OpenAL, so I would suggest either using SFML or using openAL to provide these functions for you.

Good luck!

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

In case you have a library that doesn't have this feature, it is fairly trivial.

here is a snippet (linear fallof) from a sound library that I have writen some time ago.


// mRollofFactor defaults to 1
// mReferenceDistance defaults to 1
// mMaxDistance is relative to world size
float DSND::Listener::GetVolume( const Vec3f& pos )
{
	float distance = (mPosition - pos).Length();
	distance = MIN(mMaxDistance,distance);

	float gain = 1.f - mRollofFactor * (distance - mReferenceDistance) / (mMaxDistance - mReferenceDistance);
	return MAX(0.f, MIN(1.f, gain));
}

volume = max_volume - sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));

volume = max_volume - sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));

That's a pretty bad answer...

You want to decide on a falloff function. Physics dictates that the sound volume should be proportional to 1/distance^2. If you want to cut it off completely at some point, you should probably get a smooth function that roughly looks like 1/distance^2 but reaches 0 at the desired distance and doesn't have a discontinuity. For instance

volume = reference_volume * max(1/distance_squared - 1/max_distance_squared, 0);

As has been mentioned before, some API's include this functionality. The SDL_mixer library for SDL has a few functions that simulate audio drop off due to distance. It also has a function that not only does that, but includes panning effects calculated from the angle between the audio source and the listener.

It would be wise to check your API's documentation to see if it has similar functionality.

Thanks for the info. I noticed that I cant use any of the code in this thread because once the sound is playing, you cant change the volume(you have to stop it first).

So I am looking for some APIs that have this implemented.

Is this feature called binding Listener to Camera?


volume = max_volume - sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));

That's a pretty bad answer...

You want to decide on a falloff function. Physics dictates that the sound volume should be proportional to 1/distance^2. If you want to cut it off completely at some point, you should probably get a smooth function that roughly looks like 1/distance^2 but reaches 0 at the desired distance and doesn't have a discontinuity. For instance

volume = reference_volume * max(1/distance_squared - 1/max_distance_squared, 0);


I think it depends some on what a person wants from the sound-source.

for example, the falloff can be used to various effects for different kinds of things, like say, for the sound of a general area (such as a crowd of people or a large room), you might want square-root falloff, but for the ambient noises of a chirping console, you might want squared falloff, and for a character making noise (speaking, yelling, ...), a linear falloff might be better.


for example, in my mixer, there are multiple levels of attenuation:
none, same volume everywhere in the world;
low, square-root falloff (sqrt(volume/dist));
default, linear falloff (volume/dist);
high, square falloff (volume/dist^2).

note that the minimum value for distance is 1.


the sound is skipped if the calculated volume falls below 0.001, and there is also some math to handle things like panning and Doppler shifting and similar (basically, tweaking playback speeds based on relative velocities).

generally, some of these calculations also take relative camera orientation into effect as well, such as things may be slightly louder in-front than above or behind, and things generally above/below will be closer to center-pan than ones at roughly the same height.

This topic is closed to new replies.

Advertisement