package org.pollux3d.menu;
import org.pollux3d.cam.CamLocationListener;
import org.pollux3d.core.Pollux;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
public class FacingGeometry extends Geometry implements CamLocationListener {
private boolean isFacing = false;
protected Vector3f firstAxis = new Vector3f(0,0,1.0f);
protected Vector3f secondAxis = new Vector3f(0,1.0f,0);
protected Vector3f upAxis = new Vector3f(1.0f,0,0);
public FacingGeometry(String name, Mesh mesh) {
super(name, mesh);
startFacing();
}
public void startFacing() {
if (!isFacing) {
Pollux.get().getStateManager().getCam().addCamDirectionListener(this);
isFacing = true;
}
}
public void stopFacing() {
if (isFacing) {
Pollux.get().getStateManager().getCam().removeCamDirectionListener(this);
isFacing = false;
}
}
public boolean isFacing() {
return isFacing;
}
@Override
public void onCamLocationChange(Vector3f camLoaction) {
Vector3f newDirection = new Vector3f();
Vector3f newUp = new Vector3f();
Vector3f newLeft = new Vector3f();
newDirection.set(camLoaction).subtractLocal(getLocalTranslation()).normalizeLocal();
newUp.set(Vector3f.UNIT_Y);
newLeft.set(newUp).crossLocal(newDirection).normalizeLocal();
if (newLeft.equals(Vector3f.ZERO)) {
if (newDirection.x != 0) {
newLeft.set(newDirection.y, -newDirection.x, 0f);
} else {
newLeft.set(0f, newDirection.z, -newDirection.y);
}
}
newUp.set(newDirection).crossLocal(newLeft).normalizeLocal();
Quaternion r = new Quaternion().fromAxes(newLeft, newUp, newDirection);
r.normalize();
this.setLocalRotation(r);
}
}