Simple physics-based movement

后端 未结 6 1946
眼角桃花
眼角桃花 2020-12-12 13:20

I\'m working on a 2D game where I\'m trying to accelerate an object to a top speed using some basic physics code.

Here\'s the pseudocode for it:


con         


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-12-12 13:45

    This isn't answering your question, but one thing you shouldn't do in simulations like this is depend on a fixed frame rate. Calculate the time since the last update, and use the delta-T in your equations. Something like:

    static double lastUpdate=0;
    if (lastUpdate!=0) {
      deltaT = time() - lastUpdate;
      velocity += acceleration * deltaT;
      position += velocity * deltaT;
    }
    lastUpdate = time();
    

    It's also good to check if you lose focus and stop updating, and when you gain focus set lastUpdate to 0. That way you don't get a huge deltaT to process when you get back.

提交回复
热议问题