开发者

How to align an object to match path

I am animating an object along a spline path in OpenGL. I am using some code I found to generate the spline. Sadly, I don't yet understand the mechanics of it. I want my object to align to match the tangent of path so that it looks like its following the path.

Two questions.

How do I find a vector that is tangent to the path at a given point? Secondly, given that vector and a vector pointing up, how do rotate the object to align?

Here is my spline code.

public Spline(Vector Path[])
{
    ControlPoints = Path;

    // Flatten out the control points array.
    int len = ControlPoints.length;
    float[] controlsX = new float[len];
    float[] controlsY = new float[len];
    float[] controlsZ = new float[len];
    for (int i = 0; i < len; ++i)
    {
        controlsX[i] = ControlPoints[i].x;
        controlsY[i] = ControlPoints[i].y;
        controlsZ[i] = ControlPoints[i].z;
   }

    // Calculate the gamma values just once.
    final int n = ControlPoints.length - 1;
    float[] gamma = new float[n + 1];
    gamma[0] = 1.0f / 2.0f;
    for (int i = 1; i < n; ++i) gamma[i] = 1 / (4 - gamma[i - 1]);
    gamma[n] = 1 / (2 - gamma[n - 1]);

    // Calculate the cubic segments.
    cubicX = calcNaturalCubic(n, controls开发者_StackOverflow社区X, gamma);
    cubicY = calcNaturalCubic(n, controlsY, gamma);
    cubicZ = calcNaturalCubic(n, controlsZ, gamma);
}

private Cubic[] calcNaturalCubic(int n, float[] x, float[] gamma)
{
    float[] delta = new float[n + 1];
    delta[0] = 3 * (x[1] - x[0]) * gamma[0];
    for (int i = 1; i < n; ++i)
    {
        delta[i] = (3 * (x[i + 1] - x[i - 1]) - delta[i - 1]) * gamma[i];
    }
    delta[n] = (3 * (x[n] - x[n - 1])-delta[n - 1]) * gamma[n];

    float[] D = new float[n + 1];
    D[n] = delta[n];
    for (int i = n - 1; i >= 0; --i)
    {
        D[i] = delta[i] - gamma[i] * D[i + 1];
    }

    // Calculate the cubic segments.
    Cubic[] C = new Cubic[n];
    for (int i = 0; i < n; i++) {
        final float a = x[i];
        final float b = D[i];
        final float c = 3 * (x[i + 1] - x[i]) - 2 * D[i] - D[i + 1];
        final float d = 2 * (x[i] - x[i + 1]) + D[i] + D[i + 1];
        C[i] = new Cubic(a, b, c, d);
    }
    return C;
}

final public Vector GetPoint(float Position)
{
    if(Position >= 1) { return ControlPoints[ControlPoints.length - 1]; }
    float position = Position * cubicX.length;
    int splineIndex = (int)Math.floor(position);
    float splinePosition = position - splineIndex;
    return new Vector(cubicX[splineIndex].eval(splinePosition), cubicY[splineIndex].eval(splinePosition), cubicZ[splineIndex].eval(splinePosition));
}


First question:
If you keep track of the previous position of your object as well as the new position, you can find out the facing direction without finding the tangent of the spline, like so:

facing = newPosition - previousPosition

Second question:
You can use the answers described here for rotating an object to face a particular direction: What is the easiest way to align the Z axis with a vector?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