One of the first things we realize in nature is speed. How fast things are happening or the objects are moving. As we grow we start to feel the acceleration of objects. The are moving faster or they slow down and stop. These phenomena seem strange to a kids eyes but later at school we learn that they can all me modeled in mathematics and we can easily predict speed and position of moving objects. Isaac Newton put these things in order with his work on kinematics. So today we are going to use his work to create a realistically moving object. In the Tutorials section we have built the necessary foundation upon which we will build our world.
Before we go to the code let's bring up some basic physics from our high school years.
First the definition of speed
dv = ds/dt
What this equation tells us is that when moving at constant speed, the speed is distance divided by time. This is enough approximation for the needs of a game. We don't have to be so strict with the mathematical approximation in real world simulation. So by solving for distance for constant speed movement we get
d = v*t
where t is the elapsed time and d the distance covered
If we have acceleration though things are a little more complicated. We assume that over the period we examine the acceleration is constant. This is the reason why we deal with such small time frames. If the elapsed time is little we can assume that the acceleration remains constant. The covered distance is then
s = vinit*t + 0.5*acceleration*t2 (1)
where vinit is the initial speed.
Function (1) is the general equation we can use to predict the final location of a moving object. There is an underlying bonus in this equation. Distance, speed (velocity actually) and acceleration are vectors and so they have direction built right into them. With correct use of vector math we can calculate the expected position of our object.
When all we need is the realistic movement of simple objects then all the physics and math we need is quite simple. When things get more complex then we need a real physics engine. The development of such a software product is a very serious job and requires a lot of effort and time. Apart from some physics principles that can be easily implemented it is a waste of time trying to develop a fully blown physics engine yourself. When I need an engine say for a car simulation game I simply use third party physics engines such as Newton from Newton Game Dynamics.
For the needs of our application I have broken the velocity vector into two parts. First is the speed. Speed is actually scalar and it is the length of the velocity vector. The second part is direction. This is given in degrees. I did it like this in order to make the drawing implementation easier. We have to implement our programs in such a way that we make our life easier when passing commands to the system underneath.
Our movement calculating code is this
// convert degrees to radians for library functions cos, sin double ha = heading*3.1415926/180.0; // calculate distance covered float dist = (fElapsed * speed + 0.5f*acceleration*fElapsed*fElapsed); // calculate position position += dist * (float)sin(-ha); position += dist * (float)cos(ha); // speed change = acceleration*elapsedTime speed += acceleration * fElapsed;
It is really quite simple. And we do not need to be rocket scientists to do it. We just have to understand and trust the physics any school child learns.