Java Code Examples for com.jme3.math.Vector3f#dot()

The following examples show how to use com.jme3.math.Vector3f#dot() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SweepSphere.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private float collideWithVertex(Vector3f sCenter, Vector3f sVelocity,
                                    float velocitySquared, Vector3f vertex, float t) {
        // A = velocity * velocity
        // B = 2 * (velocity . (center - vertex));
        // C = ||vertex - center||^2 - 1f;

        temp1.set(sCenter).subtractLocal(vertex);
        float a = velocitySquared;
        float b = 2f * sVelocity.dot(temp1);
        float c = temp1.negateLocal().lengthSquared() - 1f;
        float newT = getLowestRoot(a, b, c, t);

//        float A = velocitySquared;
//        float B = sCenter.subtract(vertex).dot(sVelocity) * 2f;
//        float C = vertex.subtract(sCenter).lengthSquared() - 1f;
//
//        float newT = getLowestRoot(A, B, C, Float.MAX_VALUE);
//        if (newT > 1.0f)
//            newT = Float.NaN;
        
        return newT;
    }
 
Example 2
Source File: Slider.java    From Lemur with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  Returns the slider range value for the specified location
 *  in the slider's local coordinate system.  (For example,
 *  for world space location use slider.worldToLocal() first.)
 */
public double getValueForLocation( Vector3f loc ) {

    Vector3f relative = loc.subtract(range.getLocalTranslation());

    // Components always grow down from their location
    // so we'll invert y
    relative.y *= -1;
            
    Vector3f axisDir = axis.getDirection();
    double projection = relative.dot(axisDir);
    if( projection < 0 ) {
        if( axis == Axis.Y ) {
            return model.getMaximum();
        } else {
            return model.getMinimum();
        }
    }
    
    Vector3f rangeSize = range.getSize().clone();
     
    double rangeLength = rangeSize.dot(axisDir);
    projection = Math.min(projection, rangeLength);
    double part = projection / rangeLength;       
    double rangeDelta = model.getMaximum() - model.getMinimum();
    
    // For the y-axis, the slider is inverted from the direction
    // that the component's grow... so our part is backwards
    if( axis == Axis.Y ) {
        part = 1 - part;
    }
 
    return model.getMinimum() + rangeDelta * part;        
}
 
Example 3
Source File: SpotLight.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean intersectsBox(BoundingBox box, TempVars vars) {
    if (this.spotRange > 0f) {
        // Check spot range first.
        // Sphere v. box collision
        if (!Intersection.intersect(box, position, spotRange)) {
            return false;
        }
    }
    
    Vector3f otherCenter = box.getCenter();
    Vector3f radVect = vars.vect4;
    radVect.set(box.getXExtent(), box.getYExtent(), box.getZExtent());
    float otherRadiusSquared = radVect.lengthSquared();
    float otherRadius = FastMath.sqrt(otherRadiusSquared);
    
    // Check if sphere is within spot angle.
    // Cone v. sphere collision.
    Vector3f E = direction.mult(otherRadius * outerAngleSinRcp, vars.vect1);
    Vector3f U = position.subtract(E, vars.vect2);
    Vector3f D = otherCenter.subtract(U, vars.vect3);

    float dsqr = D.dot(D);
    float e = direction.dot(D);

    if (e > 0f && e * e >= dsqr * outerAngleCosSqr) {
        D = otherCenter.subtract(position, vars.vect3);
        dsqr = D.dot(D);
        e = -direction.dot(D);

        if (e > 0f && e * e >= dsqr * outerAngleSinSqr) {
            return dsqr <= otherRadiusSquared;
        } else {
            return true;
        }
    }
    
    return false;
}
 
Example 4
Source File: SpotLight.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean intersectsSphere(BoundingSphere sphere, TempVars vars) {
    if (this.spotRange > 0f) {
        // Check spot range first.
        // Sphere v. sphere collision
        if (!Intersection.intersect(sphere, position, spotRange)) {
            return false;
        }
    }

    float otherRadiusSquared = FastMath.sqr(sphere.getRadius());
    float otherRadius = sphere.getRadius();

    // Check if sphere is within spot angle.
    // Cone v. sphere collision.
    Vector3f E = direction.mult(otherRadius * outerAngleSinRcp, vars.vect1);
    Vector3f U = position.subtract(E, vars.vect2);
    Vector3f D = sphere.getCenter().subtract(U, vars.vect3);

    float dsqr = D.dot(D);
    float e = direction.dot(D);

    if (e > 0f && e * e >= dsqr * outerAngleCosSqr) {
        D = sphere.getCenter().subtract(position, vars.vect3);
        dsqr = D.dot(D);
        e = -direction.dot(D);

        if (e > 0f && e * e >= dsqr * outerAngleSinSqr) {
            return dsqr <= otherRadiusSquared;
        } else {
            return true;
        }
    }
    
    return false;
}
 
