Java Code Examples for com.jme3.math.FastMath#nextRandomFloat()

The following examples show how to use com.jme3.math.FastMath#nextRandomFloat() . 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: TestReverb.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void simpleUpdate(float tpf) {
  time += tpf;

  if (time > nextTime) {
    Vector3f v = new Vector3f();
    v.setX(FastMath.nextRandomFloat());
    v.setY(FastMath.nextRandomFloat());
    v.setZ(FastMath.nextRandomFloat());
    v.multLocal(40, 2, 40);
    v.subtractLocal(20, 1, 20);

    audioSource.setLocalTranslation(v);
    audioSource.playInstance();
    time = 0;
    nextTime = FastMath.nextRandomFloat() * 2 + 0.5f;
  }
}
 
Example 2
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 3
Source File: TestReverb.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void simpleUpdate(float tpf) {
  time += tpf;

  if (time > nextTime) {
    Vector3f v = new Vector3f();
    v.setX(FastMath.nextRandomFloat());
    v.setY(FastMath.nextRandomFloat());
    v.setZ(FastMath.nextRandomFloat());
    v.multLocal(40, 2, 40);
    v.subtractLocal(20, 1, 20);

    audioSource.setLocalTranslation(v);
    audioSource.playInstance();
    time = 0;
    nextTime = FastMath.nextRandomFloat() * 2 + 0.5f;
  }
}
 
Example 4
Source File: EmitterMeshFaceShape.java    From MikuMikuStudio with BSD 2-Clause "Simplified" 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 5
Source File: SimpleWanderBehavior.java    From MonkeyBrains with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @return A new random steer force
 */
protected Vector3f newRandomSteer() {
        float rX = (FastMath.nextRandomFloat() - 0.5f)*2 * maxRandSteer.x;
        float rY = (FastMath.nextRandomFloat() - 0.5f)*2 * maxRandSteer.y;
        float rZ = (FastMath.nextRandomFloat() - 0.5f)*2 * maxRandSteer.z; 
        return new Vector3f(rX, rY, rZ);
}
 
Example 6
Source File: WanderAreaBehavior.java    From MonkeyBrains with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Metod for changing target position.
 *
 * @param tpf time per frame
 */
protected void changeTargetPosition(float tpf) {
    time -= tpf;
    if (time <= 0) {
        float rOffsetX = (FastMath.nextRandomFloat() - 0.5f)*2 * this.offset.x;
        float rOffsetY = (FastMath.nextRandomFloat() - 0.5f)*2 * this.offset.y;
        float rOffsetZ = (FastMath.nextRandomFloat() - 0.5f)*2 * this.offset.z; 
        targetPosition = center.add(rOffsetX, rOffsetY, rOffsetZ);
        time = timeInterval;
    }
}
 
Example 7
Source File: ParticleEmitter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Particle emitParticle(Vector3f min, Vector3f max) {
    int idx = lastUsed + 1;
    if (idx >= particles.length) {
        return null;
    }

    Particle p = particles[idx];
    if (selectRandomImage) {
        p.imageIndex = FastMath.nextRandomInt(0, imagesY - 1) * imagesX + FastMath.nextRandomInt(0, imagesX - 1);
    }

    p.startlife = lowLife + FastMath.nextRandomFloat() * (highLife - lowLife);
    p.life = p.startlife;
    p.color.set(startColor);
    p.size = startSize;
    //shape.getRandomPoint(p.position);
    particleInfluencer.influenceParticle(p, shape);
    if (worldSpace) {
        worldTransform.transformVector(p.position, p.position);
        worldTransform.getRotation().mult(p.velocity, p.velocity);
        // TODO: Make scale relevant somehow??
    }
    if (randomAngle) {
        p.angle = FastMath.nextRandomFloat() * FastMath.TWO_PI;
    }
    if (rotateSpeed != 0) {
        p.rotateSpeed = rotateSpeed * (0.2f + (FastMath.nextRandomFloat() * 2f - 1f) * .8f);
    }

    temp.set(p.position).addLocal(p.size, p.size, p.size);
    max.maxLocal(temp);
    temp.set(p.position).subtractLocal(p.size, p.size, p.size);
    min.minLocal(temp);

    ++lastUsed;
    firstUnUsed = idx + 1;
    return p;
}
 
Example 8
Source File: EmitterSphereShape.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void getRandomPoint(Vector3f store) {
    do {
        store.x = (FastMath.nextRandomFloat() * 2f - 1f);
        store.y = (FastMath.nextRandomFloat() * 2f - 1f);
        store.z = (FastMath.nextRandomFloat() * 2f - 1f);
    } while (store.lengthSquared() > 1);
    store.multLocal(radius);
    store.addLocal(center);
}
 
