How to interpolate a curve between 3 points

82 Views Asked by At

I have a particle emitter that moves.

The emitter is spraying particles at every frame, which I have used sub-frame interpolation to interpolate particles from one frame to another.

Since the emitter is moving per frame, the particle emission path is linear. That is to say that if I set velocity to 0 so that the particles form a path, I get a very jagged path, which is to be expected.

This is what the path looks like:

enter image description here

I would like to smooth out the path between three points which I can get. That is the current path point, and the 2 prior to it.

I tried using the following algorithm which is supposed to produce a Quadratic BSpline interpolated curve:

//Quadratic BSpline Interpolation
// t = position between 0.0-1.0
double t2 = t * t;

// Calculate coefficients
double a0 = 0.5 * emitposlastx - emitposx + 0.5 * emitposlastx2;
double a1 = emitposx - emitposlastx;
double a2 = 0.5 * (emitposlastx2 + emitposlastx);

// Interpolate x coordinate
posx = a0 * t2 + a1 * t + a2;

a0 = 0.5 * emitposlasty - emitposy + 0.5 * emitposlasty2;
a1 = emitposy - emitposlasty;
a2 = 0.5 * (emitposlasty2 + emitposlasty);

// Interpolate y coordinate
posy = a0 * t2 + a1 * t + a2;

a0 = 0.5 * emitposlastz - emitposz + 0.5 * emitposlastz2;
a1 = emitposz - emitposlastz;
a2 = 0.5 * (emitposlastz2 + emitposlastz);

// Interpolate z coordinate
posz = a0 * t2 + a1 * t + a2;

However, it produces a complete mess. My world-space is -1.0 to 1.0, with 0.0 being in the center.

Am I going down the wrong route? If this seems correct, perhaps I have a mistake elsewhere?

Is there a simpler / easier method to create a curved path given 3 points?

1

There are 1 best solutions below

0
lastchance On

I don't know where you got your expressions for a0, a1, a2 from. If you use LAGRANGE INTERPOLATION, with t=0, 1/2, 1 at points xn-2, xn-1, xn, respectively, then you get

x = t2(2xn-4xn-1+2xn-2) +t(-xn+4xn-1-3xn-2) +xn-2

Note that this will be continuous, but not continuously differentiable, at the end points. (It also assumes the points are roughly equal distance apart, since t=1/2 at the middle point.)