Example 5
Source File: TangentBinormalGenerator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static int parity(Vector3f n1, Vector3f n) {
    if (n1.dot(n) < 0) {
        return -1;
    } else {
        return 1;
    }

}
 
Example 6
Source File: SilentTangentBinormalGenerator.java    From OpenRTS with MIT License 5 votes vote down vote up
private static int parity(Vector3f n1, Vector3f n) {
	if (n1.dot(n) < 0) {
		return -1;
	} else {
		return 1;
	}

}
 
Example 7
Source File: TriangulatedTexture.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * This method returns an envelope of a minimal rectangle, that is set
 * in 3D space, and contains the given triangle.
 * 
 * @param triangle
 *            the triangle
 * @return a rectangle minimum and maximum point and height and width
 */
private RectangleEnvelope getTriangleEnvelope(Vector3f v1, Vector3f v2, Vector3f v3) {
    Vector3f h = v3.subtract(v1);// the height of the resulting rectangle
    Vector3f temp = v2.subtract(v1);

    float field = 0.5f * h.cross(temp).length();// the field of the rectangle: Field = 0.5 * ||h x temp||
    if (field <= 0.0f) {
        return new RectangleEnvelope(v1);// return single point envelope
    }

    float cosAlpha = h.dot(temp) / (h.length() * temp.length());// the cosinus of angle betweenh and temp

    float triangleHeight = 2 * field / h.length();// the base of the height is the h vector
    // now calculate the distance between v1 vertex and the point where
    // the above calculated height 'touches' the base line (it can be
    // settled outside the h vector)
    float x = Math.abs((float) Math.sqrt(FastMath.clamp(temp.lengthSquared() - triangleHeight * triangleHeight, 0, Float.MAX_VALUE))) * Math.signum(cosAlpha);
    // now get the height base point
    Vector3f xPoint = v1.add(h.normalize().multLocal(x));

    // get the minimum point of the envelope
    Vector3f min = x < 0 ? xPoint : v1;
    if (x < 0) {
        h = v3.subtract(min);
    } else if (x > h.length()) {
        h = xPoint.subtract(min);
    }

    Vector3f envelopeWidth = v2.subtract(xPoint);
    return new RectangleEnvelope(min, envelopeWidth, h);
}
 
Example 8
Source File: CurvesHelper.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * The method transforms the bevel along the curve.
 * 
 * @param bevel
 *            the bevel to be transformed
 * @param prevPos
 *            previous curve point
 * @param currPos
 *            current curve point (here the center of the new bevel will be
 *            set)
 * @param nextPos
 *            next curve point
 * @return points of transformed bevel
 */
private Vector3f[] transformBevel(Vector3f[] bevel, Vector3f prevPos, Vector3f currPos, Vector3f nextPos) {
    bevel = bevel.clone();

    // currPos and directionVector define the line in 3D space
    Vector3f directionVector = prevPos != null ? currPos.subtract(prevPos) : nextPos.subtract(currPos);
    directionVector.normalizeLocal();

    // plane is described by equation: Ax + By + Cz + D = 0 where planeNormal = [A, B, C] and D = -(Ax + By + Cz)
    Vector3f planeNormal = null;
    if (prevPos != null) {
        planeNormal = currPos.subtract(prevPos).normalizeLocal();
        if (nextPos != null) {
            planeNormal.addLocal(nextPos.subtract(currPos).normalizeLocal()).normalizeLocal();
        }
    } else {
        planeNormal = nextPos.subtract(currPos).normalizeLocal();
    }
    float D = -planeNormal.dot(currPos);// D = -(Ax + By + Cz)

    // now we need to compute paralell cast of each bevel point on the plane, the leading line is already known
    // parametric equation of a line: x = px + vx * t; y = py + vy * t; z = pz + vz * t
    // where p = currPos and v = directionVector
    // using x, y and z in plane equation we get value of 't' that will allow us to compute the point where plane and line cross
    float temp = planeNormal.dot(directionVector);
    for (int i = 0; i < bevel.length; ++i) {
        float t = -(planeNormal.dot(bevel[i]) + D) / temp;
        if (fixUpAxis) {
            bevel[i] = new Vector3f(bevel[i].x + directionVector.x * t, bevel[i].y + directionVector.y * t, bevel[i].z + directionVector.z * t);
        } else {
            bevel[i] = new Vector3f(bevel[i].x + directionVector.x * t, -bevel[i].z + directionVector.z * t, bevel[i].y + directionVector.y * t);
        }
    }
    return bevel;
}
 
