I\'m implementing the scrolling behaviour of a touch screen UI but I\'m too tired in the moment to wrap my mind around some supposedly trivial piece of math:
y (
A bit of non programming discussion about the car example.
First, I'll assume that the driver cannot cause the brakes to lock at speed.
The first thing (or maybe the second or third thing) most new drivers learn is that the natural tendency when braking is to hold the brake pedal at a fixed position. The result is a sudden lurch forward as the car goes from moving slowly to stopping. This happens because the brakes are transitioning from dynamic friction, where braking force is proportional to brake pressure, to static friction, where braking force is restoring the forward momentum of the car. this sudden jump in acceleration is unpleasant, and the new driver learns to feather the pedal at the very end of deceleration to stop.
This behavior masks another pecularity, but this can be noticed during normal acceleration in a manual transmission car. when accelerating (or decelerating), if the driver suddenly pops the transmission out of gear, all of the passengers will suddenly lurch forward. What is actually happening, is the accelerating force that was pressing them into the backs of their seats is suddenly removed, and they spring back to a neutral sitting position. A more comfortable way to drive is to gradually feather the clutch so the motive force of the engine is removed gradually.
In both cases, the more aesthetic driving style involves smoothing the acceleration, removing sudden jumps. This is basically another way of talking about continious second derivative. Almost any motion with this property will seem natural.
I have tried this, which works (in Ruby). Not sure if the math is sound but the output looks right, meaning that you get faster as you get towards the center:
velocity=100;
(100.downto(0)).each { |distance_from_black_hole | velocity=velocity+9.8/distance_from_black_hole; puts velocity; }
Acceleration is the first order derivative of velocity and second order derivative of distance. Your graph looks like a second order parabola something like C-k*x^2 for some constants C and k. If y is really distance you need a=-2k, if y is velocity you need a=-2kx. In either case velocity v(x) = V0 + a(x)*x. (Where x is actually time. I am following your convention and not using t.)
While the car is slowing down I want to be able to calculate the velocity it will have exactly one second later solely based on it's current velocity.
That would be the definition of acceleration. For instance, if the acceleration was a = -9 meters/sec/sec
, and the velocity right now is 20 meters/sec
, then 1 second from now the velocity will be 11 meters/sec
.
In otherwords, the change in velocity Δv
between now and t
seconds from now (assuming constant acceleration) would be
Δv = a*t
meaning that the (classic physics) equation for the velocity at any time t
, given the initial velocity at t=0
(this velocity is called v0) is
v(t) = v
0+ a*t
Using what you'll learn in the first two weeks of calculus class, you can also get the equation for x(t)
(the distance of the car at time t
) from the above equation; this would give you
x(t) = x
0+ v
0*t + 0.5*a*t
2
(it is also possible to derive this without calculus, see here)
Finally, if you are doing this for a game, and not a physics simulation (meaning you don't need exactly precise results), you will want to simply change the position and velocity every frame, rather than recalculate the position every frame. To do this, you will want to do the following every frame, assuming velocity (and acceleration) is measured in pixels-per-second(-per-second):
velocity_new = velocity_old + acceleration/frames_per_second
position_new = position_old + velocity_old/frames_per_second
You could keep track of the velocity and cut that down by a fraction of the velocity each time. That would simulate friction quite well I believe.
y(x) = y0 - a * e ^ ( k * x )
where y0
is the start constant, and a
and k
are factors.
Example plot.