Variable acceleration motion

Updated: 2025-09-27

This example extends the circular motion simulation by allowing the vehicle acceleration to evolve dynamically over time.

Instead of switching abruptly between fixed acceleration values, the system now adjusts acceleration gradually, producing behavior closer to real vehicles: slow acceleration and sharper braking.

What is modeled?

  • Progressive acceleration when starting from rest.
  • Stronger braking at higher speeds.
  • Continuous circular motion.
  • Real-time visualization of position and velocity.
  • Plots showing the evolution of acceleration and velocity.

Key components

  • acceleration: current angular acceleration.
  • deltaA: incremental change applied to acceleration.
  • INTEGRATE: computes velocity change from acceleration.
  • velocity: smoothly varying angular speed.
  • angle: angular position driving the motion.
  • PLOTTER: records acceleration and velocity over time.

Acceleration control logic

  1. Acceleration increases slowly while speed is low.
  2. When acceleration exceeds a threshold, its growth reverses, producing braking.
  3. Once velocity reaches zero, the system resets and accelerates again.

The resulting motion resembles a realistic drive cycle with smooth acceleration and sharper deceleration.

Visual outcome

  • Acceleration follows a sawtooth-like pattern.
  • Velocity exhibits smooth oscillations.
  • The car changes color: green while accelerating, red while braking.

Example

/*

 Variable acceleration motion
 
*/
SYSTEM {type:OPTIONS, TIME_DECIMALS:2, SPEED:6}
SYSTEM {TYPE:ON_TIMER, TRIGGER:moverCar, INTERVAL: 0.1}

; --- Initial variables ---
INITIAL velocity, 0.0            ; Angular velocity
INITIAL angle, 0                ; Angular position (radians)
INITIAL acceleration, 0.01      ; Angular acceleration
INITIAL radius, 200             ; Circular track radius
INITIAL deltaA, 0.0002

; --- Graphics ---
Graphic {NAME:txtVel, Type:TEXT, X:510, Y:120, TEXT:"Velocity:"}
Graphic {NAME:car, Type:LINE, X1:-10, Y1:-10, X2:-10, Y2:10, X3:10, Y3:10, X4:10, Y4:-10, COLOR:green, close:1}
Graphic {NAME:track, Type:ARC, X:300, Y:300, RADIUS:X$radius, CLOSE:0, COLOR:#999999}

; --- Evolution plot ---
PLOTTER {NAME:curve, Y_0:acceleration, Y_1:velocity, X:TIME}

START 500

PROCEDURE moverCar
    ;-------------------------------
    ; SMOOTH ACCELERATION CONTROL
    ;-------------------------------

    ; If acceleration is too high, reduce it smoothly
    IF (X$acceleration > 0.04)
        SAVEVALUE deltaA, -0.003
        SAVEVALUE acceleration, 0.04
        MOVE {name:car, color:red}
    ENDIF

    ; If velocity reaches zero, reset and start accelerating again
    IF (X$velocity <= 0.0)
        SAVEVALUE deltaA, 0.0002
        SAVEVALUE acceleration, 0
        SAVEVALUE velocity, 0.0
        MOVE {name:car, color:green}
    ENDIF

    ;---------------------------------
    ; UPDATE VELOCITY AND POSITION
    ;---------------------------------

    ; Gradually modify acceleration
    SAVEVALUE acceleration, X$acceleration + X$deltaA

    ; Integrate acceleration to obtain velocity increment
    INTEGRATE {EXPRESSION: X$acceleration, DT: 0.1, SAVEVALUE: deltaVel}
    SAVEVALUE velocity, X$velocity + X$deltaVel

    ; Update angular position
    SAVEVALUE angle, X$angle + X$velocity * 0.1

    ;--------------------------------
    ; COMPUTE POSITION AND MOVE CAR
    ;--------------------------------

    ASSIGN rad, X$angle
    ASSIGN posX, 300 + X$radius * COS(P$rad)
    ASSIGN posY, 300 + X$radius * SIN(P$rad)
    MOVE {NAME:car, X:P$posX, Y:P$posY}

    ;--------------------------
    ; DISPLAY STATE AND PLOTS
    ;--------------------------

    ASSIGN tVel, round(X$velocity, 2)
    ASSIGN tAcc, round(X$acceleration, 4)
    MOVE {NAME:txtVel, TEXT:"Velocity: P$tVel rad/s\nAcceleration: P$tAcc"}

    ; Plot data for analysis
    PLOT curve, X$acceleration, X$velocity
TERMINATE
ENDPROCEDURE