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

The following examples show how to use com.jme3.math.Vector3f#set() . 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: EmitterMeshFaceShape.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * This method fills the point with coordinates of randomly selected point on a random face.
 * The normal param is filled with selected face's normal.
 * @param store
 *        the variable to store with coordinates of randomly selected selected point on a random face
 * @param normal
 *        filled with selected face's normal
 */
@Override
public void getRandomPointAndNormal(Vector3f store, Vector3f normal) {
    int meshIndex = FastMath.nextRandomInt(0, vertices.size() - 1);
    // the index of the first vertex of a face (must be dividable by 3)
    int faceIndex = FastMath.nextRandomInt(0, vertices.get(meshIndex).size() / 3 - 1);
    int vertIndex = faceIndex * 3;
    // put the point somewhere between the first and the second vertex of a face
    float moveFactor = FastMath.nextRandomFloat();
    store.set(Vector3f.ZERO);
    store.addLocal(vertices.get(meshIndex).get(vertIndex));
    store.addLocal((vertices.get(meshIndex).get(vertIndex + 1).x - vertices.get(meshIndex).get(vertIndex).x) * moveFactor, (vertices.get(meshIndex).get(vertIndex + 1).y - vertices.get(meshIndex).get(vertIndex).y) * moveFactor, (vertices.get(meshIndex).get(vertIndex + 1).z - vertices.get(meshIndex).get(vertIndex).z) * moveFactor);
    // move the result towards the last face vertex
    moveFactor = FastMath.nextRandomFloat();
    store.addLocal((vertices.get(meshIndex).get(vertIndex + 2).x - store.x) * moveFactor, (vertices.get(meshIndex).get(vertIndex + 2).y - store.y) * moveFactor, (vertices.get(meshIndex).get(vertIndex + 2).z - store.z) * moveFactor);
    normal.set(normals.get(meshIndex).get(faceIndex));
}
 
Example 2
Source File: VRGuiManager.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Position the GUI to the given location.
 * @param pos the position of the GUI.
 * @param dir the rotation of the GUI.
 * @param tpf the time per frame.
 */
private void positionTo(Vector3f pos, Quaternion dir, float tpf) {

	if (environment != null){
		Vector3f guiPos = guiQuadNode.getLocalTranslation();
		guiPos.set(0f, 0f, guiDistance);
		dir.mult(guiPos, guiPos);
		guiPos.x += pos.x;
		guiPos.y += pos.y + environment.getVRHeightAdjustment();
		guiPos.z += pos.z;        
		if( guiPositioningElastic > 0f && posMode != VRGUIPositioningMode.MANUAL ) {
			// mix pos & dir with current pos & dir            
			guiPos.interpolateLocal(EoldPos, guiPos, Float.min(1f, tpf * guiPositioningElastic));
			EoldPos.set(guiPos);
		}
	} else {
		throw new IllegalStateException("VR GUI manager is not attached to any environment.");
	}
}
 
Example 3
Source File: Intersection.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static final void findMinMax(float x0, float x1, float x2, Vector3f minMax) {
    minMax.set(x0, x0, 0);
    if (x1 < minMax.x) {
        minMax.setX(x1);
    }
    if (x1 > minMax.y) {
        minMax.setY(x1);
    }
    if (x2 < minMax.x) {
        minMax.setX(x2);
    }
    if (x2 > minMax.y) {
        minMax.setY(x2);
    }
}
 
Example 4
Source File: BIHTree.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void setMinMax(BoundingBox bbox, boolean doMin, int axis, float value) {
    Vector3f min = bbox.getMin(null);
    Vector3f max = bbox.getMax(null);

    if (doMin) {
        min.set(axis, value);
    } else {
        max.set(axis, value);
    }

    bbox.setMinMax(min, max);
}
 
Example 5
Source File: BillboardControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Aligns this Billboard so that it points to the camera position.
 *
 * @param camera
 *            Camera
 */
private void rotateCameraAligned(Camera camera) {
    look.set(camera.getLocation()).subtractLocal(
            spatial.getWorldTranslation());
    // coopt left for our own purposes.
    Vector3f xzp = left;
    // The xzp vector is the projection of the look vector on the xz plane
    xzp.set(look.x, 0, look.z);

    // check for undefined rotation...
    if (xzp.equals(Vector3f.ZERO)) {
        return;
    }

    look.normalizeLocal();
    xzp.normalizeLocal();
    float cosp = look.dot(xzp);

    // compute the local orientation matrix for the billboard
    orient.set(0, 0, xzp.z);
    orient.set(0, 1, xzp.x * -look.y);
    orient.set(0, 2, xzp.x * cosp);
    orient.set(1, 0, 0);
    orient.set(1, 1, cosp);
    orient.set(1, 2, look.y);
    orient.set(2, 0, -xzp.x);
    orient.set(2, 1, xzp.z * -look.y);
    orient.set(2, 2, xzp.z * cosp);

    // The billboard must be oriented to face the camera before it is
    // transformed into the world.
    spatial.setLocalRotation(orient);
    fixRefreshFlags();
}
 
