Jump to content
  • Advertisement
Sign in to follow this  
Nathan Drake

OpenGL Moving an object on the bezier curve

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

If you intended to correct an error in the post then please contact us.

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

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 this post

Link to post
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);
	pos.addMul4 (vvu, cp2);
	pos.addMul4 (vuu, cp3);
	pos.addMul4 (uuu, cp4);

	// 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);
	tang.addMul4 (vvu, cp2);
	tang.addMul4 (vuu, cp3);
	tang.addMul4 (uuu, cp4);

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!