Page 1 of 1

[Solved : No Bug] SoundRange3D()

Posted: Sat Jan 10, 2015 12:44 pm
by falsam
SoundRange3D() work fine ONLY if a sound is on a node located at 0,0,0.

Snippet code :
- The distance between the camera and the node is always the same.

- The variable n indicates the new coordinates (x = y = z = n) of the node.

- If you increase n, the volume decreases.

Code: Select all

Enumeration
  #Mainform
EndEnumeration

InitEngine3D()
InitKeyboard()
InitSprite()

OpenWindow(#Mainform,0, 0, 800, 600, "", #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(#Mainform), 0, 0, 800, 600, 0, 0, 0)

Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/", #PB_3DArchive_FileSystem)
 
Camera = CreateCamera(#PB_Any,0,0,100,100)
CameraBackColor(Camera, RGB(145, 182, 201))

;Test Sound (x=y=z = n)
n = 0 ;Test with a zero value then the value 400 

;The distance between the camera and the node is always the same.
MoveCamera(Camera, n+500, n+500, n+500)
CameraLookAt(Camera, n, n, n)

;Load Sound
WaterFallSound = LoadSound3D(#PB_Any, "Siren.ogg")

If WaterFallSound 
  WaterFallNode = CreateNode(#PB_Any, n, n, n)
  CreateEntity(#PB_Any, MeshID(CreateSphere(#PB_Any, 25)), #PB_Material_None, n, n, n)  ;Node Landmark
  AttachNodeObject(WaterFallNode, SoundID3D(WaterFallSound))
  SoundVolume3D(WaterFallSound, 100)
  SoundRange3D(WaterFallSound, 100, 800)
  PlaySound3D(WaterFallSound, #PB_Sound3D_Loop)
EndIf 

Repeat
  Repeat
    Event  = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        End
        
    EndSelect
  Until Event = 0
  
  If ExamineKeyboard()
    
    If KeyboardPushed (#PB_Key_Escape)
      Break
    EndIf
  EndIf
  
  RenderWorld(80)
  FlipBuffers()  
  
ForEver
I think the SoundRange is calculated from the point 0,0,0

[Solved] SoundRange3D()

Posted: Wed Jan 28, 2015 2:51 pm
by falsam
This is nott a bug !! If using SoundRange3D (), you must also use SoundListenerLocate (x, y, z).

x = CameraX(#camera)
y = CameraY(#camera)
z = CameraZ(#camera)

■ Code (Use the arrows to move towards the red or blue sphere)

Code: Select all

Enumeration Window
  #Mainform
EndEnumeration

Enumeration D3
  #camera
  
  #player
  
  #material
  
  #ground
  
  #nodeRed
  #nodeBlue
  
  #soundRed
  #soundBlue
  
  #LandMarkRed
  #LandMarkBlue
  
  #Panel
EndEnumeration

Global PlayerSpeed.f = 0.2

InitEngine3D()
InitKeyboard()
InitSprite()
InitMouse()

ExamineDesktops()

OpenWindow(#Mainform,0, 0, 800, 600, "", #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(#Mainform), 0, 0, 800, 600, 0, 0, 0)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts", #PB_3DArchive_FileSystem)
Parse3DScripts()

KeyboardMode(#PB_Keyboard_International)
 
;Light & Shadow
CreateLight(#PB_Any,RGB(255, 255, 255), -4, 100, -50)
WorldShadows(#PB_Shadow_Additive, 30, RGB(250, 235, 215)) ;Comment if Windows 8 

;Camera 
CreateCamera(#camera ,0, 0, 100, 100)
CameraBackColor(#camera, RGB(145, 182, 201))
MoveCamera(#camera, 2, 5, 15, #PB_Absolute)  
CameraLookAt(#camera, 0,0,0)   

;Ground 
GetScriptMaterial(#material, "Color/Yellow")
CreateEntity(#ground, MeshID(CreatePlane(#PB_Any, 100, 100, 1, 1, 1, 1)), MaterialID(#material))


;Red sound (Node, Sound, Red LandMark)
CreateNode(#nodeRed, 20, 1, 30)
LoadSound3D(#soundRed, "Roar.ogg")
GetScriptMaterial(#material,"Color/Red")
CreateEntity(#LandMarkRed, MeshID(CreateSphere(#PB_Any, 1)), MaterialID(#material))
AttachNodeObject(#nodeRed, SoundID3D(#soundRed))
AttachNodeObject(#nodeRed, EntityID(#LandMarkRed))
SoundRange3D(#soundRed, 0, 30)
PlaySound3D(#soundRed, #PB_Sound3D_Loop)

;Blue sound (Node, Sound, Red LandMark)
CreateNode(#nodeBlue, -20, 1, 30)
LoadSound3D(#soundBlue, "Siren.ogg")
GetScriptMaterial(#material,"Color/Blue")
CreateEntity(#LandMarkBlue, MeshID(CreateSphere(#PB_Any, 1)), MaterialID(#material))
AttachNodeObject(#nodeBlue, SoundID3D(#soundBlue))
AttachNodeObject(#nodeBlue, EntityID(#LandMarkBlue))
SoundRange3D(#soundBlue, 0, 30)
PlaySound3D(#soundBlue, #PB_Sound3D_Loop)

;Player
CreateEntity(#player, MeshID(CreateCube(#PB_Any, 1)), #PB_Material_None, 0, 1, -10)
HideEntity(#player, #True)

Repeat ;Event 3D Loop
  Repeat ;Event Window Loop
    Event  = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        End
        
    EndSelect
  Until Event = 0
  
  If ExamineKeyboard()
 
    If KeyboardPushed (#PB_Key_Escape)
      End
    EndIf
    
    ;Move Up or Down
    If KeyboardPushed (#PB_Key_Up)
      MoveEntity(#player, 0, 0, PlayerSpeed, #PB_Absolute|#PB_Local)
    ElseIf KeyboardPushed (#PB_Key_Down)
      MoveEntity(#player, 0, 0, -PlayerSpeed, #PB_Absolute|#PB_Local)
    EndIf
    
    ;Rotate Left or Right
    If KeyboardPushed (#PB_Key_Left)
      RotateEntity(#player, 0, 1.5, 0, #PB_Relative)
    ElseIf KeyboardPushed (#PB_Key_Right)
      RotateEntity(#player, 0, -1.5, 0, #PB_Relative)
    EndIf    
  EndIf
  
  CameraFollow(#camera, EntityID(#player), 180, 1, 1, 0.2, 0.2)
  
  ;IMPORTANT The ear follows the camera (Les oreilles suivent la caméra)
  SoundListenerLocate(CameraX(#camera), CameraY(#camera), CameraZ(#camera))
  
  RenderWorld(30)
  
  ;-Control Panel
  CreateSprite(#Panel, 800, 40)
  StartDrawing(SpriteOutput(#Panel))
  Box(0,0, 800, 40, RGBA(178, 34, 34, 20))
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawText(5,2,"FPS: "+ Str(Engine3DStatus(#PB_Engine3D_CurrentFPS)) +
               "  Min: " + Str(Engine3DStatus(#PB_Engine3D_MinimumFPS)) + 
               "  Max: " + Str(Engine3DStatus(#PB_Engine3D_MaximumFPS)))
  
  DrawText(200, 2, "(" + Str(EntityX(#player)) + ", " + Str(EntityY(#Player)) + ", " + Str(EntityZ(#Player)) + ")")
  StopDrawing()  
  DisplaySprite(#Panel, 0, 0)
  FlipBuffers()  
  
ForEver