Cocos2d-x Parallax with accelerometer (how to stop smoothly when reaching edges and when changing direction)

I am creating a game with three background layers. They are added to the CCParallaxNode and moved by tilting the device to the right, left, up and down. I am using this code to move the CCParallaxNode (Accelerometer delegate method - made by Accelerate ):

void SelectScreen::didAccelerate(cocos2d::CCAcceleration *pAccelerationValue)
{
    float deceleration = 0.1f, sensitivity = 30.0f, maxVelocity = 200;

    accelX = pAccelerationValue->x * sensitivity;
    accelY = pAccelerationValue->z * sensitivity;

    parallaxMovementX = parallaxMovementX * deceleration + pAccelerationValue->x * sensitivity;
    parallaxMovementX = fmaxf(fminf(parallaxMovementX, maxVelocity), -maxVelocity);

    float offset = -calibration * sensitivity;

    parallaxMovementY = (parallaxMovementY * deceleration + pAccelerationValue->z * sensitivity) + offset;
}

      

Then in the update :

void SelectScreen::update(float dt)
{
CCNode* node = getChildByTag(100);

    float maxX = (Data::getInstance()->getWinSize().width * 2) + 100;
    float minX = node->getContentSize().width - 100;

    float maxY = Data::getInstance()->getWinSize().height * 0.1f;
    float minY = -200;

    float diffX = parallaxMovementX;
    float diffY = parallaxMovementY;

    float newX = node->getPositionX() + diffX;
    float newY = node->getPositionY() + diffY;

    newX = MIN(MAX(newX, minX), maxX);

    newY = MIN(MAX(newY, minY), maxY);

    if(isUpdating)
        node->setPositionX(newX);
    if(isUpdatingY)
        node->setPositionY(newY);
}

      

The movement is nicely done, however, upon reaching any of the 4 ribs, it stops suddenly . Also, when there is a change in direction (eg, move to the right, move to the left), it is abrupt .

Question . How can I make a smooth stop and a smooth direction change (maybe some small bounce effect)? I think it also has to do with the accelerometer data (it should bounce longer when moving fast than slow moving).

Thanks in advance.

+3


source to share


1 answer


You need math to smooth out the movements. Try to check the code here: http://www.nscodecenter.com/preguntas/10768/3d-parallax-con-accelerometer



0


source







All Articles