2
0

pan.script 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. local position_min = 0 -- <1>
  2. local position_max = sys.get_config_int("display.width") -- <2>
  3. local function normalize_position(x_position) -- <3>
  4. local average = (position_min + position_max) / 2
  5. local range = (position_max - position_min) / 1.8
  6. local result = (x_position - average) / range
  7. return result
  8. end
  9. function on_message(self, message_id, message, sender) -- <4>
  10. if message_id == hash("collision_response") then
  11. local coin_pos = normalize_position(go.get_position().x)
  12. sound.play("#coin", { gain = 0.6, pan = coin_pos } )
  13. end
  14. end
  15. --[[
  16. 1. - Local variable to represent the minimum x position value.
  17. 2. - Local variable to represent the maximum x position value. sys.get_config_int("display.width") to get
  18. screen width used for maximum x position value.
  19. 3. - This function uses the screen x position min & max local variables that is set at the top
  20. of the script to get an average and range then pass in the coin objects x position into
  21. result to get a normalized value and the function returns that value. note: in range if we
  22. divide by 2.0 we would get range -1.0 to 1.0 full 45 degree pan at min/max positions, instead
  23. use 1.8 to get around a 40 deg pan that way we always get a little bit of sound in both
  24. left and right channel outputs no matter the min/max position.
  25. 4. - When a collision_response is received we pass in the coin objects x position into the
  26. normalize_position function and set the results to the local variable coin_pos. Then play
  27. a sound and pass in coin_pos into the sounds pan property.
  28. Now we have simple sound localization using the pan property. If you close your eyes, you should
  29. be able to gauge which direction the collisions are occurring.(as long as you are using stereo sound)
  30. --]]