Example 6
Source File: ParticleEmitter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Instantly emits available particles, up to num.
 */
public void emitParticles(int num) {
    // Force world transform to update
    this.getWorldTransform();

    TempVars vars = TempVars.get();

    BoundingBox bbox = (BoundingBox) this.getMesh().getBound();

    Vector3f min = vars.vect1;
    Vector3f max = vars.vect2;

    bbox.getMin(min);
    bbox.getMax(max);

    if (!Vector3f.isValidVector(min)) {
        min.set(Vector3f.POSITIVE_INFINITY);
    }
    if (!Vector3f.isValidVector(max)) {
        max.set(Vector3f.NEGATIVE_INFINITY);
    }

    for(int i=0;i<num;i++) {
        if( emitParticle(min, max) == null ) break;
    }

    bbox.setMinMax(min, max);
    this.setBoundRefresh();

    vars.release();
}
 
Example 7
Source File: EmitterMeshFaceShape.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * This method fills the point with coordinates of randomly selected point on a random face.
 * @param store
 *        the variable to store with coordinates of randomly selected selected point on a random face
 */
@Override
public void getRandomPoint(Vector3f store) {
    int meshIndex = FastMath.nextRandomInt(0, vertices.size() - 1);
    // the index of the first vertex of a face (must be dividable by 3)
    int vertIndex = FastMath.nextRandomInt(0, vertices.get(meshIndex).size() / 3 - 1) * 3;
    // put the point somewhere between the first and the second vertex of a face
    float moveFactor = FastMath.nextRandomFloat();
    store.set(Vector3f.ZERO);
    store.addLocal(vertices.get(meshIndex).get(vertIndex));
    store.addLocal((vertices.get(meshIndex).get(vertIndex + 1).x - vertices.get(meshIndex).get(vertIndex).x) * moveFactor, (vertices.get(meshIndex).get(vertIndex + 1).y - vertices.get(meshIndex).get(vertIndex).y) * moveFactor, (vertices.get(meshIndex).get(vertIndex + 1).z - vertices.get(meshIndex).get(vertIndex).z) * moveFactor);
    // move the result towards the last face vertex
    moveFactor = FastMath.nextRandomFloat();
    store.addLocal((vertices.get(meshIndex).get(vertIndex + 2).x - store.x) * moveFactor, (vertices.get(meshIndex).get(vertIndex + 2).y - store.y) * moveFactor, (vertices.get(meshIndex).get(vertIndex + 2).z - store.z) * moveFactor);
}
 
Example 8
Source File: TangentBinormalGenerator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static TriangleData processTriangle(int[] index,
        Vector3f[] v, Vector2f[] t) {
    TempVars tmp = TempVars.get();
    try {
        Vector3f edge1 = tmp.vect1;
        Vector3f edge2 = tmp.vect2;
        Vector2f edge1uv = tmp.vect2d;
        Vector2f edge2uv = tmp.vect2d2;
        
        Vector3f tangent = tmp.vect3;
        Vector3f binormal = tmp.vect4;
        Vector3f normal = tmp.vect5;
        
        t[1].subtract(t[0], edge1uv);
        t[2].subtract(t[0], edge2uv);
        float det = edge1uv.x * edge2uv.y - edge1uv.y * edge2uv.x;
        
        boolean normalize = false;
        if (Math.abs(det) < ZERO_TOLERANCE) {
            log.log(Level.WARNING, "Colinear uv coordinates for triangle "
                    + "[{0}, {1}, {2}]; tex0 = [{3}, {4}], "
                    + "tex1 = [{5}, {6}], tex2 = [{7}, {8}]",
                    new Object[]{index[0], index[1], index[2],
                        t[0].x, t[0].y, t[1].x, t[1].y, t[2].x, t[2].y});
            det = 1;
            normalize = true;
        }
        
        v[1].subtract(v[0], edge1);
        v[2].subtract(v[0], edge2);
        
        tangent.set(edge1);
        tangent.normalizeLocal();
        binormal.set(edge2);
        binormal.normalizeLocal();
        
        if (Math.abs(Math.abs(tangent.dot(binormal)) - 1)
                < ZERO_TOLERANCE) {
            log.log(Level.WARNING, "Vertices are on the same line "
                    + "for triangle [{0}, {1}, {2}].",
                    new Object[]{index[0], index[1], index[2]});
        }
        
        float factor = 1 / det;
        tangent.x = (edge2uv.y * edge1.x - edge1uv.y * edge2.x) * factor;
        tangent.y = (edge2uv.y * edge1.y - edge1uv.y * edge2.y) * factor;
        tangent.z = (edge2uv.y * edge1.z - edge1uv.y * edge2.z) * factor;
        if (normalize) {
            tangent.normalizeLocal();
        }
        
        binormal.x = (edge1uv.x * edge2.x - edge2uv.x * edge1.x) * factor;
        binormal.y = (edge1uv.x * edge2.y - edge2uv.x * edge1.y) * factor;
        binormal.z = (edge1uv.x * edge2.z - edge2uv.x * edge1.z) * factor;
        if (normalize) {
            binormal.normalizeLocal();
        }
        
        tangent.cross(binormal, normal);
        normal.normalizeLocal();
        
        return new TriangleData(
                tangent.clone(),
                binormal.clone(),
                normal.clone());
    } finally {
        tmp.release();
    }
}
 
