/** * Sets this {@link Matrix4} to a world matrix with the specified cardinal axis and the origin at the * provided position. * * @param position {@link Vector3} The position to use as the origin of the world coordinates. * @param forward {@link Vector3} The direction of the forward (z) vector. * @param up {@link Vector3} The direction of the up (y) vector. * * @return A reference to this {@link Matrix4} to facilitate chaining. */ @NonNull public Matrix4 setToWorld(@NonNull Vector3 position, @NonNull Vector3 forward, @NonNull Vector3 up) { mVec1.setAll(forward).normalize(); // Forward mVec2.setAll(mVec1).cross(up).normalize(); // Right mVec3.setAll(mVec2).cross(mVec1).normalize(); // Up return setAll(mVec2, mVec3, mVec1.multiply(-1d), position); }
public void set(Vector3 point1, Vector3 point2, Vector3 point3) { Vector3 v1 = new Vector3(); Vector3 v2 = new Vector3(); v1.subtractAndSet(point1, point2); v2.subtractAndSet(point3, point2); mNormal = v1.cross(v2); mNormal.normalize(); mD = -point1.dot(mNormal); }
v2.y = terrain[x][z]; na = v1.subtract(v0).cross(v2.subtract(v0)); na.inverse(); v2.y = terrain[x + 1][z]; nb = v1.subtract(v0).cross(v2.subtract(v0)); nb.inverse(); v2.y = terrain[x][z + 1]; nc = v1.subtract(v0).cross(v2.subtract(v0)); nc.inverse(); v2.y = terrain[x - 1][z]; nd = v1.subtract(v0).cross(v2.subtract(v0)); nd.inverse();
private void applyRotation() { if (mIsRotating) { mapToSphere((float) mPrevScreenCoord.getX(), (float) mPrevScreenCoord.getY(), mPrevSphereCoord); mapToSphere((float) mCurrScreenCoord.getX(), (float) mCurrScreenCoord.getY(), mCurrSphereCoord); Vector3 rotationAxis = mPrevSphereCoord.clone(); rotationAxis.cross(mCurrSphereCoord); rotationAxis.normalize(); double rotationAngle = Math.acos(Math.min(1, mPrevSphereCoord.dot(mCurrSphereCoord))); mCurrentOrientation.fromAngleAxis(rotationAxis, MathUtil.radiansToDegrees(rotationAngle)); mCurrentOrientation.normalize(); Quaternion q = new Quaternion(mStartOrientation); q.multiply(mCurrentOrientation); mEmpty.setOrientation(q); } }
@Test public void testCrossFromDoublesXyz() { final Vector3 u = new Vector3(1d, 2d, 3d); final Vector3 out = u.cross(4d, 5d, 6d); assertNotNull(out); assertTrue(out == u); assertEquals(-3d, u.x, 0); assertEquals(6d, u.y, 0); assertEquals(-3d, u.z, 0); }
@Test public void testCrossFromVector3() { final Vector3 u = new Vector3(1d, 2d, 3d); final Vector3 v = new Vector3(4d, 5d, 6d); final Vector3 out = u.cross(v); assertNotNull(out); assertTrue(out == u); assertEquals(-3d, u.x, 0); assertEquals(6d, u.y, 0); assertEquals(-3d, u.z, 0); }
/** * Sets this {@link Matrix4} to a world matrix with the specified cardinal axis and the origin at the * provided position. * * @param position {@link Vector3} The position to use as the origin of the world coordinates. * @param forward {@link Vector3} The direction of the forward (z) vector. * @param up {@link Vector3} The direction of the up (y) vector. * @return A reference to this {@link Matrix4} to facilitate chaining. */ public Matrix4 setToWorld(final Vector3 position, final Vector3 forward, final Vector3 up) { mVec1.setAll(forward).normalize(); mVec2.setAll(mVec1).cross(up).normalize(); mVec3.setAll(mVec2).cross(mVec1).normalize(); return setAll(mVec2, mVec3, mVec1, position); }
/** * Sets this {@link Matrix4} to a look at matrix with a direction and up {@link Vector3}. * You can multiply this with a translation {@link Matrix4} to get a camera Model-View matrix. * * @param direction {@link Vector3} The look direction. * @param up {@link Vector3} The up axis. * @return A reference to this {@link Matrix4} to facilitate chaining. */ public Matrix4 setToLookAt(final Vector3 direction, final Vector3 up) { mVec3.setAll(direction).normalize(); mVec1.setAll(direction).normalize(); mVec1.cross(up).normalize(); mVec2.setAll(mVec1).cross(mVec3).normalize(); identity(); m[M00] = mVec1.x; m[M01] = mVec1.y; m[M02] = mVec1.z; m[M10] = mVec2.x; m[M11] = mVec2.y; m[M12] = mVec2.z; m[M20] = mVec3.x; m[M21] = mVec3.y; m[M22] = mVec3.z; return this; }
public void set(Vector3 point1, Vector3 point2, Vector3 point3) { Vector3 v1 = new Vector3(); Vector3 v2 = new Vector3(); v1.subtractAndSet(point1, point2); v2.subtractAndSet(point3, point2); mNormal = v1.cross(v2); mNormal.normalize(); mD = -point1.dot(mNormal); }
private void applyRotation() { if (mIsRotating) { mapToSphere((float) mPrevScreenCoord.getX(), (float) mPrevScreenCoord.getY(), mPrevSphereCoord); mapToSphere((float) mCurrScreenCoord.getX(), (float) mCurrScreenCoord.getY(), mCurrSphereCoord); Vector3 rotationAxis = mPrevSphereCoord.clone(); rotationAxis.cross(mCurrSphereCoord); rotationAxis.normalize(); double rotationAngle = Math.acos(Math.min(1, mPrevSphereCoord.dot(mCurrSphereCoord))); mCurrentOrientation.fromAngleAxis(rotationAxis, MathUtil.radiansToDegrees(rotationAngle)); mCurrentOrientation.normalize(); Quaternion q = new Quaternion(mStartOrientation); q.multiply(mCurrentOrientation); mEmpty.setOrientation(q); } }