Example 9
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 10
Source File: MyParticleEmitter.java    From OpenRTS with MIT License 5 votes vote down vote up
private Particle emitParticle(Vector3f min, Vector3f max) {
    int idx = lastUsed + 1;
    if (idx >= particles.length) {
        return null;
    }

    Particle p = particles[idx];
    if (selectRandomImage) {
        p.imageIndex = FastMath.nextRandomInt(0, imagesY - 1) * imagesX + FastMath.nextRandomInt(0, imagesX - 1);
    }

    p.startlife = lowLife + FastMath.nextRandomFloat() * (highLife - lowLife);
    p.life = p.startlife;
    p.color.set(startColor);
    p.size = startSize;
    //shape.getRandomPoint(p.position);
    particleInfluencer.influenceParticle(p, shape);
    if (worldSpace) {
        worldTransform.transformVector(p.position, p.position);
        worldTransform.getRotation().mult(p.velocity, p.velocity);
        // TODO: Make scale relevant somehow??
    }
    if (randomAngle) {
        p.angle = FastMath.nextRandomFloat() * FastMath.TWO_PI;
    }
    if (rotateSpeed != 0) {
        p.rotateSpeed = rotateSpeed * (0.2f + (FastMath.nextRandomFloat() * 2f - 1f) * .8f);
    }

    temp.set(p.position).addLocal(p.size, p.size, p.size);
    max.maxLocal(temp);
    temp.set(p.position).subtractLocal(p.size, p.size, p.size);
    min.minLocal(temp);

    ++lastUsed;
    firstUnUsed = idx + 1;
    return p;
}
 
Example 11
Source File: ParticleEmitter.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private Particle emitParticle(Vector3f min, Vector3f max) {
    int idx = lastUsed + 1;
    if (idx >= particles.length) {
        return null;
    }

    Particle p = particles[idx];
    if (selectRandomImage) {
        p.imageIndex = FastMath.nextRandomInt(0, imagesY - 1) * imagesX + FastMath.nextRandomInt(0, imagesX - 1);
    }

    p.startlife = lowLife + FastMath.nextRandomFloat() * (highLife - lowLife);
    p.life = p.startlife;
    p.color.set(startColor);
    p.size = startSize;
    //shape.getRandomPoint(p.position);
    particleInfluencer.influenceParticle(p, shape);
    if (worldSpace) {
        worldTransform.transformVector(p.position, p.position);
        worldTransform.getRotation().mult(p.velocity, p.velocity);
        // TODO: Make scale relevant somehow??
    }
    if (randomAngle) {
        p.angle = FastMath.nextRandomFloat() * FastMath.TWO_PI;
    }
    if (rotateSpeed != 0) {
        p.rotateSpeed = rotateSpeed * (0.2f + (FastMath.nextRandomFloat() * 2f - 1f) * .8f);
    }

    temp.set(p.position).addLocal(p.size, p.size, p.size);
    max.maxLocal(temp);
    temp.set(p.position).subtractLocal(p.size, p.size, p.size);
    min.minLocal(temp);

    ++lastUsed;
    firstUnUsed = idx + 1;
    return p;
}
 
Example 12
Source File: EmitterSphereShape.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void getRandomPoint(Vector3f store) {
    do {
        store.x = (FastMath.nextRandomFloat() * 2f - 1f) * radius;
        store.y = (FastMath.nextRandomFloat() * 2f - 1f) * radius;
        store.z = (FastMath.nextRandomFloat() * 2f - 1f) * radius;
    } while (store.distance(center) > radius);
}
 
Example 13
Source File: EmitterMeshFaceShape.java    From MikuMikuStudio with BSD 2-Clause "Simplified" 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 14
Source File: TestInstanceNode.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void simpleUpdate(float tpf) {
    time += tpf;

    if (time > 1f) {
        time = 0f;
        
        for (Spatial instance : instancedNode.getChildren()) {
            if (!(instance instanceof InstancedGeometry)) {
                Geometry geom = (Geometry) instance;
                geom.setMaterial(materials[FastMath.nextRandomInt(0, materials.length - 1)]);

                Mesh mesh; 
                if (FastMath.nextRandomInt(0, 1) == 1) mesh = mesh2;
                else mesh = mesh1;
                geom.setMesh(mesh);
            }
        }
    }
    
    for (Spatial child : instancedNode.getChildren()) {
        if (!(child instanceof InstancedGeometry)) {
            float val = ((Float)child.getUserData("height")).floatValue();
            float dir = ((Float)child.getUserData("dir")).floatValue();

            val += (dir + ((FastMath.nextRandomFloat() * 0.5f) - 0.25f)) * tpf;

            if (val > 1f) {
                val = 1f;
                dir = -dir;
            } else if (val < 0f) {
                val = 0f;
                dir = -dir;
            }

            Vector3f translation = child.getLocalTranslation();
            translation.y = (smoothstep(0, 1, val) * 2.5f) - 1.25f;

            child.setUserData("height", val);
            child.setUserData("dir", dir);

            child.setLocalTranslation(translation);
        }
    }
}
 
Example 15
Source File: EmitterBoxShape.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void getRandomPoint(Vector3f store) {
    store.x = min.x + len.x * FastMath.nextRandomFloat();
    store.y = min.y + len.y * FastMath.nextRandomFloat();
    store.z = min.z + len.z * FastMath.nextRandomFloat();
}
 
Example 16
Source File: EmitterBoxShape.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void getRandomPoint(Vector3f store) {
    store.x = min.x + len.x * FastMath.nextRandomFloat();
    store.y = min.y + len.y * FastMath.nextRandomFloat();
    store.z = min.z + len.z * FastMath.nextRandomFloat();
}