Search code examples
physicsgame-physics

How do I calculate the location of an object that is both turning, and accelerating?


I am trying to write a simple game, but I'm stuck on what I think is simple physics. I have an object that at point 0,0,0 and is travelling at say 1 unit per second. If I give an instruction, that the object must turn 15 degrees per second , for 6 seconds (so it ends up 90 degrees right of it's starting position), and accelerate at 1 unit per second for 4 seconds (so it's final speed is 5 units per second), how do I calculate it's end point?

I think I know how to answer this for an object that isn't accelerating, because it's just a circle. In the example above, I know that the circumference of the circle is 4 * distance (because it is traversing 1/4 of a circle), and from that I can calculate the radius and angles and use simple trig to solve the answer.

However, because at any given moment in time the object is travelling slightly faster than it was in the previous moment, my end result wouldn't be a circle, it would be some sort of arc. I suppose I could estimate the end point by looping through each step (say 60 steps per second), but this sounds error prone and inefficient.

Could anybody point me in the right direction?


Solution

  • Your notion of stepping through is exactly what you do.

    Almost all games operate under what's known as a "game tick". There are actually a number of different ticks that could be going on.

    "Game tick" - each game tick, a set of requests are fired, AI is re-evaluated, and overall the game state has changed.

    "physics tick" - each physics tick, each physical object is subject to a state change based on its current physical state.

    "graphics tick" - also known as a rendering loop, this is simply drawing the game state to the screen.

    The game tick and physics tick often, but do not need to, coincide with each other. You could have a physics tick that moves objects at their current speed along their current movement vector, and also applied gravity to it if necessary (altering its speed,) while adding additional acceleration (perhaps via rocket boosters?) in a completely separate loop. With proper multi-threading care, it would fit together nicely. The more de-coupled they are, the easier it will be to swap them out with better implementations later anyway.

    Simulating via a time-step is how almost all physics are done in real-time gaming. I even used to do thermal modeling for the department of defense, and that's how we did our physics modelling there too (we just got to use bigger computers :-) )

    Also, this allows you to implement complex rotations in your physics engine. The less special cases you have in your physics engine, the less things will break.