proximity based volume [SOLVED] :)

Get help using Construct 2

Post » Mon Nov 26, 2012 10:40 am

Here's the setup:

I have a player sprite and an 'emitter' sprite, what I want to happen is when the player gets within a range of say 600 px of the emitter, I want the engine to play and loop an audio file.

I want the volume of the audio file to increase to a maximum of 0db and to a minimum of -20 dependant on the proximity of the player sprite to the emitter sprite.

When the player is out of the range of the emitter, I want the engine to stop playing the audio file.


I can do most of this myself but I can't work out how to set up the variables and limk them with the volume of the audio file and to clamp the min / max range of said audio file.

Any help will be appriciated.
ThanksCrudeMik2012-11-26 18:15:22
B
39
S
12
G
8
Posts: 580
Reputation: 7,108

Post » Mon Nov 26, 2012 11:26 am

So looking over the manual I think there are two ways you could do this.

1) Find a custom plugin for audio (if one exists).

2) Use multiple versions of the same audio track manually set at different volumes using a sound editor. This would require no plugin, but would increase the number of the same audio file you need for your game. (this increasing its size).
B
20
S
4
G
2
Posts: 124
Reputation: 2,482

Post » Mon Nov 26, 2012 11:48 am

There's a way of doing it without duplicating audio files or writing a custom plugin for sure, I just don't know quite know how to do it.

We can control the volume of an audio file via a variable. I just don't know how to link and clamp that variable with the volume properties of an audio file.CrudeMik2012-11-26 11:49:01
B
39
S
12
G
8
Posts: 580
Reputation: 7,108

Post » Mon Nov 26, 2012 12:05 pm

Oh oh oh yeah, sorry missed it in the manual.

Are you asking how to set the distance to interact with the Set volume action?
B
20
S
4
G
2
Posts: 124
Reputation: 2,482

Post » Mon Nov 26, 2012 12:10 pm

I want to compare the distance between two sprites and adjust the volume of an audiofile accordingly and to update every tick.

So if player is 600 px or greater distance from the emitter i want to stop playing the file.

If the player is less than 600 px I want to play the file and to have its volume to ramp up incrementally from -20db.

If the player is within 100 px of the emitter I want the volume to set at 0 db.

CrudeMik2012-11-26 12:11:32
B
39
S
12
G
8
Posts: 580
Reputation: 7,108

Post » Mon Nov 26, 2012 12:14 pm

This post will be helpful to determine the distance between your emitter and sprite:

http://www.scirra.com/forum/how-to-get-the-distance-between-two-sprites_topic45039.html

Assign the formula to a variable

Then you can set a condition of when player sprite comes in X distance then set action to change audio set volume.

IsometricRobot2012-11-26 12:15:04
B
20
S
4
G
2
Posts: 124
Reputation: 2,482

Post » Mon Nov 26, 2012 12:27 pm

I get that, but doing it this way would not mean a smooth gradient of volume it'd step.


For example, if player is 599 px away from sprite : set volume to -20
if player is 589 px away from sprite : set volume to -19.5
if player is 579 px away from sprite : set volume to -19

etc

There has to be an efficent dynamic way of doing this using min / max values set to clamped variables. I just can't work it out.
B
39
S
12
G
8
Posts: 580
Reputation: 7,108

Post » Mon Nov 26, 2012 12:41 pm

You need a formula in the set volume action that has a relationship between a decreasing number and a slowly increasing number.


Hmmm let me chart this and I might be able to grab the needed formula. Give me about two hours or so. I'm at work and about to get off shift.
B
20
S
4
G
2
Posts: 124
Reputation: 2,482

Post » Mon Nov 26, 2012 12:49 pm

set volume to: -distance(player.x,player.y,emitter.x,emitter.y) / factor + startingCondition

Factor should be something from 10 to 200, and determines how quickly the sound fades out when you move away. StartingCondition is the volume you want when the player is right on top of the emitter.

Hope this helps a little.
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Mon Nov 26, 2012 12:54 pm

thanks a lot guys this looks good will try it out at lunch
B
39
S
12
G
8
Posts: 580
Reputation: 7,108

Next

Return to How do I....?

Who is online

Users browsing this forum: 99Instances2Go, abdalghani, Ethan, Marxally, Naightwolf and 28 guests