Just found out that I can't use my custom class the way I have it set up here (that's what I get for typing it up in notepad), so instead here's a new snippet:
public Point CurrentPoint(float t, Point p0, Point p1, Point p2, Point p3)
{
float cube = t * t * t;
float square = t * t;
float ax = 3 * (p1.X - p0.X);
float ay = 3 * (p1.Y - p0.Y);
float bx = 3 * (p2.X - p1.X) - ax;
float by = 3 * (p2.Y - p1.Y) - ay;
float cx = p3.X - p0.X - ax - bx;
float cy = p3.Y - p0.Y - ay - by;
float x = (cx * cube) + (bx * square) + (ax * t) + p0.X;
float y = (cy * cube) + (by * square) + (ay * t) + p0.Y;
return new Point(x, y);
}
The implementation is pretty much the same:
Point ObjectPoint;
Point Endpoint1 = new Point(200, 80);
Point ControlPoint1 = new Point(180, 160);
Point ControlPoint2 = new Point(90, 300);
Point EndPoint2 = new Point(312, 115);
// Forms Apps:
float t = 0.0f;
private void timer1_Tick(object sender, EventArgs e)
{
t += 0.01f
if (t > 1.0f)
t = 0.0f;
ObjectPoint = CurrentPoint(t, Endpoint1, ControlPoint1, ControlPoint2, EndPoint2);
}
// XNA:
TimeSpan MoveTime = TimeSpan.FromMilliseconds(50);
TimeSpan LastMove;
float t = 0.0f;
protected override void Update(GameTime gameTime)
{
if (gameTime.TotalGameTime - LastMove > MoveTime)
{
LastMove = gameTime.TotalGameTime;
t += 0.01f;
if (t > 1.0f)
t = 0.0f;
ObjectPoint = CurrentPoint(t, EndPoint1, ControlPoint1, ControlPoint2, EndPoint2);
}
}
When using XNA just change all the Point variable types to Vector2 variable types. The method I used in the original snippet will work with Vector2 types (not sure about Point types). Once again, enjoy!
Jamie