在XNA中为Windows Phone 7绘制贝塞尔路径

问题描述:

我对Windows Phone 7非常陌生。我正在编写一个示例游戏,我希望随机移动图像。在XNA中为Windows Phone 7绘制贝塞尔路径

在与我的一位朋友讨论时,他告诉我使用Bezier路径。我在网上搜索了解贝塞尔路径概念。它看起来会适合我的解决方案。但我没有找到任何可以做到这一点的示例代码。

请帮我找到样品。

Bezier路径是解决您的问题的有效方法,但我可能会建议使用Catmull-Rom样条代替。实施起来非常方便,尤其是因为XNA 已经包含了一个用于生成这样的样条函数的函数。它也更容易使用(每个控制点也是样条曲线上的一个点)。

有问题的功能是Vector2.CatmullRomVector3也有版本,MathHelper也有浮动版本)。你指定四点。其中的中间两个对您的样条有效。如果您需要两点以上的点数,只需在移动时循环输入(第二点变为第一点,第三点变为第二点,依此类推)。沿着路径,amount论点描述了你想要的位置。

The Catmull-Rom spline is described here on Wikipedia

here is an interactive demo showing how the spline works

贝塞尔曲线的简单的抽签,你可以使用这个(为cubic Bezier curve):那么,在更新

private Vector2 Bezier(int t, Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3) 
{ 
    var x = OrdinateX((float)t/100, p0, p1, p2, p3); 
    var y = OrdinateY((float)t/100, p0, p1, p2, p3); 

    return new Vector2(x, y); 
} 

private float OrdinateX(float t, Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3) 
{ 
    return (float)((Math.Pow((double)(1 - t), 3) * p0.Y) + (3 * Math.Pow((double)(1 - t), 2) * t * p1.X) + (3 * (1 - t) * (t * t) * p2.X) + ((t * t * t) * p3.X)); 
} 

private float OrdinateY(float t, Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3) 
{ 
    return (float)((Math.Pow((double)(1 - t), 3) * p0.Y) + (3 * Math.Pow((double)(1 - t), 2) * t * p1.Y) + (3 * (1 - t) * (t * t) * p2.Y) + ((t * t * t) * p3.Y)); 
} 

你必须把这个:

for (int t = 0; t <= 100; t++) 
    object.position = Bezier(t, new Vector(0, 0), new Vector(100, 100), new Vector(300,300), new Vector(0, 300)); 

但我认为,更容易获得曲线的方法是使用Catmull-Rom样条,Andrew Russell如何写。