cocos2d-x 4.0 The Road to Learning (12) Complex Movement (Bezier Bezier Curve)

Let's learn some more complicated sports today.

For example, if we want to get out of a smooth curved motion, we can use BezierTo and BezierBy.

This Bezier is a Bezier curve, you can search for basic knowledge.

ccBezierConfig has 3 parameters,

Vec2 controlPoint_1: curve point 1 (curve point 1)
Vec2 controlPoint_2: curve point 2 (curve point 2)
Vec2 endPosition: final position

What do the curve points 1 and 2 mean? Look at the explanatory diagram of Baidu Encyclopedia to understand.

If you still want to understand in depth why this curve is so curved, then study it carefully. Bezier curve formula calculation (I ca n’t understand)

I tested two cases, the start and end coordinates are the same, respectively (200, 400), (600, 400). But the two curve points are different, we can look at the effect.

Test code and effect for the first time:

    auto sprite1 = Sprite::create("s1.png");
    sprite1->setPosition(Vec2(200, 400));
    this->addChild(sprite1);    

    ccBezierConfig bz;
    bz.controlPoint_1 = Vec2(300, 500);
    bz.controlPoint_2 = Vec2(500, 300);
    bz.endPosition = Vec2(600, 400);
    auto move = BezierTo::create(3, bz);

The second test code and effect:

    auto sprite1 = Sprite::create("s1.png");
    sprite1->setPosition(Vec2(200, 400));
    this->addChild(sprite1);    

    ccBezierConfig bz;
    bz.controlPoint_1 = Vec2(200, 600);
    bz.controlPoint_2 = Vec2(600, 200);
    bz.endPosition = Vec2(600, 400);
    auto move = BezierTo::create(3, bz);

The above is BezierTo, the effect of BezierBy is completely different, because BezierBy moves based on the current position of the sprite. I will not give an example.

 

 

Published 104 original articles · Like8 · Visit 210,000+

Guess you like

Origin blog.csdn.net/sunnyboychina/article/details/105268585