I’ve been playing with quadratic bezier curves recently, its easy enough to draw and get the points along it using something like:
float x = (1 - t) * (1 - t) * startX + 2 * (1 - t) * t * cx + t * t * endX;
float y = (1 - t) * (1 - t) * startY + 2 * (1 - t) * t * cy + t * t * endY;
Where start/end is the beginning/end point on the curve and c being the control point.
t is a value between 0 and 1 which can be used to move along the curve, 0 being the start and 1 being the end, however the problem is that this value can’t be used to move along the curve at a constant speed since t=0.5 is not necessarily at the middle of the curve or t=0.25 the first quarter.
I’ve got an approximation of the length of the curve using
public float getApproxLength() {
float t = 0;
length = 0; // reset length
float oldX = startX;
float oldY = startY;
// scroll through curve and update length
for (int i = 0; i < 100; i++) {
t += 0.01f;
float x = (1 - t) * (1 - t) * startX + 2 * (1 - t) * t * cx1 + t * t * endX;
float y = (1 - t) * (1 - t) * startY + 2 * (1 - t) * t * cy1 + t * t * endY;
length += getLineLength(x, y, oldX, oldY);
oldX = x;
oldY = y;
}
return length;
}
Are there any easy solutions to achieve this other than to use a whole ton of look up tables and memory? doesn’t need to be super accurate just something good enough to get a decent constant looking movement along the curve.