Accelerate

From GameStudio Wiki

Jump to: navigation, search

accelerate(var speed, var accel, var friction);

Calculates the move distance for a given speed and acceleration. The resistance of a surrounding medium can be given by the friction parameter. This function is more precise than the approximative motion code used in the script tutorial or the template scripts.


Parameters:
speed current speed in quants per tick. This value is changed by the acceleration, and thus must be a variable and not a number.
accel acceleration in quants per square tick.
friction the resitance parameter of the surrounding medium. 0 means no resistance.


Returns:
Distance to cover in quants.


Modify:
speed - current speed vector in quants per tick.


Speed:
Medium


Algorithm:

 if(friction == 0)
     distance = speed*time + 0.5*accel*time*time
     speed = speed + accel*time
   else
     distance = accel*time/friction + (speed-accel/friction)*(1-exp(-friction*time))/friction
     speed = accel/friction + (speed-accel/friction)*exp(-friction*time)


Example: [C-Script]

 var aspeed; // local var to store the current angular speed
 // accelerate the camera left-right rotation with cursor keys
 vec_add(camera.pan,accelerate(aspeed,5*key_force.x,0.7));
Personal tools