public Vector3f transformVector(final Vector3f in, Vector3f store) { if (store == null) store = new Vector3f(); // multiply with scale first, then rotate, finally translate (cf. // Eberly) return rot.mult(store.set(in).multLocal(scale), store).addLocal(translation); }
/** * <code>rotateUpTo</code> is a utility function that alters the local rotation to point the Y * axis in the direction given by newUp. * * @param newUp the up vector to use - assumed to be a unit vector. */ public void rotateUpTo(Vector3f newUp) { TempVars vars = TempVars.get(); Vector3f compVecA = vars.vect1; Quaternion q = vars.quat1; // First figure out the current up vector. Vector3f upY = compVecA.set(Vector3f.UNIT_Y); Quaternion rot = localTransform.getRotation(); rot.multLocal(upY); // get angle between vectors float angle = upY.angleBetween(newUp); // figure out rotation axis by taking cross product Vector3f rotAxis = upY.crossLocal(newUp).normalizeLocal(); // Build a rotation quat and apply current local rotation. q.fromAngleNormalAxis(angle, rotAxis); q.mult(rot, rot); vars.release(); setTransformRefresh(); }
public Vector3f reflect(Vector3f point, Vector3f store) { if (store == null) store = new Vector3f(); float d = pseudoDistance(point); store.set(normal).negateLocal().multLocal(d * 2f); store.addLocal(point); return store; }
/** * <code>lookAt</code> is a convenience method for auto-setting the local rotation based on a * position in world space and an up vector. It computes the rotation to transform the z-axis to * point onto 'position' and the y-axis to 'up'. Unlike {@link * Quaternion#lookAt(com.jme3.math.Vector3f, com.jme3.math.Vector3f) } this method takes a world * position to look at and not a relative direction. * * <p>Note : 28/01/2013 this method has been fixed as it was not taking into account the parent * rotation. This was resulting in improper rotation when the spatial had rotated parent nodes. * This method is intended to work in world space, so no matter what parent graph the spatial has, * it will look at the given position in world space. * * @param position where to look at in terms of world coordinates * @param upVector a vector indicating the (local) up direction. (typically {0, 1, 0} in jME.) */ public void lookAt(Vector3f position, Vector3f upVector) { Vector3f worldTranslation = getWorldTranslation(); TempVars vars = TempVars.get(); Vector3f compVecA = vars.vect4; compVecA.set(position).subtractLocal(worldTranslation); getLocalRotation().lookAt(compVecA, upVector); if (getParent() != null) { Quaternion rot = vars.quat1; rot = rot.set(parent.getWorldRotation()).inverseLocal().multLocal(getLocalRotation()); rot.normalizeLocal(); setLocalRotation(rot); } vars.release(); setTransformRefresh(); }
/** Loads the identity. Equal to translation=0,0,0 scale=1,1,1 rot=0,0,0,1. */ public void loadIdentity() { translation.set(0, 0, 0); scale.set(1, 1, 1); rot.set(0, 0, 0, 1); }
/** * Sets this matrix's scale to the given x,y,z values. * * @param x This matrix's new x scale. * @param y This matrix's new y scale. * @param z This matrix's new z scale. * @return this */ public Transform setScale(float x, float y, float z) { scale.set(x, y, z); return this; }
/** * Sets this matrix's translation to the given x,y,z values. * * @param x This matrix's new x translation. * @param y This matrix's new y translation. * @param z This matrix's new z translation. * @return this */ public Transform setTranslation(float x, float y, float z) { translation.set(x, y, z); return this; }
/** * Stores this scale value into the given vector3f. If scale is null, a new vector3f is created to * hold the value. The value, once stored, is returned. * * @param scale The store location for this matrix's scale. * @return The value of this matrix's scale. */ public Vector3f getScale(Vector3f scale) { if (scale == null) scale = new Vector3f(); scale.set(this.scale); return scale; }
/** * Stores this translation value into the given vector3f. If trans is null, a new vector3f is * created to hold the value. The value, once stored, is returned. * * @param trans The store location for this matrix's translation. * @return The value of this matrix's translation. */ public Vector3f getTranslation(Vector3f trans) { if (trans == null) trans = new Vector3f(); trans.set(this.translation); return trans; }
/** * Initialize the Plane using the given 3 points as coplanar. * * @param v1 the first point * @param v2 the second point * @param v3 the third point */ public void setPlanePoints(Vector3f v1, Vector3f v2, Vector3f v3) { normal.set(v2).subtractLocal(v1); normal.crossLocal(v3.x - v1.x, v3.y - v1.y, v3.z - v1.z).normalizeLocal(); constant = normal.dot(v1); }
public Vector3f getClosestPoint(Vector3f point, Vector3f store) { // float t = constant - normal.dot(point); // return store.set(normal).multLocal(t).addLocal(point); float t = (constant - normal.dot(point)) / normal.dot(normal); return store.set(normal).multLocal(t).addLocal(point); }