ios - Cocos2d-x Parallax with Accelerometer (How to stop smoothly when reaching the edges and when changing direction) -
i creating game has 3 layers of background. added ccparallaxnode , it's moved tilting device right, left, , down. using code move ccparallaxnode (accelerometer delegate method - didaccelerate):
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 update method:
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 nicely done, however, when reaching of 4 edges stops abruptly. also, when changing direction (eg. moving right moving left) abruptly.
question: how can smooth stop , smooth direction change (maybe little bouncing effect)? think related accelerometer data (when going fast must bounce longer should when going slow).
thanks in advance.
you need math smooth movements. try checking code here: http://www.nscodecenter.com/preguntas/10768/3d-parallax-con-accelerometer
Comments
Post a Comment