XNA CatmullRom曲线

Bus*_*hes 2 c# xna curve catmull-rom-curve

我需要对我正在尝试的技术做一些澄清.我正在尝试将实体从A点移动到B点,但我不希望实体沿着直线行进.

例如,如果实体位于x:0,y:0并且我想要到达点x:50,y:0,我希望实体以曲线行进到目标,我会想象它的最大距离将离开是x:25 y:25所以它在X上朝向目标移动但是已经远离y上的目标.

我已经研究了几个选项,包括样条曲线,曲线,但我认为可以做的工作是CatmullRom曲线.我有点困惑如何使用它?我想知道每帧移动我的实体的位置,而不是函数返回的插值.我会很感激如何使用它.

如果有任何替代方法可能比我错过的更容易,我也很感激听到它们.

编辑:

为了说明我如何得到一条曲线:

Vector2 blah = Vector2.CatmullRom(
    StartPosition, 
    new Vector2(StartPosition.X + 5, StartPosition.Y + 5), 
    new Vector2(StartPosition.X + 10, StartPosition.Y + 5), 
    /*This is the end position*/ 
    new Vector2(StartPosition.X + 15, StartPosition.Y), 0.25f);
Run Code Online (Sandbox Code Playgroud)

这个想法最终是我在飞行中产生这些点,但我现在只是试图解决这个问题.

Cor*_*rey 5

正如您所注意到的,样条线会产生不同长度的线段.曲线越紧,分段越短.这对于显示目的来说很好,对于移动设备的路径生成不是那么有用.

要获得样条路径的恒定速度遍历的合理近似值,需要沿曲线段进行一些插值.由于您已经有一组线段(在返回的点对之间Vector2.CatmullRom()),因此您需要一种以恒定速度遍历这些线段的方法.

给定一组点和沿着定义为这些点之间的线的路径移动的总距离,以下(或多或少的伪)代码将找到沿路径具有特定距离的点:

