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

The following examples show how to use com.jme3.math.FastMath#interpolateLinear() . 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: ParticleEmitter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void updateParticle(Particle p, float tpf, Vector3f min, Vector3f max){
    // applying gravity
    p.velocity.x -= gravity.x * tpf;
    p.velocity.y -= gravity.y * tpf;
    p.velocity.z -= gravity.z * tpf;
    temp.set(p.velocity).multLocal(tpf);
    p.position.addLocal(temp);

    // affecting color, size and angle
    float b = (p.startlife - p.life) / p.startlife;
    p.color.interpolateLocal(startColor, endColor, b);
    p.size = FastMath.interpolateLinear(b, startSize, endSize);
    p.angle += p.rotateSpeed * tpf;

    // Computing bounding volume
    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);

    if (!selectRandomImage) {
        p.imageIndex = (int) (b * imagesX * imagesY);
    }
}
 
Example 2
Source File: MyParticleEmitter.java    From OpenRTS with MIT License 6 votes vote down vote up
protected void updateParticle(Particle p, float tpf, Vector3f min, Vector3f max){
    // applying gravity
    p.velocity.x -= gravity.x * tpf;
    p.velocity.y -= gravity.y * tpf;
    p.velocity.z -= gravity.z * tpf;
    temp.set(p.velocity).multLocal(tpf);
    p.position.addLocal(temp);

    // affecting color, size and angle
    float b = (p.startlife - p.life) / p.startlife;
    p.color.interpolateLocal(startColor, endColor, b);
    p.size = FastMath.interpolateLinear(b, startSize, endSize);
    p.angle += p.rotateSpeed * tpf;

    // Computing bounding volume
    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);

    if (!selectRandomImage) {
        p.imageIndex = (int) (b * imagesX * imagesY);
    }
}
 
Example 3
Source File: ParticleEmitter.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void updateParticle(Particle p, float tpf, Vector3f min, Vector3f max){
    // applying gravity
    p.velocity.x -= gravity.x * tpf;
    p.velocity.y -= gravity.y * tpf;
    p.velocity.z -= gravity.z * tpf;
    temp.set(p.velocity).multLocal(tpf);
    p.position.addLocal(temp);

    // affecting color, size and angle
    float b = (p.startlife - p.life) / p.startlife;
    p.color.interpolate(startColor, endColor, b);
    p.size = FastMath.interpolateLinear(b, startSize, endSize);
    p.angle += p.rotateSpeed * tpf;

    // Computing bounding volume
    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);

    if (!selectRandomImage) {
        p.imageIndex = (int) (b * imagesX * imagesY);
    }
}
 
Example 4
Source File: FbxAnimCurve.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Retrieve the curve value at the given time.
 * If the curve has no data, 0 is returned.
 * If the time is outside the curve, then the closest value is returned.
 * If the time isn't on an exact keyframe, linear interpolation is used
 * to determine the value between the keyframes at the given time.
 * @param time The time to get the curve value at (in FBX time units).
 * @return The value at the given time.
 */
public float getValueAtTime(long time) {
    if (keyTimes.length == 0) {
        return 0;
    }
    
    // If the time is outside the range, 
    // we just return the closest value. (No extrapolation)
    if (time <= keyTimes[0]) {
        return keyValues[0];
    } else if (time >= keyTimes[keyTimes.length - 1]) {
        return keyValues[keyValues.length - 1];
    }
    
    

    int startFrame = 0;
    int endFrame = 1;
    int lastFrame = keyTimes.length - 1;
    
    for (int i = 0; i < lastFrame && keyTimes[i] < time; ++i) {
        startFrame = i;
        endFrame = i + 1;
    }
    
    long keyTime1    = keyTimes[startFrame];
    float keyValue1  = keyValues[startFrame];
    long keyTime2    = keyTimes[endFrame];
    float keyValue2  = keyValues[endFrame];
    
    if (keyTime2 == time) {
        return keyValue2;
    }
    
    long prevToNextDelta    = keyTime2 - keyTime1;
    long prevToCurrentDelta = time     - keyTime1;
    float lerpAmount = (float)prevToCurrentDelta / prevToNextDelta;
    
    return FastMath.interpolateLinear(lerpAmount, keyValue1, keyValue2);
}
 
