Speed Velocity, Acceleration and Deceleration:
Back to more fun examples
To run the program
- Program: Click here to download/run the program.
Here is the source code to this program (download and unzip).
- How to run:
- Left-Mouse-Button (LMB) click define the guide ball (Gp).
The direction for the hero velocity (V) is defined to be from
current hero center (Hp)
towards the guide ball (Gp).
- LMB-Drag distance defines the speed (S) for the velocity.
- What:
- The hero ball shoots toward the guide ball accelerating by 5% of current speed.
- When the hero speed exceeds 1000 pixels/second, a deceleration of 90%
of current speed
is applied to stop the hero ball.
Implementation:
- Direction of hero Velocity: from Hero towards guid ball.
- V = (Gp - Hp).Normalize()
(Normaliize: to get unit vector for direction.)
- Implementation:
WindowHandler::OnMouseButton() function: lines 93 and 94.
- Speed for hero Velocity: distance between guide ball position (Gp)
and current mouse position (Mp).
- S = Length(Mp - Gp).
- Implementation:
WindowHandler::OnMouseButton() function: line 92.
- Setting the hero velocity (S * V) to hero: lines 95 and 96.
- Acceleration for hero Velocity: increase by 5% of current speed.
- CurrentSpeed = Length(Hero.Velocity)
- Hero.Velocity = CurrentSpeed * 1.05 * Normalize(Hero.Velocity)
- Implementation:
Model::UpdateABall() function:
- Line 40: Compute CurrentSpeed.
- Lines 41 and 48: sets Hero.Velocity
Deceleration for hero Velocity: when current hero speed exceed 1000 pixel/sec,
begin decelerate by 90% of current speed until stops.
- CurrentSpeed = Length(Hero.Velocity), check CurrentSpeed against 1000.
- If too fast, set acceration to 0.9.
- Hero.Velocity = CurrentSpeed * 0.9 * Normalize(Hero.Velocity)
- Implementation:
Model::UpdateABall() function:
- Line 42: check CurrentSpeed for beginning of deceleration.
- Lines 45 and 46: sets acceleration to be slowing down.