Example 9
Source File: PhysicsHoverControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void prePhysicsTick(PhysicsSpace space, float f) {
    Vector3f angVel = getAngularVelocity();
    float rotationVelocity = angVel.getY();
    Vector3f dir = getForwardVector(tempVect2).multLocal(1, 0, 1).normalizeLocal();
    getLinearVelocity(tempVect3);
    Vector3f linearVelocity = tempVect3.multLocal(1, 0, 1);

    if (steeringValue != 0) {
        if (rotationVelocity < 1 && rotationVelocity > -1) {
            applyTorque(tempVect1.set(0, steeringValue, 0));
        }
    } else {
        // counter the steering value!
        if (rotationVelocity > 0.2f) {
            applyTorque(tempVect1.set(0, -mass * 20, 0));
        } else if (rotationVelocity < -0.2f) {
            applyTorque(tempVect1.set(0, mass * 20, 0));
        }
    }
    if (accelerationValue > 0) {
        // counter force that will adjust velocity
        // if we are not going where we want to go.
        // this will prevent "drifting" and thus improve control
        // of the vehicle
        float d = dir.dot(linearVelocity.normalize());
        Vector3f counter = dir.project(linearVelocity).normalizeLocal().negateLocal().multLocal(1 - d);
        applyForce(counter.multLocal(mass * 10), Vector3f.ZERO);

        if (linearVelocity.length() < 30) {
            applyForce(dir.multLocal(accelerationValue), Vector3f.ZERO);
        }
    } else {
        // counter the acceleration value
        if (linearVelocity.length() > FastMath.ZERO_TOLERANCE) {
            linearVelocity.normalizeLocal().negateLocal();
            applyForce(linearVelocity.mult(mass * 10), Vector3f.ZERO);
        }
    }
}
 
Example 10
Source File: SweepSphere.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private float collideWithSegment(Vector3f sCenter,
                                 Vector3f sVelocity,
                                 float velocitySquared,
                                 Vector3f l1,
                                 Vector3f l2,
                                 float t,
                                 Vector3f store) {
    Vector3f edge = temp1.set(l2).subtractLocal(l1);
    Vector3f base = temp2.set(l1).subtractLocal(sCenter);

    float edgeSquared = edge.lengthSquared();
    float baseSquared = base.lengthSquared();

    float EdotV = edge.dot(sVelocity);
    float EdotB = edge.dot(base);

    float a = (edgeSquared * -velocitySquared) + EdotV * EdotV;
    float b = (edgeSquared * 2f * sVelocity.dot(base))
            - (2f * EdotV * EdotB);
    float c = (edgeSquared * (1f - baseSquared)) + EdotB * EdotB;
    float newT = getLowestRoot(a, b, c, t);
    if (!Float.isNaN(newT)){
        float f = (EdotV * newT - EdotB) / edgeSquared;
        if (f >= 0f && f < 1f){
            store.scaleAdd(f, edge, l1);
            return newT;
        }
    }
    return Float.NaN;
}
 
