Bezier Curve functions for cocos2d

I needed to draw a curve tonight in cocos2d. The Primitives file has a few basic OpenGL based drawing methods, but no curves. So I created these two methods:

[c]
#import
#import
#import
#import

void drawQuadBezier(CGPoint origin, CGPoint control, CGPoint destination, int segments)
{
CGPoint vertices[segments + 1];

float t = 0.0;
for(int i = 0; i < segments; i++) { float x = pow(1 - t, 2) * origin.x + 2.0 * (1 - t) * t * control.x + t * t * destination.x; float y = pow(1 - t, 2) * origin.y + 2.0 * (1 - t) * t * control.y + t * t * destination.y; vertices[i] = CGPointMake(x, y); t += 1.0 / segments; } vertices[segments] = destination; glVertexPointer(2, GL_FLOAT, 0, vertices); glEnableClientState(GL_VERTEX_ARRAY); glDrawArrays(GL_LINE_STRIP, 0, segments); glDisableClientState(GL_VERTEX_ARRAY); } void drawCubicBezier(CGPoint origin, CGPoint control1, CGPoint control2, CGPoint destination, int segments) { CGPoint vertices[segments + 1]; float t = 0.0; for(int i = 0; i < segments; i++) { float x = pow(1 - t, 3) * origin.x + 3.0 * pow(1 - t, 2) * t * control1.x + 3.0 * (1 - t) * t * t * control2.x + t * t * t * destination.x; float y = pow(1 - t, 3) * origin.y + 3.0 * pow(1 - t, 2) * t * control1.y + 3.0 * (1 - t) * t * t * control2.y + t * t * t * destination.y; vertices[i] = CGPointMake(x, y); t += 1.0 / segments; } vertices[segments] = destination; glVertexPointer(2, GL_FLOAT, 0, vertices); glEnableClientState(GL_VERTEX_ARRAY); glDrawArrays(GL_LINE_STRIP, 0, segments); glDisableClientState(GL_VERTEX_ARRAY); }[/c] Been trying to post these on the mailing list, but keep getting rejected with the message that this list contains spam or malware. Anyone know what's up with that?

This entry was posted in iPhone, Objective C. Bookmark the permalink.

4 Responses to Bezier Curve functions for cocos2d

  1. gropapa says:

    hi there! this is awesome. I plan on developping for iphone (but i first need to buy a mac ;((
    For non mathematicians like me this is really great.
    I suppose “t” can be changed if i want an object to follow a path (i guess…)
    In france iphone develoment seems to rock this time (as3 dev as well), it s worth having a glance about it.
    BTW…thanks 🙂

  2. memo says:

    Hey, you’re posts on cocoa are great and very useful, and cocos2d looks really cool too. But I thought I’d let you know (in case you didn’t know and might find it interesting), that you can develop for iPhone using openFrameworks and C++ (syntax much closer to AS3 than ObjectiveC). http://www.openframeworks.cc/download

    An example of developing using C++/openframeworks for iphone can be found at http://www.memo.tv/openframeworks_on_iphone_sample_1

    And also for openframeworks there are many addons, including some to wrap up opengl commands and also handle or draw splines.
    http://www.memo.tv/ofxmsaspline
    http://www.memo.tv/ofxmsashape3d
    http://addons.openframeworks.cc/

    P.S. and I’m not just posting this to plug my own site 😛 I really do think many people wanting to develop on iPhone can greatly benefit from this! And since openframeworks and ofxiPhone are opensource, hopefully contribute and develop it even further.

  3. Gilles says:

    glDrawArrays(GL_LINE_STRIP, 0, segments); should be glDrawArrays(GL_LINE_STRIP, 0, segments+1);

    otherwise the destination point is never displayed (it tends to it if “segments” is large though).

  4. @Gilles

    Wow!!!

    For so long I have been trying to debug that problem, and that never occured to me!
    Thanks Kieth for the equations, and thanks Gilles for the keen eye.

    It really made a big difference, i was using 25 segments because i couldn’t get the object to line up perfectly, so it was causing some serious slowdown.

Leave a Reply