int index23 = index2 * 3; Vector3 v0 = new Vector3(mesh.vertices[index03], mesh.vertices[index03 + 1], mesh.vertices[index03 + 2]); Vector3 v1 = new Vector3(mesh.vertices[index13], mesh.vertices[index13 + 1], mesh.vertices[index13 + 2]); Vector3 v2 = new Vector3(mesh.vertices[index23], mesh.vertices[index23 + 1], mesh.vertices[index23 + 2]); Vector3 normal = Vector3.crossAndCreate(Vector3.subtractAndCreate(v2, v0), Vector3.subtractAndCreate(v1, v0)); normal.inverse(); mesh.boneVertices[index0].normal.add(normal); mesh.boneVertices[index1].normal.add(normal); mesh.boneVertices[index2].normal.add(normal); Vector3 normal = vert.normal.clone(); vert.normal.normalize(); normal.normalize(); mesh.normals[normIndex + 2] = (float) normal.z; vert.normal.setAll(0, 0, 0); SkeletonJoint joint = mJoints[weight.jointIndex]; vert.normal.add(Vector3.scaleAndCreate(joint.getOrientation().multiply(normal), weight.weightValue));
public boolean isLookingAtObject(Object3D target, float maxAngle) { mHeadViewQuaternion.fromMatrix(mHeadViewMatrix); mHeadViewQuaternion.inverse(); mForwardVec.setAll(0, 0, 1); mForwardVec.rotateBy(mHeadViewQuaternion); mHeadTranslation.setAll(mHeadViewMatrix.getTranslation()); mHeadTranslation.subtract(target.getPosition()); mHeadTranslation.normalize(); return mHeadTranslation.angle(mForwardVec) < maxAngle; } }
/** * Calculates the side lengths that child nodes * of this node should have. */ protected void calculateChildSideLengths() { //Determine the distance on each axis Vector3 temp = Vector3.subtractAndCreate(mTransformedMax, mTransformedMin); temp.multiply(0.5f); //Divide it in half float overlap = 1.0f + mOverlap/100.0f; temp.multiply(overlap); temp.absoluteValue(); mChildLengths.setAll(temp); }
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); }
/** * Utility method to move the specified number of units along the current forward axis. This will * also adjust the look at target (if a valid one is currently set). * * @param units {@code double} Number of units to move. If negative, movement will be in the "back" direction. */ public void moveForward(double units) { mTempVec.setAll(WorldParameters.FORWARD_AXIS); mTempVec.rotateBy(mOrientation).normalize(); mTempVec.multiply(units); mPosition.add(mTempVec); if (mLookAtEnabled && mLookAtValid) { mLookAt.add(mTempVec); resetToLookAt(); } markModelMatrixDirty(); }
/** * Sets this {@link Quaternion} to be oriented to a target {@link Vector3}. * It is safe to use the input vectors for other things as they are cloned internally. * * @param lookAt {@link Vector3} The point to look at. * @param upDirection {@link Vector3} to use as the up direction. * * @return A reference to this {@link Quaternion} to facilitate chaining. */ @NonNull public Quaternion lookAt(@NonNull Vector3 lookAt, @NonNull Vector3 upDirection) { mTmpVec1.setAll(lookAt); mTmpVec2.setAll(upDirection); // Vectors are parallel/anti-parallel if their dot product magnitude and length product are equal final double dotProduct = Vector3.dot(lookAt, upDirection); final double dotError = Math.abs(Math.abs(dotProduct) - (lookAt.length() * upDirection.length())); if (dotError <= PARALLEL_TOLERANCE) { // The look and up vectors are parallel mTmpVec2.normalize(); if (dotProduct < 0) { mTmpVec1.inverse(); } fromRotationBetween(WorldParameters.FORWARD_AXIS, mTmpVec1); return this; } Vector3.orthoNormalize(mTmpVec1, mTmpVec2); // Find the forward and up vectors mTmpVec3.crossAndSet(mTmpVec2, mTmpVec1); // Create the right vector fromAxes(mTmpVec3, mTmpVec2, mTmpVec1); return this; }
mFrustumCentroid.setAll(0, 0, 0); for(int i=0; i<8; i++) mFrustumCentroid.add(mFrustumCorners[i]); mFrustumCentroid.divide(8.0); double distance = mFrustumCentroid.distanceTo(lightBox.getMin()); Vector3 lightDirection = light.getDirectionVector().clone(); lightDirection.normalize(); Vector3 lightPosition = Vector3.subtractAndCreate(mFrustumCentroid, Vector3.multiplyAndCreate(lightDirection, distance)); mFrustumCorners[i].multiply(mLightViewMatrix);
double halfViewportWidth = viewportWidth / 2; double halfViewportHeight = viewportHeight / 2; Vector3 screenPosition = new Vector3(); double screenPositionPixels_x, screenPositionPixels_y; Camera camera = mRenderer.getCurrentScene().getCamera(); Vector3 cameraPosition = camera.getPosition().clone(); Vector3 cameraLookAt = camera.getLookAt() != null ? camera.getLookAt().clone() : new Vector3(0, 0, 1); Vector3 cameraDirection = cameraLookAt.clone().subtract(cameraPosition); cameraDirection.normalize(); screenPosition.setAll(lensFlare.getPosition().clone()); screenPosition.multiply(viewMatrix); screenPosition.project(projMatrix); Vector3 lightToCamDirection = lensFlare.getPosition().clone().subtract(cameraPosition); lightToCamDirection.normalize(); double angleLightCamera = lightToCamDirection.dot(cameraDirection); screenPosition.setAll(sprite.getScreenPosition());
else if (alt > max) max = alt; normals[i][j] = new Vector3(0f, 1f, 0f); Vector3 v0 = new Vector3(); Vector3 v1 = new Vector3(); Vector3 v2 = new Vector3(); Vector3 na = new Vector3(); Vector3 nb = new Vector3(); Vector3 nc = new Vector3(); Vector3 nd = new Vector3(); na = v1.subtract(v0).cross(v2.subtract(v0)); na.inverse(); nb = v1.subtract(v0).cross(v2.subtract(v0)); nb.inverse(); nc = v1.subtract(v0).cross(v2.subtract(v0)); nc.inverse(); nd = v1.subtract(v0).cross(v2.subtract(v0)); nd.inverse(); normals[x][z].add(na); normals[x][z].add(nb); normals[x][z].add(nc); normals[x][z].add(nd);
public void calculatePoint(Vector3 result, final double t) { if (mCalculateTangents) { double prevt = t == 0 ? t + DELTA : t - DELTA; double nextt = t == 1 ? t - DELTA : t + DELTA; p(mCurrentTangent, prevt); p(mTempNext, nextt); mCurrentTangent.subtract(mTempNext); mCurrentTangent.multiply(.5); mCurrentTangent.normalize(); } p(result,t); }
/** * Sets the world axis values after checking that they are all orthogonal to each other. The check performed * is to verify that the cross product between {@code right} and {@code up} is equivilant to {@code forward} * withing 1ppm error on each component. * * @param right {@link Vector3} The desired right vector. Must be normalized. * @param up {@link Vector3} The desired up vector. Must be normalized. * @param forward {@link Vector3} The desired forward vector. Must be normalized. */ public static void setWorldAxes(Vector3 right, Vector3 up, Vector3 forward) { TEMP_VECTOR.crossAndSet(right, up); if (!TEMP_VECTOR.equals(forward, 1e-6)) { throw new IllegalArgumentException("World axes must be orthogonal."); } RIGHT_AXIS.setAll(right); NEG_RIGHT_AXIS.setAll(RIGHT_AXIS).inverse(); UP_AXIS.setAll(up); NEG_UP_AXIS.setAll(UP_AXIS).inverse(); FORWARD_AXIS.setAll(forward); NEG_FORWARD_AXIS.setAll(FORWARD_AXIS).inverse(); } }
Vector3 position = new Vector3(); Vector3 pos = Vector3.addAndCreate(joint.getPosition(), rotPos); pos.multiply(weight.weightValue); position.add(pos);
Vector3 min = new Vector3(Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE); Vector3 max = new Vector3(-Float.MAX_VALUE, -Float.MAX_VALUE, -Float.MAX_VALUE); Vector3 bs_position = bs.getPosition(); double radius = bs.getScaledRadius(); Vector3 rad = new Vector3(); rad.setAll(radius, radius, radius); test_against_min = Vector3.subtractAndCreate(bs_position, rad); test_against_max = Vector3.addAndCreate(bs_position, rad); } else { RajLog.e("[" + this.getClass().getName() + "] Received a bounding box of unknown type."); mMin.setAll(min); mMax.setAll(max); mTransformedMin.setAll(min); mTransformedMax.setAll(max); calculatePoints(); calculateChildSideLengths();
public void calculatePoint(Vector3 result, double t) { mCurrent = t; if(mCurrent <0) mCurrent=0; if(mCurrent >1) mCurrent=1; mTmpPoint1.setAll(mPoint1); mTmpPoint1.multiply((1-mCurrent) * (1-mCurrent)); mTmpPoint2.setAll(mControlPoint); mTmpPoint2.multiply(2 * (1-mCurrent) * t); mTmpPoint3.setAll(mPoint2); mTmpPoint3.multiply(mCurrent * mCurrent); mTmpPoint2.add(mTmpPoint3); result.addAndSet(mTmpPoint1, mTmpPoint2); }
/** * Efficient Gram-Schmitt Ortho-normalization for the special case of 2 vectors. * * @param v1 The first {@link Vector3} object to be ortho-normalized. * @param v2 The second {@link Vector3}. {@link Vector3} object to be ortho-normalized. */ public static void orthoNormalize(@NonNull Vector3 v1, @NonNull Vector3 v2) { v1.normalize(); v2.subtract(Vector3.projectAndCreate(v2, v1)); v2.normalize(); }
/** * Multiplies this {@link Quaternion} by a {@link Vector3}. * Note that if you plan on using the returned {@link Vector3}, * you should clone it immediately as it is an internal scratch * member of this {@link Quaternion} and may be modified at any * time. This is the same as out = q*vector*`q, meaning the magnitude * will be maintained. * * @param vector {@link Vector3} to multiply by. * * @return {@link Vector3} The result of the multiplication. */ @NonNull public Vector3 multiply(@NonNull Vector3 vector) { mTmpVec3.setAll(x, y, z); mTmpVec1.crossAndSet(mTmpVec3, vector); mTmpVec2.crossAndSet(mTmpVec3, mTmpVec1); mTmpVec1.multiply(2.0 * w); mTmpVec2.multiply(2.0); mTmpVec1.add(mTmpVec2); mTmpVec1.add(vector); return mTmpVec1; }
/** * 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); }
/** * Add a Curve * * @param point1 * The first point * @param controlPoint * The control point * @param point2 * The second point */ public void addPoint(Vector3 point1, Vector3 controlPoint, Vector3 point2) { mPoint1 = point1; mControlPoint = controlPoint; mPoint2 = point2; mStartTangent.setAll(controlPoint).subtract(point1); mEndTangent.setAll(point2).subtract(controlPoint); }