# OpenGL Moving an object on the bezier curve

This topic is 1063 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have written a code for drawing a bezier curve in OpenGL 2.

for example this is my bezier curve that i have drawn:

i can setup my bezier by glMap1f() using my controlPoints and number of controlPoints (which is uOrder)

void MyBezierLine::setup(const GLfloat* controlPoints,
GLint uOrder)
{
controlpoints = controlPoints;
uorder = uOrder;
//setup the bezier
glMap1f(GL_MAP1_VERTEX_3, //to be generated data
0.0f, //lower u range
1.0f, //higher u range
3, //u control point stride in array
uorder, //order of u or number of u control point
controlpoints); //the control points array
//enabling bezier evaluator
glEnable(GL_MAP1_VERTEX_3);
}


and i can simply draw them by glMapGrid1f() and glEvalMesh1().

void MyBezierLine::draw(GLenum draw_mode, //GL_LINE or GL_POINT
GLint ures)
{
//setting the 1D grid map containing ures points
//map to u range 0.0 - 1.0
glMapGrid1f(ures, 0.0f, 1.0f);
//evaluate the bezier surface
glEvalMesh1(draw_mode, 0, ures);
}


what i want to do is try to move an object along this bezier curve which has drawn by purple color.

based on what i have searched over internet, it looks like i have to write the function and  calculate the bezier curve by myself to get the position of each point on the line. despite of that, i have to evaluate the rotation of the object based on the position of each point.

do you have any proper way or idea about how can i do this ?

##### Share on other sites
You can calculate the tangent along to the position, like code below.
Still missing a binormal to bulid an orientation, i usualy set a start and end rotation for the start / end points,
slerp between those by t, and finaly slerp that to the tangent.

inline void EvaluateBezierLine4 (hkVector4 &pos, hkVector4 &tang, hkVector4 &cp1, hkVector4 &cp2, hkVector4 &cp3, hkVector4 &cp4, hkSimdReal t)
{
hkSimdReal u = 1.0f - t;

hkSimdReal vvv = u*u*u;
hkSimdReal vvu = u*u*t*3;
hkSimdReal vuu = u*t*t*3;
hkSimdReal uuu = t*t*t;

pos.setMul4 (vvv, cp1);

// tangent...
vvv = -3*u*u; // opt
vvu = -6*u*t + 3*u*u;
vuu =  6*u*t - 3*t*t;
uuu =  3*t*t;

tang.setMul4 (vvv, cp1);
}


• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 27
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634098
• Total Posts
3015525
×