Example 9
Source File: SpringGridLayout.java    From Lemur with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected final void setMajor( Vector3f v, float f ) {
    v.set(mainAxis.index(), f);
}
 
Example 10
Source File: LODGeomap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public FloatBuffer[] writeTangentArray(FloatBuffer normalBuffer, FloatBuffer tangentStore, FloatBuffer binormalStore, FloatBuffer textureBuffer, Vector3f scale) {
        if (!isLoaded()) {
            throw new NullPointerException();
        }

        if (tangentStore != null) {
            if (tangentStore.remaining() < getWidth() * getHeight() * 3) {
                throw new BufferUnderflowException();
            }
        } else {
            tangentStore = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
        }
        tangentStore.rewind();

        if (binormalStore != null) {
            if (binormalStore.remaining() < getWidth() * getHeight() * 3) {
                throw new BufferUnderflowException();
            }
        } else {
            binormalStore = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
        }
        binormalStore.rewind();

        Vector3f normal = new Vector3f();
        Vector3f tangent = new Vector3f();
        Vector3f binormal = new Vector3f();
        /*Vector3f v1 = new Vector3f();
        Vector3f v2 = new Vector3f();
        Vector3f v3 = new Vector3f();
        Vector2f t1 = new Vector2f();
        Vector2f t2 = new Vector2f();
        Vector2f t3 = new Vector2f();*/

        for (int r = 0; r < getHeight(); r++) {
            for (int c = 0; c < getWidth(); c++) {
                
                int idx = (r * getWidth() + c) * 3;
                normal.set(normalBuffer.get(idx), normalBuffer.get(idx+1), normalBuffer.get(idx+2));
                tangent.set(normal.cross(new Vector3f(0,0,1)));
                binormal.set(new Vector3f(1,0,0).cross(normal));
                
                BufferUtils.setInBuffer(tangent.normalizeLocal(), tangentStore, (r * getWidth() + c)); // save the tangent
                BufferUtils.setInBuffer(binormal.normalizeLocal(), binormalStore, (r * getWidth() + c)); // save the binormal
            }
        }

/*        for (int r = 0; r < getHeight(); r++) {
            for (int c = 0; c < getWidth(); c++) {

                int texIdx = ((getHeight() - 1 - r) * getWidth() + c) * 2; // pull from the end
                int texIdxAbove = ((getHeight() - 1 - (r - 1)) * getWidth() + c) * 2; // pull from the end
                int texIdxNext = ((getHeight() - 1 - (r + 1)) * getWidth() + c) * 2; // pull from the end

                v1.set(c, getValue(c, r), r);
                t1.set(textureBuffer.get(texIdx), textureBuffer.get(texIdx + 1));

                // below
                if (r == getHeight()-1) { // last row
                    v3.set(c, getValue(c, r), r + 1);
                    float u = textureBuffer.get(texIdx) - textureBuffer.get(texIdxAbove);
                    u += textureBuffer.get(texIdx);
                    float v = textureBuffer.get(texIdx + 1) - textureBuffer.get(texIdxAbove + 1);
                    v += textureBuffer.get(texIdx + 1);
                    t3.set(u, v);
                } else {
                    v3.set(c, getValue(c, r + 1), r + 1);
                    t3.set(textureBuffer.get(texIdxNext), textureBuffer.get(texIdxNext + 1));
                }
                
                //right
                if (c == getWidth()-1) { // last column
                    v2.set(c + 1, getValue(c, r), r);
                    float u = textureBuffer.get(texIdx) - textureBuffer.get(texIdx - 2);
                    u += textureBuffer.get(texIdx);
                    float v = textureBuffer.get(texIdx + 1) - textureBuffer.get(texIdx - 1);
                    v += textureBuffer.get(texIdx - 1);
                    t2.set(u, v);
                } else {
                    v2.set(c + 1, getValue(c + 1, r), r); // one to the right
                    t2.set(textureBuffer.get(texIdx + 2), textureBuffer.get(texIdx + 3));
                }

                calculateTangent(new Vector3f[]{v1.mult(scale), v2.mult(scale), v3.mult(scale)}, new Vector2f[]{t1, t2, t3}, tangent, binormal);
                BufferUtils.setInBuffer(tangent, tangentStore, (r * getWidth() + c)); // save the tangent
                BufferUtils.setInBuffer(binormal, binormalStore, (r * getWidth() + c)); // save the binormal
            }
        }
        */
        return new FloatBuffer[]{tangentStore, binormalStore};
    }
 
