#include <acknex.h>
#include <default.c>
//vars, vectors, entities and angles
var constr_front_left;
var constr_front_right;
var constr_back_left;
var constr_back_right;
VECTOR mom_speed;
var max_angle;
var stear_contr;
var max_speed = 1500;
var ang_force;
var speed_contr;
VECTOR* vec_gravity = {x = 0; y = 0; z = -1800;}
ENTITY* chassis;
function wheel_physics_init()
{
set (my, SHADOW);
phent_settype (my, PH_RIGID, PH_SPHERE);
phent_setmass (my, 30, PH_SPHERE);
phent_setgroup (my, 2);
phent_setfriction (my, 100);
phent_setdamping (my, 20, 20);
phent_setelasticity (my, 0, 100);
}
// front wheels
function front_tyre_left()
{
wheel_physics_init();
constr_front_left = phcon_add (PH_WHEEL, chassis, my);
phcon_setparams1 (constr_front_left, my.x, vector (0,0,1), nullvector);
phcon_setparams2 (constr_front_left, nullvector, nullvector, nullvector);
}
function front_tyre_right()
{
wheel_physics_init();
constr_front_right = phcon_add (PH_WHEEL, chassis, my);
phcon_setparams1 (constr_front_right, my.x, vector (0,0,1), nullvector);
phcon_setparams2 (constr_front_right, nullvector, nullvector, nullvector);
}
// rear wheels
function back_tyre_left()
{
wheel_physics_init();
constr_back_left = phcon_add (PH_WHEEL, chassis, my);
phcon_setparams1 (constr_back_left, my.x, nullvector, vector (0,1,0));
phcon_setparams2 (constr_back_left, nullvector, nullvector, nullvector);
}
function back_tyre_right()
{
wheel_physics_init();
constr_back_right = phcon_add (PH_WHEEL, chassis, my);
phcon_setparams1 (constr_back_right, my.x, nullvector, vector (0,1,0));
phcon_setparams2 (constr_back_right, nullvector, nullvector, nullvector);
}
function chassis_init()
{
chassis = my;
set (chassis, SHADOW);
//init physics variables
phent_settype (chassis, PH_RIGID, PH_BOX); // rigid and box as collision hull
phent_setmass (chassis, 15, PH_BOX); // lighter than wheels to avoid tilt
phent_setgroup (chassis, 2); // all parts of the car in one group --> no collision
phent_setfriction (chassis, 20);
phent_setdamping (chassis, 5, 5);
phent_setelasticity (chassis, 10, 100); // only little bouncing
//init car wheels
//back
ent_create ("car_tyre_left.mdl", vector (chassis.x - 65, chassis.y - 45, chassis.z - 20), back_tyre_left);
ent_create ("car_tyre_right.mdl", vector (chassis.x - 65, chassis.y + 45, chassis.z - 20), back_tyre_right);
//front
ent_create ("car_tyre_left.mdl", vector (chassis.x + 65, chassis.y - 45, chassis.z - 20), front_tyre_left);
ent_create ("car_tyre_right.mdl", vector (chassis.x + 65, chassis.y + 45, chassis.z - 20), front_tyre_right);
wait(1);
}
void main()
{
level_load ("test.wmb");
//initialize the physics sub-system:
ph_setgravity (vec_gravity);
ph_fps_max_lock = 300;
//init chassis
ent_create ("chassis.mdl", vector (0,0, 100), chassis_init);
while (!chassis) {wait (1);} // wait until the chassis appears in the level
// speed and steering control
while (1)
{
// steering control
stear_contr = (key_d - key_a);//d = 1, a = -1, a & d = 0
max_angle += stear_contr * 3 * time_step;
max_angle = clamp (max_angle, -30, 30);
if (stear_contr != 0)
{
phcon_setparams2 (constr_front_left, vector (max_angle, max_angle, 0), nullvector, vector (95000, 500, 0));
phcon_setparams2 (constr_front_right, vector (max_angle, max_angle, 0), nullvector, vector (95000, 500, 0));
}
else
{
max_angle = max_angle * (1 - (0.25 * time_step));
if (abs(max_angle) < 1)
max_angle = 0;
phcon_setparams2 (constr_front_left, vector (max_angle, max_angle, 0), nullvector, vector (95000, 500, 0));
phcon_setparams2 (constr_front_right, vector (max_angle, max_angle, 0), nullvector, vector (95000, 500, 0));
}
// speed control
speed_contr = (key_w - key_s);//w = 1, s = -1, w & s = 0
ang_force = speed_contr * 1000 * time_step;
phcon_setmotor (constr_back_left, nullvector, vector(-ang_force, max_speed, 0), nullvector);
phcon_setmotor (constr_back_left, nullvector, vector(-ang_force, max_speed, 0), nullvector);
camera.x = chassis.x - 250 * cos(chassis.pan);
camera.y = chassis.y - 250 * sin(chassis.pan);
camera.pan = chassis.pan; // the camera and the car have the same pan angle
camera.z = chassis.z + 50; // place the camera above the car, play with this value
camera.tilt = -15; // look downwards
wait(1);
}
}