Problem
To make your 3D game more real, you want to place every sound somewhere in the 3D space. In this way, the explosion on the right side of the camera will be played mainly through the right channel, so that the user will feel that the explosion actually occurred on the right side of the camera. Even if the explosion is not in the camera's view, the player can know what is happening on his right side.
Note:Zune does not support xact and the following apply3d () methods. It only supports cue objects. 3D sound effects are only supported by PC and Xbox 360.
Solution
XNa makes these things simple. For each sound, you must set the camera's 3D location and use the apply3d method of the sound cue object to set the location of the sound source. Note that when the camera or sound source moves, you need to call this method to update the cue. Working principle if you want to set the 3D location of the sound source, you must first set an audioemitter object for the sound source and set an audiolistener object for the camera. When the two are passed to the apply3d method of cue, xNa calculates the distribution of the left and right audio channels of the audio (or headphones.
The following method creates two objects:
Private void updatesoundposition (cue, vector3 sourcepos, vector3 Campos, vector3 camforward, vector3 camup )... {audioemitter emitter = new audioemitter (); emitter. position = sourcepos; audiolistener listener = new audiolistener (); listener. position = Campos; listener. forward = camforward; listener. up = camup; cue. apply3d (listener, emitter );}
For emitter, you only need to set the location. For the listener (CAMERA), you also need to set the forward and up vectors, because when the camera rotates for 180 degrees, the left and right channels will be exchanged.
After these two variables are created, you can pass them to the apply3d method of cue. This operation is required when the audio source or camera moves.
You need to call the apply3d method before calling the play method for the first time. Therefore, make sure that the play method has not been called in cue:
Protected override void initialize ()... {fpscam = new quakecamera (graphicsdevice. viewport, new vector3 (0, 0, 5), 0, 0); audioengine = new audioengine ("content/Audio/myxactproject. xgs "); wavebank = new wavebank (audioengine," content/Audio/mywavebank. xwb "); soundbank = new soundbank (audioengine," content/Audio/mysoundbank. xsb "); cue1 = soundbank. getcue ("audio1"); base. initialize ();}
In the update method, if you move the camera location or audio source location, you need to call the updatesoundposition method:
Protected override void Update (gametime )... {gamepadstate = gamepad. getstate (playerindex. one); If (gamepadstate. buttons. back = buttonstate. pressed) This. exit (); mousestate = mouse. getstate (); keyboardstate keystate = keyboard. getstate (); fpscam. update (mousestate, keystate, gamepadstate); float time = (float) gametime. totalgametime. totalmilliseconds/1000.0f; vector3 startingpos = new vector3 (0, 0,-10); matrix rotmatrix = matrix. createrotationy (time); modelpos = vector3.transform (startingpos, rotmatrix); updatesoundposition (cue1, modelpos, fpscam. position, fpscam. forward, fpscam. upvector); If (cue1.isprepared) cue1.play (); audioengine. update (); base. update (gametime );}
The first half of this method checks user input and passes the input to the camera. For more information, see tutorial 2-3. InCodeThe location of the block update audio source. After the camera and audio source are updated, you need to call the updatesoundposition method to inform cue1 of the final position of the object. If cue1 has not been played, start playing.
Beware:3D effect is implemented by controlling the volume of the left and right audio channels, so any sound information in the sound will be lost: first, convert the sound into a single sound channel, set the volume of the left-right audio channel at the 3D position of the corresponding object.
Code
All initialize, updatesoundposition, and update methods have been written before.
Additional reading
The xact audio tool allows you to add some great 3D sound effects in cue, such as distance attenuation and Doppler effects. Because it is a graphical interface, it is much better to introduce it in the video than in the book. There are some good videos on the http://creators.xna.com site that show you how to add 3D sound effects to cue.
Note:In.