Today I felt like adding some filler sounds so that I could experiment with “3D” sound effects. When I say 3D, I mean making the sounds “come” from the position on the screen that they are currently on. A good example of a popular Game Maker game that had this feature would be Robbie Swifthand. Depending on where the player was on the screen, the sounds would come through those speakers (left or right) at different volumes. He also applied the method to all the other sound effects in the game, whether they came from the orb, the door, or from spikes and axes.

I thought through what I had to do in order to accomplish this, and figured I’d need to know the object’s distance from the center of the room or view, and then convert it to a number between -1 and 1 (because the function sound_pan() has -1 equal left, 0 equal center, and 1 equal right). After a bit of playing around, I came up with the code below.

Create:
1. w = room_width/2;
2. pan = 0;

The above works if you’re not using views; if you are, just change room_width to view_wview. It simply finds the center of the room’s width by dividing it by 2.

Step:
1. dist = distance_to_point(w,y); 

This code above creates the variable dist and sets it equal to the current object’s distance from the center of the room’s width. Below is the full code required for the object’s step event in order to create the dynamic sound effect.

Step:
1. dist = distance_to_point(w,y);
2. 
3. if(dist=0)
4.           {pan=0}
5. else if(x>0) and (x<room_width)
6.           {if(x<w)
7.                     {pan=(-dist/w)}
8.           else if(x>w)
9.                     {pan=(dist/w)}
10.         }

Lines 3 and 4 just give pan a value if the object is in the center of the room. If the object isn’t in the center, it then makes sure that the player is on the screen on line 5. This bit isn’t entirely necessary, but can be good depending on what you want, so I chose to leave it in this example. After checking that, it then sees which side of the room the object is on.

If it is to the left of the screen, it will divide the opposite of the variable dist by the maximum distance, which would be from the center of the room to the edge of the room (w). For instance, if the object’s value is 60, and the width of the room is 640, making w equal 320, then dist will equal 260. Then it divides -260 by 320, which equals -0.81. This value works within the parameters required for sound_pan(), because the value is in the negative range and the object is on the left side of screen.

However, if the object is to the right side of the screen, it simply makes the result positive instead of negative. If the player’s value is 345, and the width of the room is the same as in the previous example, then dist will equal 25. It would then divide 25 by 320, which equals 0.01. This value works as well because the player is on the positive side of the screen (the right side), but is also very close to the center.

Lastly, you need to actually use pan to change the pan for whatever sounds are going to play for the current object. Unfortunately, this method won’t work if the same sound is going to play from multiple objects; you’ll have to use a real 3D sound method in order to do that. Use the code below and insert the name of your sound resource where the variable  index is located.

Step:
12. sound_pan(index,pan); 

If this was helpful or interesting or whatever, leave me a comment for a bit of motivation!
-Nathan Wood

Advertisements

About turboRamble

I am a seventeen-year-old who enjoys programming games and applications, as well as reading and writing.

One response »

  1. Vamparius says:

    Very helpful! 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s