I tracked down a problem I've been having for a while. However, I can not figure out how to solve it. It is related to moving the listener away from the origin.
I place a source somewhere like:
Position: (0, 0, 250)
I place the listener at:
Position: (0, 0, 0)
Look-at: (0, 0, 1)
Up-direction: (0, 1, 0)
Now, I can turn the listener, and the source volume pans between left and right speakers correctly.
However, if I move the listener to:
Position: (0, 0, 200)
Look-at: (0, 0, 201)
Up-direction: (0, 1, 0 )
Now, if I turn the listener, the source doesn't pan between left and right speakers any more. I suspect it has something to do with the orientation being set incorrectly. Does anyone have some experience with this, or know of a good tutorial I could read about moving the listener? All the tutorials I can find cover moving the sources, but not much with moving the listener.
My code for moving the listener currently looks like this:
private void moveListener( float x, float y, float z )
{
float xOffset = x - listenerPosition.get( 0 );
float yOffset = y - listenerPosition.get( 1 );
float zOffset = z - listenerPosition.get( 2 );
listenerPosition.put( 0, x );
listenerPosition.put( 1, y );
listenerPosition.put( 2, z );
AL10.alListener( AL10.AL_POSITION, listenerPosition );
// Keep the listener facing the same direction by
// moving the "look at" point by the offset values:
listenerOrientation.put( 0, listenerOrientation.get( 0 ) + xOffset );
listenerOrientation.put( 1, listenerOrientation.get( 1 ) + yOffset );
listenerOrientation.put( 2, listenerOrientation.get( 2 ) + zOffset );
AL10.alListener( AL10.AL_ORIENTATION, listenerOrientation );
}
For reference:
Listener is initially created using the following code:
private synchronized void initListener()
{
// Listener is at the origin, facing along the z axis, no velocity:
listenerPosition = BufferUtils.createFloatBuffer( 3 ).put(
new float[] { 0.0f, 0.0f, 0.0f } );
listenerOrientation = BufferUtils.createFloatBuffer( 6 ).put (
new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f } );
listenerVelocity = BufferUtils.createFloatBuffer( 3 ).put (
new float[] { 0.0f, 0.0f, 0.0f } );
// Flip the buffers, so they can be used:
listenerPosition.flip();
listenerOrientation.flip();
listenerVelocity.flip();
AL10.alListener( AL10.AL_POSITION, listenerPosition );
AL10.alListener( AL10.AL_ORIENTATION, listenerOrientation );
AL10.alListener( AL10.AL_VELOCITY, listenerVelocity );
}
And the listener is turned using the following:
public void setListenerAngle( float angle )
{
float xOffset = -1.0f * (float) Math.sin( angle );
float zOffset = -1.0f * (float) Math.cos( angle );
listenerOrientation.put( 0, listenerPosition.get( 0 ) + xOffset);
listenerOrientation.put( 2, listenerPosition.get( 2 ) + zOffset);
AL10.alListener( AL10.AL_ORIENTATION, listenerOrientation );
}
Any ideas what I am doing wrong? I've tried not changing the listener's orientation at all, and I've also tried changing the listener's up-direction by adding the offset values, but neither of those ideas worked.