Example 11
Source File: SilentTangentBinormalGenerator.java    From OpenRTS with MIT License 4 votes vote down vote up
public static TriangleData processTriangle(int[] index, Vector3f[] v, Vector2f[] t) {
		Vector3f edge1 = new Vector3f();
		Vector3f edge2 = new Vector3f();
		Vector2f edge1uv = new Vector2f();
		Vector2f edge2uv = new Vector2f();

		Vector3f tangent = new Vector3f();
		Vector3f binormal = new Vector3f();
		Vector3f normal = new Vector3f();

		t[1].subtract(t[0], edge1uv);
		t[2].subtract(t[0], edge2uv);
		float det = edge1uv.x * edge2uv.y - edge1uv.y * edge2uv.x;

		boolean normalize = false;
		if (Math.abs(det) < ZERO_TOLERANCE) {
//			log.log(Level.WARNING, "Colinear uv coordinates for triangle " + "[{0}, {1}, {2}]; tex0 = [{3}, {4}], " + "tex1 = [{5}, {6}], tex2 = [{7}, {8}]",
//					new Object[] { index[0], index[1], index[2], t[0].x, t[0].y, t[1].x, t[1].y, t[2].x, t[2].y });
			det = 1;
			normalize = true;
		}

		v[1].subtract(v[0], edge1);
		v[2].subtract(v[0], edge2);

		tangent.set(edge1);
		tangent.normalizeLocal();
		binormal.set(edge2);
		binormal.normalizeLocal();

		if (Math.abs(Math.abs(tangent.dot(binormal)) - 1) < ZERO_TOLERANCE) {
//			log.log(Level.WARNING, "Vertices are on the same line " + "for triangle [{0}, {1}, {2}].", new Object[] { index[0], index[1], index[2] });
		}

		float factor = 1 / det;
		tangent.x = (edge2uv.y * edge1.x - edge1uv.y * edge2.x) * factor;
		tangent.y = (edge2uv.y * edge1.y - edge1uv.y * edge2.y) * factor;
		tangent.z = (edge2uv.y * edge1.z - edge1uv.y * edge2.z) * factor;
		if (normalize) {
			tangent.normalizeLocal();
		}

		binormal.x = (edge1uv.x * edge2.x - edge2uv.x * edge1.x) * factor;
		binormal.y = (edge1uv.x * edge2.y - edge2uv.x * edge1.y) * factor;
		binormal.z = (edge1uv.x * edge2.z - edge2uv.x * edge1.z) * factor;
		if (normalize) {
			binormal.normalizeLocal();
		}

		tangent.cross(binormal, normal);
		normal.normalizeLocal();

		return new TriangleData(tangent, binormal, normal);
	}
 
