use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class BillboardControl method rotateScreenAligned.
/**
* Rotate the billboard so it points directly opposite the direction the
* camera's facing
*
* @param camera
* Camera
*/
private void rotateScreenAligned(Camera camera) {
// coopt diff for our in direction:
look.set(camera.getDirection()).negateLocal();
// coopt loc for our left direction:
left.set(camera.getLeft()).negateLocal();
orient.fromAxes(left, camera.getUp(), look);
Node parent = spatial.getParent();
Quaternion rot = new Quaternion().fromRotationMatrix(orient);
if (parent != null) {
rot = parent.getWorldRotation().inverse().multLocal(rot);
rot.normalizeLocal();
}
spatial.setLocalRotation(rot);
fixRefreshFlags();
}
use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class CameraControl method controlUpdate.
// fields used, when inversing ControlDirection:
@Override
protected void controlUpdate(float tpf) {
if (spatial != null && camera != null) {
switch(controlDir) {
case SpatialToCamera:
camera.setLocation(spatial.getWorldTranslation());
camera.setRotation(spatial.getWorldRotation());
break;
case CameraToSpatial:
// set the localtransform, so that the worldtransform would be equal to the camera's transform.
// Location:
TempVars vars = TempVars.get();
Vector3f vecDiff = vars.vect1.set(camera.getLocation()).subtractLocal(spatial.getWorldTranslation());
spatial.setLocalTranslation(vecDiff.addLocal(spatial.getLocalTranslation()));
// Rotation:
Quaternion worldDiff = vars.quat1.set(camera.getRotation()).subtractLocal(spatial.getWorldRotation());
spatial.setLocalRotation(worldDiff.addLocal(spatial.getLocalRotation()));
vars.release();
break;
}
}
}
use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class Quaternion method lookAt.
/**
* <code>lookAt</code> is a convienence method for auto-setting the
* quaternion based on a direction and an up vector. It computes
* the rotation to transform the z-axis to point into 'direction'
* and the y-axis to 'up'.
*
* @param direction
* where to look at in terms of local coordinates
* @param up
* a vector indicating the local up direction.
* (typically {0, 1, 0} in jME.)
*/
public void lookAt(Vector3f direction, Vector3f up) {
TempVars vars = TempVars.get();
vars.vect3.set(direction).normalizeLocal();
vars.vect1.set(up).crossLocal(direction).normalizeLocal();
vars.vect2.set(direction).crossLocal(vars.vect1).normalizeLocal();
fromAxes(vars.vect1, vars.vect2, vars.vect3);
vars.release();
}
use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class Quaternion method toRotationMatrix.
/**
* <code>toRotationMatrix</code> converts this quaternion to a rotational
* matrix. The result is stored in result. 4th row and 4th column values are
* untouched. Note: the result is created from a normalized version of this quat.
*
* @param result
* The Matrix4f to store the result in.
* @return the rotation matrix representation of this quaternion.
*/
public Matrix4f toRotationMatrix(Matrix4f result) {
TempVars tempv = TempVars.get();
Vector3f originalScale = tempv.vect1;
result.toScaleVector(originalScale);
result.setScale(1, 1, 1);
float norm = norm();
// we explicitly test norm against one here, saving a division
// at the cost of a test and branch. Is it worth it?
float s = (norm == 1f) ? 2f : (norm > 0f) ? 2f / norm : 0;
// compute xs/ys/zs first to save 6 multiplications, since xs/ys/zs
// will be used 2-4 times each.
float xs = x * s;
float ys = y * s;
float zs = z * s;
float xx = x * xs;
float xy = x * ys;
float xz = x * zs;
float xw = w * xs;
float yy = y * ys;
float yz = y * zs;
float yw = w * ys;
float zz = z * zs;
float zw = w * zs;
// using s=2/norm (instead of 1/norm) saves 9 multiplications by 2 here
result.m00 = 1 - (yy + zz);
result.m01 = (xy - zw);
result.m02 = (xz + yw);
result.m10 = (xy + zw);
result.m11 = 1 - (xx + zz);
result.m12 = (yz - xw);
result.m20 = (xz - yw);
result.m21 = (yz + xw);
result.m22 = 1 - (xx + yy);
result.setScale(originalScale);
tempv.release();
return result;
}
use of com.jme3.math.Quaternion in project jmonkeyengine by jMonkeyEngine.
the class CubeField method camTakeOver.
/**
* Forcefully takes over Camera adding functionality and placing it behind the character
* @param tpf Tickes Per Frame
*/
private void camTakeOver(float tpf) {
cam.setLocation(player.getLocalTranslation().add(-8, 2, 0));
cam.lookAt(player.getLocalTranslation(), Vector3f.UNIT_Y);
Quaternion rot = new Quaternion();
rot.fromAngleNormalAxis(camAngle, Vector3f.UNIT_Z);
cam.setRotation(cam.getRotation().mult(rot));
camAngle *= FastMath.pow(.99f, fpsRate * tpf);
}
Aggregations