Example 5
Source File: PositionTrack.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void onUpdate(float tpf) {
    if (spatial != null) {
        value = Math.min(time / initialDuration, 1.0f);
        Vector3f pos = FastMath.interpolateLinear(value, startPosition, endPosition);
        spatial.setLocalTranslation(pos);
    }
}
 
Example 6
Source File: PanelTweens.java    From Lemur with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void doInterpolate( double t ) {
    float value = FastMath.interpolateLinear((float)t, from, to);
    target.setAlpha(value);
}
 
Example 7
Source File: AnimationFactory.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Interpolates over the key frames for the given keyFrame array and the given type of transform
 * @param keyFrames the keyFrames array
 * @param type the type of transforms
 */
private void interpolate(Object[] keyFrames, Type type) {
    int i = 0;
    while (i < totalFrames) {
        //fetching the next keyFrame index transform in the array
        int key = getNextKeyFrame(i, keyFrames);
        if (key != -1) {
            //computing the frame span to interpolate over
            int span = key - i;
            //interating over the frames
            for (int j = i; j <= key; j++) {
                // computing interpolation value
                float val = (j - i) / (float) span;
                //interpolationg depending on the transform type
                switch (type) {
                    case Translation:
                        translations[j] = FastMath.interpolateLinear(val, (Vector3f) keyFrames[i], (Vector3f) keyFrames[key]);
                        break;
                    case Rotation:
                        Quaternion rot = new Quaternion();
                        rotations[j] = rot.slerp(((Rotation) keyFrames[i]).rotation, ((Rotation) keyFrames[key]).rotation, val);
                        break;
                    case Scale:
                        scales[j] = FastMath.interpolateLinear(val, (Vector3f) keyFrames[i], (Vector3f) keyFrames[key]);
                        break;
                }
            }
            //jumping to the next keyFrame
            i = key;
        } else {
            //No more key frame, filling the array with the last transform computed.
            for (int j = i; j < totalFrames; j++) {

                switch (type) {
                    case Translation:
                        translations[j] = ((Vector3f) keyFrames[i]).clone();
                        break;
                    case Rotation:
                        rotations[j] = ((Rotation) keyFrames[i]).rotation.clone();
                        break;
                    case Scale:
                        scales[j] = ((Vector3f) keyFrames[i]).clone();
                        break;
                }
            }
            //we're done
            i = totalFrames;
        }
    }
}
 
Example 8
Source File: AnimationFactory.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Interpolates over the key frames for the given keyFrame array and the given type of transform
 * @param keyFrames the keyFrames array
 * @param type the type of transforms
 */
private void interpolate(Object[] keyFrames, Type type) {
    int i = 0;
    while (i < totalFrames) {
        //fetching the next keyFrame index transform in the array
        int key = getNextKeyFrame(i, keyFrames);
        if (key != -1) {
            //computing the frame span to interpolate over
            int span = key - i;
            //interating over the frames
            for (int j = i; j <= key; j++) {
                // computing interpolation value
                float val = (float) (j - i) / (float) span;
                //interpolationg depending on the transform type
                switch (type) {
                    case Translation:
                        translations[j] = FastMath.interpolateLinear(val, (Vector3f) keyFrames[i], (Vector3f) keyFrames[key]);
                        break;
                    case Rotation:
                        Quaternion rot = new Quaternion();
                        rotations[j] = rot.slerp(((Rotation) keyFrames[i]).rotation, ((Rotation) keyFrames[key]).rotation, val);
                        break;
                    case Scale:
                        scales[j] = FastMath.interpolateLinear(val, (Vector3f) keyFrames[i], (Vector3f) keyFrames[key]);
                        break;
                }
            }
            //jumping to the next keyFrame
            i = key;
        } else {
            //No more key frame, filling the array witht he last transform computed.
            for (int j = i; j < totalFrames; j++) {

                switch (type) {
                    case Translation:
                        translations[j] = ((Vector3f) keyFrames[i]).clone();
                        break;
                    case Rotation:
                        rotations[j] = ((Quaternion) ((Rotation) keyFrames[i]).rotation).clone();
                        break;
                    case Scale:
                        scales[j] = ((Vector3f) keyFrames[i]).clone();
                        break;
                }
            }
            //we're done
            i = totalFrames;
        }
    }
}