How can I make the car follow a path, using physics ? The variable that controls the angle of the whell is max_angle, but I can not make the wheels follow the path. I tried using vec_set, and vec_diff vec_to_angle but I can not at all.
The code is:
function wheel_physics_init()
{
entroda = my;
set (my, SHADOW);
my.emask |= ENABLE_ENTITY | ENABLE_IMPACT | ENABLE_FRICTION | ENABLE_BLOCK;
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();
my.emask |= ENABLE_ENTITY | ENABLE_IMPACT | ENABLE_FRICTION | ENABLE_BLOCK;
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()
{
entroda = my;
wheel_physics_init();
//my.emask |= ENABLE_ENTITY | ENABLE_IMPACT | ENABLE_FRICTION | ENABLE_BLOCK;
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()
{
var LastPos[3];
var Dir[3];
chassis = my;
set (chassis, SHADOW);
//init physics variables
phent_settype (chassis, PH_RIGID, PH_BOX); // rigid and box as collision hull
phent_setmass (chassis, 1, 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, 100);
phent_setdamping (chassis, 0, 0);
phent_setelasticity (chassis, 20, 20); // only little bouncing
//init car wheels
//back
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);
//front
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);
wait(1);
while(1)
{
//if (key_a) stear_contr = - 60 * time_step;
//if (key_d) stear_contr = 60 * time_step;
stear_contr = (key_d - key_a);
max_angle += stear_contr * 10 * 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
//speed_contr = 1 * time_step;
ang_force = speed_contr * 1000 * time_step;
phcon_setmotor (constr_back_left, nullvector, vector(-ang_force, max_speed, 0), nullvector);
phcon_setmotor (constr_back_right, nullvector, vector(-ang_force, max_speed, 0), nullvector);
car_cam();
wait(1);
}
}
Last edited by Cristiano_Iost; 12/03/12 12:20.