If you think the Android project Schooner-3D listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright 2012 Dan Mercer/*fromwww.java2s.com*/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package com.supermercerbros.gameengine.math;
/**
* Represents a piecewise Bezier curve, used for interpolation
*/publicclass BezierCurve implements Curve {
privatefinalfloat[] values;
privatefinalfloat[] times;
privatefinalint lastIndex;
public BezierCurve(float[] x, float[] y) {
if (x.length != y.length) {
thrownew IllegalArgumentException(
"x and y arrays must be of equal length.");
} elseif ((x.length - 1) % 3 != 0) {
thrownew IllegalArgumentException(
"(x.length - 1) % 3 must equal zero.");
}
times = x;
values = y;
lastIndex = times.length - 1;
}
/* (non-Javadoc)
* @see com.supermercerbros.gameengine.math.Curve#getInterpolation(float)
*/
@Override
publicfloat getInterpolation(float x) {
finalfloat frame = x * times[lastIndex];
if (x >= 1) {
return values[lastIndex];
} elseif (x <= 0) {
return values[0];
}
// Get index of lower keyframe
int keyframe = 0;
while (frame > times[(keyframe + 1) * 3]) {
keyframe++;
}
// Point frame coordinates
finalfloat fp0 = times[(keyframe * 3) + 0];
finalfloat fp1 = times[(keyframe * 3) + 1];
finalfloat fp2 = times[(keyframe * 3) + 2];
finalfloat fp3 = times[(keyframe * 3) + 3];
// Point value coordinates
finalfloat vp0 = values[(keyframe * 3) + 0];
finalfloat vp1 = values[(keyframe * 3) + 1];
finalfloat vp2 = values[(keyframe * 3) + 2];
finalfloat vp3 = values[(keyframe * 3) + 3];
// Estimate T given X
float lowerT = 0;
float upperT = 1;
float tGuess = (fp0 - frame) / (fp0 - fp3);
for (int i = 1; i <= 5; i++) {
finalfloat frameGuess = solve(fp0, fp1, fp2, fp3, tGuess);
if (frameGuess < frame) {
lowerT = tGuess;
} elseif (frameGuess > frame){
upperT = tGuess;
} else {
return solve(vp0, vp1, vp2, vp3, tGuess);
}
tGuess = (lowerT + upperT) / 2;
}
finalfloat lowerFrame = solve(fp0, fp1, fp2, fp3, lowerT);
finalfloat upperFrame = solve(fp0, fp1, fp2, fp3, upperT);
finalfloat alpha = (lowerFrame - frame) / (lowerFrame - upperFrame);
finalfloat t = lowerT + (upperT - lowerT) * alpha;
// Solve for Y now that we have an estimated T
return solve(vp0, vp1, vp2, vp3, t);
}
privatestaticfloat solve(finalfloat p0, finalfloat p1, finalfloat p2,
finalfloat p3, finalfloat t) {
if (t == 0.0) {
return p0;
} elseif (t == 1.0) {
return p3;
}
finalfloat d = 1 - t;
return (d * d * d * p0) +
(3 * d * d * t * p1) +
(3 * d * t * t * p2) +
(t * t * t * p3);
}
/* (non-Javadoc)
* @see com.supermercerbros.gameengine.math.Curve#getStartValue()
*/
@Override
publicfloat getStartValue() {
return values[0];
}
}