I am following a Pixar course at Khan Academy and I came across a simulation of a double spring with a given timestep. I understand how it works and why it behaves the way it does, but I want to know if the math is actually accurate. What I'm interested in is the following lines:
// Mass 1 velocity
mass1VelocityY = mass1VelocityY + mass1AccelerationY * timeStep;
mass1VelocityX = mass1VelocityX + mass1AccelerationX * timeStep;
// Mass 1 position
mass1PositionY = mass1PositionY + mass1VelocityY * timeStep;
mass1PositionX = mass1PositionX + mass1VelocityX * timeStep;
So we first calculate the velocity, adding a fraction of the calculated acceleration to it, and then we calculate the position, adding a fraction of the calculated velocity to it. So we are building the final position at time T
by summing T/timestep
intermediate positions.
Given the formula for displacement s=ut+0.5at^2
where:
s = displacement u = initial velocity a = acceleration t = time
If I plug in the values: s = 0, u = 0, a = 1, t = 2, I will get s = 2
. That means that if I start at position zero and I build up the velocity with an acceleration of 1m/s/s I will end up at position 2, with a velocity of 2m/s.
Now if I try to follow the same logic but break that result into 20 steps(2/0.1 - t = 2 timeStep = 0.1)
, and sum the results of all these intermediate steps as is being done in that code, I will get a different result: s=0.55 at t=1
and s=2.1 at t=2
.
My initial intuition is that because in the code we end up multiplying timeStep by acceleration twice, it becomes exponentiation and so the progession isn't linear anymore and so smaller steps will get smaller values in the beginning. So I have 3 questions:
1) Did I understand what the code is doing correctly?
2) Is what the code is doing the most correct/accurate way to calculate displacement?
3) I really want to grasp these concepts so if you have any other advice or know of something that would be helpful for me to learn, please point me to it.