Search code examples
c++cocos2d-xcurve

How to draw an arbitrary curve in cocos2dx


I'm working on a game based on cocos2dx, now I need to draw an arbitrary curve as the finger moves on the screen,then the curve will become the path along which my hero walks.Any idea will be appreciated.


Solution

  • Curve is connected line.
    so you can draw curve using connected line.

    Accumulate the points in cctouchesmove/cctouchemove event function.
    Make line sprite, add and draw them.

    and also cocos2d-x have curve classes. if you use them, you can run action easily.

    CCCardinalSplineTo
    CCCardinalSplineBy
    CCCatmullRomTo
    CCCatmullRomBy
    CCBezierBy
    CCBezierTo
    

    And for drawing, this is sample code.

    void HelloWorld::draw()  
    {  
        // move to 50,50 since the "by" path will start at 50,50  
        kmGLPushMatrix();
        kmGLTranslatef(50, 50, 0);
        ccDrawCardinalSpline(m_pArray, 7, 100);
    
        kmGLPopMatrix();
    
        CCSize s = CCDirector::sharedDirector()->getWinSize();  
    
        kmGLPushMatrix();  
        kmGLTranslatef(s.width/2, 50, 0);  
        ccDrawCardinalSpline(m_pArray, 1, 100);  
        kmGLPopMatrix();  
    }