Example 11
Source File: PhysicsHoverControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void prePhysicsTick(PhysicsSpace space, float f) {
    Vector3f angVel = getAngularVelocity();
    float rotationVelocity = angVel.getY();
    Vector3f dir = getForwardVector(tempVect2).multLocal(1, 0, 1).normalizeLocal();
    getLinearVelocity(tempVect3);
    Vector3f linearVelocity = tempVect3.multLocal(1, 0, 1);
    float groundSpeed = linearVelocity.length();

    if (steeringValue != 0) {
        if (rotationVelocity < 1 && rotationVelocity > -1) {
            applyTorque(tempVect1.set(0, steeringValue, 0));
        }
    } else {
        // counter the steering value!
        if (rotationVelocity > 0.2f) {
            applyTorque(tempVect1.set(0, -mass * 20, 0));
        } else if (rotationVelocity < -0.2f) {
            applyTorque(tempVect1.set(0, mass * 20, 0));
        }
    }
    if (accelerationValue > 0) {
        // counter force that will adjust velocity
        // if we are not going where we want to go.
        // this will prevent "drifting" and thus improve control
        // of the vehicle
        if (groundSpeed > FastMath.ZERO_TOLERANCE) {
            float d = dir.dot(linearVelocity.normalize());
            Vector3f counter = dir.project(linearVelocity).normalizeLocal().negateLocal().multLocal(1 - d);
            applyForce(counter.multLocal(mass * 10), Vector3f.ZERO);
        }

        if (linearVelocity.length() < 30) {
            applyForce(dir.multLocal(accelerationValue), Vector3f.ZERO);
        }
    } else {
        // counter the acceleration value
        if (groundSpeed > FastMath.ZERO_TOLERANCE) {
            linearVelocity.normalizeLocal().negateLocal();
            applyForce(linearVelocity.mult(mass * 10), Vector3f.ZERO);
        }
    }
}
 
Example 12
Source File: MikktspaceTangentGenerator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static TSpace evalTspace(int face_indices[], final int iFaces, final int piTriListIn[], final TriInfo pTriInfos[],
        final MikkTSpaceContext mikkTSpace, final int iVertexRepresentitive) {
    TSpace res = new TSpace();
    float fAngleSum = 0;        

    for (int face = 0; face < iFaces; face++) {
        final int f = face_indices[face];

        // only valid triangles get to add their contribution
        if ((pTriInfos[f].flag & GROUP_WITH_ANY) == 0) {
            
            int i = -1;
            if (piTriListIn[3 * f + 0] == iVertexRepresentitive) {
                i = 0;
            } else if (piTriListIn[3 * f + 1] == iVertexRepresentitive) {
                i = 1;
            } else if (piTriListIn[3 * f + 2] == iVertexRepresentitive) {
                i = 2;
            }
            assert (i >= 0 && i < 3);

            // project
            int index = piTriListIn[3 * f + i];
            Vector3f n = getNormal(mikkTSpace, index);
            Vector3f vOs = pTriInfos[f].os.subtract(n.mult(n.dot(pTriInfos[f].os)));
            Vector3f vOt = pTriInfos[f].ot.subtract(n.mult(n.dot(pTriInfos[f].ot)));
            vOs.normalizeLocal();
            vOt.normalizeLocal();

            int i2 = piTriListIn[3 * f + (i < 2 ? (i + 1) : 0)];
            int i1 = piTriListIn[3 * f + i];
            int i0 = piTriListIn[3 * f + (i > 0 ? (i - 1) : 2)];

            Vector3f p0 = getPosition(mikkTSpace, i0);
            Vector3f p1 = getPosition(mikkTSpace, i1);
            Vector3f  p2 = getPosition(mikkTSpace, i2);
            Vector3f v1 = p0.subtract(p1);
            Vector3f v2 = p2.subtract(p1);

            // project
            v1.subtractLocal(n.mult(n.dot(v1))).normalizeLocal();
            v2.subtractLocal(n.mult(n.dot(v2))).normalizeLocal();

            // weight contribution by the angle
            // between the two edge vectors
            float fCos = v1.dot(v2);
            fCos = fCos > 1 ? 1 : (fCos < (-1) ? (-1) : fCos);
            float fAngle = (float) Math.acos(fCos);
            float fMagS = pTriInfos[f].magS;
            float fMagT = pTriInfos[f].magT;

            res.os.addLocal(vOs.multLocal(fAngle));
            res.ot.addLocal(vOt.multLocal(fAngle));
            res.magS += (fAngle * fMagS);
            res.magT += (fAngle * fMagT);
            fAngleSum += fAngle;
        }
    }

    // normalize
    res.os.normalizeLocal();
    res.ot.normalizeLocal();

    if (fAngleSum > 0) {
        res.magS /= fAngleSum;
        res.magT /= fAngleSum;
    }

    return res;
}