0

I'm working on an OpenGL project which simply uses a 2D array to create a bezier curve (order is start point -> start control point -> end control point -> end):

GLfloat controlPointsGirl[numControlPointsGirl][3] = {
    {1.0,0.0,0.0},{1.0,-0.5,0.0},{1.0,0.0,0.0},{-1.0,0.5,0.0}
};  

And moves a character along that path with the following formula, taken from https://www.opengl.org/discussion_boards/showthread.php/136550-Moving-object-along-a-bezier-curve :

//Drawing, initializing, display refresh every timeInterval seconds, etc.

girlX = ((1 - time)*(1 - time)*(1 - time)*controlPointsGirl[0][0]
    + (3 + time*(1 - time)*(1 - time))* controlPointsGirl[1][0]
    + (3 * time*time*(1 - time))* controlPointsGirl[2][0]
    + time*time*time*controlPointsGirl[3][0])
    /10;

cout << girlX <<" ";

girlY = ((1 - time)*(1 - time)*(1 - time)*controlPointsGirl[0][1]
    + (3 + time*(1 - time)*(1 - time))* controlPointsGirl[1][1]
    + (3 * time*time*(1 - time))* controlPointsGirl[2][1]
    + time*time*time*controlPointsGirl[3][1])
    /10;

cout << girlY << endl;

time += timeInterval;

The problem is, this logic leads to very jerky motion, which also rapidly accelerates as soon as the clock exceeds 1 second. This obviously isn't ideal; I want the velocity of the girl's motion to be fixed, and ideally I also want to be able to choose a velocity at which the motion will be carried out. I've tried a lot of tweaks to this formula but am having trouble wrapping my head around the issue, how would I alter this code to achieve the results I'm looking for?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
MMMMMCK
  • 307
  • 3
  • 14
  • protip: cache some of those values because you are recomputing all those coefficients way too many times. `t=time,mt=1-time,t2=t*t,mt2=mt*mt,t3=t*t*t,mt3=mt*mt*mt` and now your code is easier to read *and* you only compute the coefficients once. Bonus points for computing `a=mt3,b=3*t*mt2,c=3*mt*t2,d=t3` and then using those in your girlX/girlY computation. – Mike 'Pomax' Kamermans Mar 16 '18 at 17:05

1 Answers1

1

Looking at the link to the formula you provided, you have a mistake in your equations. The second term uses 3 + time when it should be 3 * time

(3 * time*(1 - time)*(1 - time))* controlPointsGirl[1][0]

in both equations.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56