Hello Guest

OpenAL play only when the camera is in the right dirction

  • 6 Replies
  • 17477 Views
*

Offline KKSystem

  • *
  • 3
  • Dream your best, and do even better
OpenAL play only when the camera is in the right dirction
« on: February 06, 2017, 03:12:03 »
Hi everybody,
I am using OpanGL in lwjgl according to karl (ThinMatrix) youtube tutorial, and I try this 3 weeks to find out how do I make the music to know in which direction my camera is on.
For example, when I looking with my camera forward the music needs to be in my right ear, and when I looking with my camera to back, the music needs to be in my left ear.
Do someone know how do I can do it?
Thanks for help,
Korel.
Korel.


*

Offline KKSystem

  • *
  • 3
  • Dream your best, and do even better
Re: OpenAL play only when the camera is in the right dirction
« Reply #2 on: February 06, 2017, 19:03:34 »
I don't have the functions:
transformDirection
positiveZ
positiveY
In my Matrix4f objects, so it doesn't works for me :(

My camera objects:
private Vector3f _position = new Vector3f(0, 40f, 0);
private float _pitch;
private float _yaw;
private float _roll;

Set listener datat method:
public static void setListenerData(float x, float y, float z, float directionX, float directionY, float directionZ)
{
   AL10.alListener3f(AL10.AL_POSITION, x, y, z);
   AL10.alListener3f(AL10.AL_VELOCITY, 0, 0, 0);
   AL10.alListener3f(AL10.AL_DIRECTION, directionX, directionY, directionZ);
}

Audio source init/creation:
buffer = AudioMaster.loadSound("StartBlurMusicMono");
for (int i = 0; i < 8; i++)
{
   list.add(new Source(buffer));
   currentSource = list.get(list.size() - 1);
   currentSource.setLooping(true);
   currentSource.setPosition(100 + i * 100, 50, -100);
   currentSource.setRefferenceDistance(105);
   currentSource.setMaxDistance(200);
   currentSource.setRollOffFactor(10f);
   currentSource.setVolume(0.6f);
   currentSource.play();
   currentSource.pause();
   currentSource.setDirection(1f, 0f, 0f);
}
Korel.

*

Kai

Re: OpenAL play only when the camera is in the right dirction
« Reply #3 on: February 06, 2017, 19:24:36 »
'transformDirection()' is just a multiplication of the upper-left 3x3 submatrix with a 3D vector.
And you also only need 'invert()' additionally. See the first code snippet of the referenced post.
What you generally need to do (which is also described in the referenced forum post) is to transform the base axes of the camera/eye-space into world-space and use those for OpenAL's AL_ORIENTATION.
Also, AL_DIRECTION is not defined for the listener. It is only used for the source.
« Last Edit: February 06, 2017, 19:29:35 by Kai »

*

Offline KKSystem

  • *
  • 3
  • Dream your best, and do even better
Re: OpenAL play only when the camera is in the right dirction
« Reply #4 on: February 06, 2017, 20:45:28 »
"'transformDirection()' is just a multiplication of the upper-left 3x3 submatrix with a 3D vector." - can you show me in code how can I do this thing please?
I tried something like this:

Matrix4f cam = Maths.createTransformationMatrix(camera.getPosition(), camera.getRoll(), camera.getPitch(), camera.getYaw(), 1);
Matrix4f invCam;
Matrix4f.invert(cam, invCam);
Vector3f at = new Vector3f(0, 0, -1);
//invCam.transformDirection(at);
Vector3f up = new Vector3f(0, 1, 0);
//invCam.transformDirection(up);

createTransformationMatrix method:
public static Matrix4f createTransformationMatrix(Vector3f translation, float rx, float ry, float rz, float scale)
{
   Matrix4f matrix = new Matrix4f();
   matrix.setIdentity();
   Matrix4f.translate(translation, matrix, matrix);
   Matrix4f.rotate((float) Math.toRadians(rx), new Vector3f(1,0,0), matrix, matrix);
   Matrix4f.rotate((float) Math.toRadians(ry), new Vector3f(0,1,0), matrix, matrix);
   Matrix4f.rotate((float) Math.toRadians(rz), new Vector3f(0,0,1), matrix, matrix);
   Matrix4f.scale(new Vector3f(scale,scale,scale), matrix, matrix);
   return matrix;
}

Thank you for your help :)
Korel.

Re: OpenAL play only when the camera is in the right dirction
« Reply #5 on: April 14, 2020, 21:04:10 »
I have the same problem. The sounds are working and they are directional, it's just that they aren't affected by my camera rotation at all.

The code I'm using is:
Code: [Select]
Matrix4f matr = Maths.createViewMatrix(MainGameLoop.camera);
Matrix4f invCam = new Matrix4f();
Matrix4f.invert(matr, invCam);
Vector4f at = new Vector4f(0, 0, -1, 0);
Matrix4f.transform(invCam, at, at);
Vector4f up = new Vector4f(0, 1, 0, 0);
Matrix4f.transform(invCam, up, up);

playerOrientation[0] = at.x; // x
playerOrientation[1] = at.y; // y
playerOrientation[2] = at.z; // z

playerOrientation[3] = up.x; // x
playerOrientation[4] = up.y; // y
playerOrientation[5] = up.z; // z

alListenerf(AL_ORIENTATION, playerOrientation[0]);

*

Offline abcdef

  • ****
  • 336
Re: OpenAL play only when the camera is in the right dirction
« Reply #6 on: April 21, 2020, 12:04:53 »
AL_ORIENTATION needs 6 parameters, three for at direction and 3 for up direction

alListenerf is not the right method to use