Point2D WalkPath(Point2D[] path, double distance)
{
    Point curr = path[0];
    for (int i = 1; i < path.Length; ++i)
    {
        double dist = Distance(curr, path[i]);
        if (dist < distance)
            return Interpolate(curr, path[i], distance / dist;

        distance -= dist;
        curr = path[i];
    }
    return curr;
}
Run Code Online (Sandbox Code Playgroud)

您可以采取各种优化来加快速度,例如存储路径中每个点的路径距离,以便在步行操作期间更容易查找.随着路径变得越来越复杂,这变得越来越重要,但对于只有少数段的路径来说可能会过度杀伤.

编辑: 是我在JavaScript中使用此方法的一个示例.这是一个概念验证,所以不要过于批评代码:P

编辑:关于样条生成的更多信息
给定一组"结"点 - 曲线必须按顺序通过的点 - 曲线算法最明显的拟合是Catmull-Rom.缺点是CR需要两个额外的控制点,这些控制点可能很难自动生成.

前一阵子我在网上发现了一篇非常有用的文章(我找不到正确的归因),根据路径中各点的位置计算出一组控制点.这是计算控制点的方法的C#代码:

// Calculate control points for Point 'p1' using neighbour points
public static Point2D[] GetControlsPoints(Point2D p0, Point2D p1, Point2D p2, double tension = 0.5)
{
    // get length of lines [p0-p1] and [p1-p2]
    double d01 = Distance(p0, p1);
    double d12 = Distance(p1, p2);
    // calculate scaling factors as fractions of total
    double sa = tension * d01 / (d01 + d12);
    double sb = tension * d12 / (d01 + d12);
    // left control point
    double c1x = p1.X - sa * (p2.X - p0.X);
    double c1y = p1.Y - sa * (p2.Y - p0.Y);
    // right control point
    double c2x = p1.X + sb * (p2.X - p0.X);
    double c2y = p1.Y + sb * (p2.Y - p0.Y);
    // return control points
    return new Point2D[] { new Point2D(c1x, c1y), new Point2D(c2x, c2y) };
}
Run Code Online (Sandbox Code Playgroud)

tension参数调整控制点生成以改变曲线的紧密度.值越高,曲线越宽,曲线越窄,值越低.玩它,看看哪种价值最适合你.

给定一组'n'结(曲线上的点),我们可以生成一组控制点,用于生成结之间的曲线:

// Generate all control points for a set of knots
public static List<Point2D> GenerateControlPoints(List<Point2D> knots)
{
    if (knots == null || knots.Count < 3)
        return null;
    List<Point2D> res = new List<Point2D>();
    // First control point is same as first knot
    res.Add(knots.First());
    // generate control point pairs for each non-end knot 
    for (int i = 1; i < knots.Count - 1; ++i)
    {
        Point2D[] cps = GetControlsPoints(knots[i - 1], knots[i], knots[i+1]);
        res.AddRange(cps);
    }
    // Last control points is same as last knot
    res.Add(knots.Last());
    return res;
}
Run Code Online (Sandbox Code Playgroud)

所以现在你有了一系列2*(n-1)控制点,然后你可以用它们来生成结点之间的实际曲线段.

public static Point2D LinearInterp(Point2D p0, Point2D p1, double fraction)
{
    double ix = p0.X + (p1.X - p0.X) * fraction;
    double iy = p0.Y + (p1.Y - p0.Y) * fraction;
    return new Point2D(ix, iy);
}

public static Point2D BezierInterp(Point2D p0, Point2D p1, Point2D c0, Point2D c1, double fraction)
{
    // calculate first-derivative, lines containing end-points for 2nd derivative
    var t00 = LinearInterp(p0, c0, fraction);
    var t01 = LinearInterp(c0, c1, fraction);
    var t02 = LinearInterp(c1, p1, fraction);
    // calculate second-derivate, line tangent to curve
    var t10 = LinearInterp(t00, t01, fraction);
    var t11 = LinearInterp(t01, t02, fraction);
    // return third-derivate, point on curve
    return LinearInterp(t10, t11, fraction);
}

// generate multiple points per curve segment for entire path
public static List<Point2D> GenerateCurvePoints(List<Point2D> knots, List<Point2D> controls)
{
    List<Point2D> res = new List<Point2D>();
    // start curve at first knot
    res.Add(knots[0]);
    // process each curve segment
    for (int i = 0; i < knots.Count - 1; ++i)
    {
        // get knot points for this curve segment
        Point2D p0 = knots[i];
        Point2D p1 = knots[i + 1];
        // get control points for this curve segment
        Point2D c0 = controls[i * 2];
        Point2D c1 = controls[i * 2 + 1];
        // calculate 20 points along curve segment
        int steps = 20;
        for (int s = 1; s < steps; ++s)
        {
            double fraction = (double)s / steps;
            res.Add(BezierInterp(p0, p1, c0, c1, fraction));
        }
    }
    return res;
}
Run Code Online (Sandbox Code Playgroud)

一旦你在你的结上运行它,你现在有一组插值点,它们是可变距离,距离取决于线的曲率.从中可以迭代地运行原始的WalkPath方法,生成一组相距恒定的点,这些点以恒定的速度定义移动设备沿曲线的进展.

在路径中的任何一点移动设备的航向(大致)是两侧各点之间的角度.对于任何点n的路径之间的角度p[n-1]p[n+1]是方位角.

// get angle (in Radians) from p0 to p1
public static double AngleBetween(Point2D p0, Point2D p1)
{
    return Math.Atan2(p1.X - p0.X, p1.Y - p0.Y);
}
Run Code Online (Sandbox Code Playgroud)

我已经从我的代码中修改了上面的内容,因为我使用了很久以前编写的Point2D类,它具有许多功能 - 点算术,插值等 - 内置.我可能在翻译过程中添加了一些错误,但希望他们当你玩它时会很容易发现.

让我知道事情的后续.如果遇到任何特殊困难,我会看到我能做些什么来帮助你.