Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 77 additions & 10 deletions jme3-core/src/main/java/com/jme3/audio/Listener.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,17 @@
*/
public class Listener {

private final Vector3f location;
private final Vector3f velocity;
private final Quaternion rotation;
private float volume = 1;
private final Vector3f location = new Vector3f();
private final Vector3f velocity = new Vector3f();
private final Quaternion rotation = new Quaternion();
private float volume = 1f;
private AudioRenderer renderer;


/**
* Constructs a new {@code Listener} with default parameters.
*/
public Listener() {
location = new Vector3f();
velocity = new Vector3f();
rotation = new Quaternion();
}

/**
Expand All @@ -62,9 +60,9 @@ public Listener() {
* @param source The {@code Listener} to copy the properties from.
*/
public Listener(Listener source) {
this.location = source.location.clone();
this.velocity = source.velocity.clone();
this.rotation = source.rotation.clone();
this.location.set(source.location);
this.velocity.set(source.velocity);
this.rotation.set(source.rotation);
this.volume = source.volume;
this.renderer = source.renderer; // Note: Renderer is also copied
}
Expand Down Expand Up @@ -109,6 +107,17 @@ public Vector3f getLocation() {
return location;
}

/**
* Gets the current location of the listener in world space.
*
* @param store The vector to store the result in.
* @return The listener's location as a {@link Vector3f}.
*/
public Vector3f getLocation(Vector3f store) {
if (store == null) store = new Vector3f();
return store.set(location);
}

/**
* Gets the current rotation of the listener in world space.
*
Expand All @@ -118,6 +127,17 @@ public Quaternion getRotation() {
return rotation;
}

/**
* Gets the current rotation of the listener in world space.
*
* @param store The quaternion to store the result in.
* @return The listener's rotation as a {@link Quaternion}.
*/
public Quaternion getRotation(Quaternion store) {
if (store == null) store = new Quaternion();
return store.set(rotation);
}

/**
* Gets the current velocity of the listener.
* This is used for Doppler effect calculations.
Expand All @@ -128,6 +148,17 @@ public Vector3f getVelocity() {
return velocity;
}

/**
* Gets the current velocity of the listener.
*
* @param store The vector to store the result in.
* @return The listener's velocity as a {@link Vector3f}.
*/
public Vector3f getVelocity(Vector3f store) {
if (store == null) store = new Vector3f();
return store.set(velocity);
}

/**
* Gets the left direction vector of the listener.
* This vector is derived from the listener's rotation.
Expand All @@ -138,6 +169,18 @@ public Vector3f getLeft() {
return rotation.getRotationColumn(0);
}


/**
* Gets the left direction vector of the listener. This vector is derived from the listener's rotation.
*
* @param store The vector to store the result in.
* @return The listener's left direction as a {@link Vector3f}.
*/
public Vector3f getLeft(Vector3f store) {
if (store == null) store = new Vector3f();
return rotation.getRotationColumn(0, store);
}

/**
* Gets the up direction vector of the listener.
* This vector is derived from the listener's rotation.
Expand All @@ -148,6 +191,18 @@ public Vector3f getUp() {
return rotation.getRotationColumn(1);
}

/**
* Gets the up direction vector of the listener.
* This vector is derived from the listener's rotation.
*
* @param store The vector to store the result in.
* @return The listener's up direction as a {@link Vector3f}.
*/
public Vector3f getUp(Vector3f store) {
if (store == null) store = new Vector3f();
return rotation.getRotationColumn(1, store);
}

/**
* Gets the forward direction vector of the listener.
* This vector is derived from the listener's rotation.
Expand All @@ -158,6 +213,18 @@ public Vector3f getDirection() {
return rotation.getRotationColumn(2);
}

/**
* Gets the forward direction vector of the listener.
* This vector is derived from the listener's rotation.
*
* @param store The vector to store the result in.
* @return The listener's forward direction.
*/
public Vector3f getDirection(Vector3f store) {
if (store == null) store = new Vector3f();
return rotation.getRotationColumn(2, store);
}

/**
* Sets the location of the listener in world space.
* If an {@link AudioRenderer} is set, it will be notified of the position change.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import com.jme3.math.Vector3f;
import com.jme3.util.BufferUtils;
import com.jme3.util.NativeObjectManager;
import com.jme3.util.TempVars;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
Expand Down Expand Up @@ -766,14 +768,16 @@ private void applyListenerPosition(Listener listener) {
}

private void applyListenerRotation(Listener listener) {
Vector3f dir = listener.getDirection();
Vector3f up = listener.getUp();
TempVars vars = TempVars.get();
Vector3f dir = listener.getDirection(vars.vect1);
Vector3f up = listener.getUp(vars.vect2);
// Use the shared FloatBuffer fb
fb.rewind();
fb.put(dir.x).put(dir.y).put(dir.z);
fb.put(up.x).put(up.y).put(up.z);
fb.flip();
al.alListener(AL_ORIENTATION, fb);
vars.release();
}

private void applyListenerVelocity(Listener listener) {
Expand Down