I am studying cocos2d-x engine, I’m not very familiar with this topic, so I’m not sure if I did everything right.

I move the sprite to 5 pixels 60 times per second and, if the FPS falls, then I want the sprite to move to the same 5 * 60 pixels in a second, no less and no more. Did I correct the movement adjustment?

void HelloWorld::update(float delta) { float newPosX = sprite->getPositionX() + (xMovement * 5 * delta / 0.016666); float newPosY = sprite->getPositionY() + (yMovement * 5 * delta / 0.016666); sprite->setPosition(newPosX, newPosY); } 

I checked it by inserting this code into the update function, it seems to work:

 if (RandomHelper::random_int(1, 100) == 5) Sleep(100); 

I know that I formulated the question badly, but I hope knowledgeable people will understand. If you have any idea how to do it differently, I’ll read it with pleasure.

The variables xMovement and yMovement have values ​​of -1, 0, 1, depending on which keys are pressed.

  • Judging by the code, delta in this code - is it the difference from the last frame? - VTT
  • Useful Fiction: Fix Your Timestep . - HolyBlackCat
  • @VTT I saw the code here and changed a bit - Mishakov Maxim
  • one
    No, the adjustment here is wrong. The bottom line is that with correct processing, the number of fps should not affect the game position. And in this case, due to errors in calculations with floating accurate results, the greater the difference fps, the more divergent the results will be. By adjusting methods, you can write a whole book, but the point is that the delta in all cases must be either fixed or absolute and an independent update of the game position and display of the game position is usually necessary. - VTT
  • @VTT delta - the amount of time that has elapsed since the previous method (frame) call - Mishakov Maxim

0