Drawing Bezier path in XNA for Windows Phone 7
I am very new to Windows Phone 7. I am wr开发者_运维百科iting a sample game in which I want to move the Image randomly.
While discussing with one of my friend, he told me to use Bezier path. I search in the net to understand Bezier path concept. It looks like it will suit for my solution. But I did not found any sample code which will do this.
Please help me to find sample.
A Bezier path is a valid solution to your problem, but might I suggest using a Catmull-Rom spline instead. It is far eaiser to implement, not least of all because XNA already includes a function for generating such a spline. It is also easier to use (each control point is also a point on the spline).
The function in question is Vector2.CatmullRom
(there are also versions for Vector3
and for floats in MathHelper
). You specify four points. The middle two of which are valid for your spline. If you need more than two points, simply cycle the inputs as you move (second becomes first, third becomes second, and so on). The amount
argument species the position that you want, along the path.
The Catmull-Rom spline is described here on Wikipedia.
And here is an interactive demo showing how the spline works.
For simple draw of bezier curve you can use this (for 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));
}
So, and in Update you have to put this:
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));
But I think, that easier way to get curve is to use Catmull-Rom spline, how Andrew Russell wrote.
精彩评论