car.c A8

The main physics script for ground vehicles like cars and hovercraft, also used by the car templates. Contains a startup function that activates the PhysX engine and establishes a default setting for racing games. The script is automatically included when adding the car template (t_car.h), and all default parameters can be adjusted in this template.

Global parameters

Parameter Default Remarks
Gravity factor.
Car brake power in multiples of the car's motor power (car_maxTorque)
Steer angle increase in degrees per tick.
Steer angle reduction at high speed, in percent.
Crash speed, plays crash sound when abruptly stopped above this speed (km/h).
Engine sound, loopable.
Tire screech sound, loopable.
Crash sound.
Horn sound, loopable.
Accelerator, reverse gear.
Steer left.
Steer right.
Full brake.
Sound the horn.
Upwards kick for lifting a toppled car.


Physics car action. Attaches 4 wheels to the car and initiates car physics, controlled by keyboard and joystick. Assign this action to the player's car in WED, or set the skills as described below and call the action for a script-created car. If a skill is 0, its default value is used. The parameters allow to set up a quite realistic driving behavior, better than the ODE physics cars from A6/A7.


Skill/Flag Name Default Remarks
FLAG1 car_hover 0 Set this flag for a hovercraft (invisible wheels).
skill1 car_maxSpeed 200 Car Maximum Speed, in km/h on even road. When the car approaches this speed, the motor torque is automatically reduced. The car can exceed this speed a little when driving downhill or receiving heavy tail wind.
skill2..4 car_wheel_fx/y/z 40,20,-14 Offset of the front wheels from the car center. Scaled with the car scale.
skill5..7 car_wheel_rx/y/z -34,20,-14 Offset of the rear wheels from the car center. Scaled with the car scale.
skill8 car_wheelDrive 1 1 = Rear wheel drive, 2 = Front wheel drive, 3 = 4-wheel drive.
skill9 car_maxTorque 1000 Motor Power, maximum torque applied to the driven wheels (automatically reduced at high speed).
skill10 car_maxSteer 40 Maximum Steer Angle, determines the turning circle (automatically reduced at high speed).
skill11 car_massFactor 100 Car Mass Factor - multiplies the car volume mass with the given percentage.
skill12 car_massOffset -100 Car Mass Offset - vertical offset of the mass center in percent. Place the center low to avoid that the car topples in fast curves.
skill13 car_massShape 100 Car Mass Shape - inertia tensor x component, determines the inertia for rolling about the car's length axis. Make this small for elongated cars (like dragsters) or high for less toppling.
skill14 car_spsSpring 300 Suspension Spring element - fine tune the spring stiffness according to the car's weight.
skill15 car_spsDamper 100 Suspension Damper element - increase this for reducing bumpiness.
skill16 car_spsWay 10 Suspension Way in quants - increase this for large wheels or monster trucks.
skill18 car_frictionWheel 100 Wheel Longitudinal Friction - friction factor in forward direction, affects acceleration and braking.
skill19 car_frictionLatF 200 Front Wheel Lateral Friction - sidewards friction factor, affects steering.
skill20 car_frictionLatR 200 Rear Wheel Lateral Friction - sidewards friction factor, affects power sliding in curves.
skill61 car_torque   Current motor power (can be set by an external AI function; automatically reduced at high speed).
skill62 car_brake   Current brake power (can be set by an external AI function; automatically applied when the car rolls forward and reverse gear is hit).
skill63 car_steerangle   Current steer angle (can be set by an external AI function; automatically reduced at high speed).
skill66 actor_id   Unique number to identify the entity (1357 == car)
skill71 car_speed   Current speed of the car in km/h; can be read by external functions, f.i. from the speedometer in t_car_props.c.
skill75 car_wheelFL   Pointer of the front left wheel entity; can be used for effects or for individually adjusting the ground friction.
skill76 car_wheelFR   Pointer of the front right wheel entity.
skill77 car_wheelRL   Pointer of the rear left wheel entity.
skill78 car_wheelRR   Pointer of the rear right wheel entity.



action vehicle1()
  my.flags |= SHADOW;
  my.car_maxSpeed = 80;
  my.car_maxTorque = 500;
  my.car_spsSpring = 150; 
  my.car_spsDamper = 30; 
  my.car_massOffset = -60; 
  my.car_frictionLatR = 150;
// wheel positions can be determined from the MED status line: 
  my.car_wheel_fx = 23.5; 
  my.car_wheel_rx = -26; 
  my.car_wheel_fy = 21; 
  my.car_wheel_ry = 21; 
  my.car_wheel_fz = -10;
  my.car_wheel_rz = -11;
  my.car_spsWay = 7; // for suspension animation


Initializes the my entity as a car. Adds 4 wheels and sets the car parameters described above. For loading the wheel model, the function adds "_wheel" to the car name (f.i. "bmw_wheel.mdl" when the car is a "bmw.mdl"). If the specific wheel is not found, a general "wheel.mdl" is used. If that isn't found either, an error message pops up.

car_input(ENTITY* car)

Takes a car entity as parameter, and sets its car_torque, car_brake, and car_steerangle parameters from joystick and the cursor keys. Also handles kicking a toppled car with the [L] key, and sounding the horn with the [H] key. Call this in a while() loop to control a car.

car_move(ENTITY* car)

Drives a car according to the current car_torque, car_brake, and car_steerangle parameters. Call this in a while() loop to drive a car.

car_animate(ENTITY* car)

Animates wheel suspension and the steering of a car entity. Bones animation must be used. The suspension scenes must be named after the wheel they affect - "frontright", "frontleft", "rearright" and "rearleft". The steering scene must be named "steering". At the begin of the animations, the suspension is fully extended and the steer angle is full to the left. Call this in a while() loop to animate a car.

car_noise(ENTITY* car, SOUND* sndEngine, SOUND* sndTire, SOUND* sndCrash )

Plays motor sound, wheel sounds, and crash sound with the given car entity, depending on speed, torque, and brake. Call this in a while() loop to drive a car.


pXent_getspeed(ENTITY*): var

Physics helper function: returns the forward speed component of the entity in km/h units. The entity must be registered for physics.


See Also:

car template, car_props.c, PhysX, pXcon_setwheel

► latest version online