Example 12
Source File: GeoMap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
     * Creates a normal array from the normal data in this Geomap
     *
     * @param store A preallocated FloatBuffer where to store the data (optional), size must be &gt;= getWidth()*getHeight()*3
     * @return store, or a new FloatBuffer if store is null
     *
     * @throws NullPointerException If isLoaded() or hasNormalmap() is false
     */
    public FloatBuffer writeNormalArray(FloatBuffer store, Vector3f scale) {
        
        if (store!=null){
            if (store.remaining() < getWidth()*getHeight()*3)
                throw new BufferUnderflowException();
        }else{
            store = BufferUtils.createFloatBuffer(getWidth()*getHeight()*3);
        }
        store.rewind();

        Vector3f oppositePoint = new Vector3f();
        Vector3f adjacentPoint = new Vector3f();
        Vector3f rootPoint = new Vector3f();
        Vector3f tempNorm = new Vector3f();
        int normalIndex = 0;

        for (int y = 0; y < getHeight(); y++) {
            for (int x = 0; x < getWidth(); x++) {
                rootPoint.set(x, getValue(x,y), y);
                if (y == getHeight() - 1) {
                    if (x == getWidth() - 1) {  // case #4 : last row, last col
                        // left cross up
//                            adj = normalIndex - getWidth();
//                            opp = normalIndex - 1;
                        adjacentPoint.set(x, getValue(x,y-1), y-1);
                        oppositePoint.set(x-1, getValue(x-1, y), y);
                    } else {                    // case #3 : last row, except for last col
                        // right cross up
//                            adj = normalIndex + 1;
//                            opp = normalIndex - getWidth();
                        adjacentPoint.set(x+1, getValue(x+1,y), y);
                        oppositePoint.set(x, getValue(x,y-1), y-1);
                    }
                } else {
                    if (x == getWidth() - 1) {  // case #2 : last column except for last row
                        // left cross down
                        adjacentPoint.set(x-1, getValue(x-1,y), y);
                        oppositePoint.set(x, getValue(x,y+1), y+1);
//                            adj = normalIndex - 1;
//                            opp = normalIndex + getWidth();
                    } else {                    // case #1 : most cases
                        // right cross down
                        adjacentPoint.set(x, getValue(x,y+1), y+1);
                        oppositePoint.set(x+1, getValue(x+1,y), y);
//                            adj = normalIndex + getWidth();
//                            opp = normalIndex + 1;
                    }
                }



                tempNorm.set(adjacentPoint).subtractLocal(rootPoint)
                        .crossLocal(oppositePoint.subtractLocal(rootPoint));
                tempNorm.multLocal(scale).normalizeLocal();
//                    store.put(tempNorm.x).put(tempNorm.y).put(tempNorm.z);
                BufferUtils.setInBuffer(tempNorm, store,
                        normalIndex);
                normalIndex++;
            }
        }

        return store;
    }
 
Example 13
Source File: TestTempVars.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static void methodThatUsesAllocation(){
    Vector3f vector = new Vector3f();
    vector.set(0.1f, 0.2f, 0.3f);
    sumCompute.addLocal(vector);
}
 
Example 14
Source File: EmitterPointShape.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void getRandomPoint(Vector3f store) {
    store.set(point);
}
 
Example 15
Source File: EmitterPointShape.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * This method fills the point with data.
 * It does not fill the normal.
 * @param store the variable to store the point data
 * @param normal not used in this class
 */
@Override
public void getRandomPointAndNormal(Vector3f store, Vector3f normal) {
    store.set(point);
}
 
Example 16
Source File: VehicleWheel.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Copy this wheel's physics-space location to the specified vector.
 *
 * @param store storage for the result (not null, modified)
 */
public void getWheelWorldLocation(final Vector3f store) {
    store.set(this.wheelWorldLocation);
}
 
Example 17
Source File: PhysicsSpace.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Copy the gravitational acceleration acting on newly-added bodies.
 *
 * @param gravity storage for the result (not null, modified)
 * @return acceleration (in the vector provided)
 */
public Vector3f getGravity(Vector3f gravity) {
    return gravity.set(this.gravity);
}
 
Example 18
Source File: AnimationFactory.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Adds a key frame for the given scale at the given keyFrame index
 * @param keyFrameIndex the index at which the keyFrame must be inserted
 * @param scale the scale to use for this keyFrame
 */
public void addKeyFrameScale(int keyFrameIndex, Vector3f scale) {
    Vector3f s = getScaleForFrame(keyFrameIndex);
    s.set(scale);
}
 
Example 19
Source File: AnimationFactory.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Adds a key frame for the given scale at the given keyFrame index
 * @param keyFrameIndex the index at which the keyFrame must be inserted
 * @param scale the scale to use for this keyFrame
 */
public void addKeyFrameScale(int keyFrameIndex, Vector3f scale) {
    Vector3f s = getScaleForFrame(keyFrameIndex);
    s.set(scale);
}
 
Example 20
Source File: EmitterPointShape.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * This method fills the point with data.
 * It does not fill the normal.
 * @param store the variable to store the point data
 * @param normal not used in this class
 */
@Override
public void getRandomPointAndNormal(Vector3f store, Vector3f normal) {
    store.set(point);
}