code:
var video_mode = 9;
var video_depth = 32;
entity* p_player;
entity* p_FR_tyre;
entity* p_FL_tyre;
entity* p_RL_tyre;
entity* p_RR_tyre;
var d3d_alphadepth = 32;
var d3d_shadowdepth = 24;
var d3d_lightres=on;
var max_entities=10000;
var max_ph_entites=500;
var detail_size=12;
var clip_size=0;
on_f12=_tog_dbg;
string mdl_car=<car.mdl>;
string mdl_tyre=<tyre_0.mdl>;
//-----------------------------------------------------------------------------move_camera_player
function move_camera_player()
{
camera.ambient=0;
camera.arc=60;
var cam_ang[3];
var cam_dist=3000;
cam_ang.pan=0;
cam_ang.tilt=70;
cam_ang.roll=0;
while(1)
{
if(mouse_right)
{
cam_ang.pan+=10*mouse_force.x;
cam_ang.tilt+=10*mouse_force.y;
cam_ang.tilt=min(max(cam_ang.tilt,5),70);
}
else
{
}
cam_dist-=mickey.z;
cam_dist=min(max(cam_dist,300),4500);
camera.x=p_player.x+cos(cam_ang.pan)*(cam_dist*cos(cam_ang.tilt));
camera.y=p_player.y+sin(cam_ang.pan)*(cam_dist*cos(cam_ang.tilt));
camera.z=p_player.z+sin(cam_ang.tilt)*cam_dist;
vec_set(temp,p_player.x);
vec_sub(temp,camera.x);
vec_to_angle(camera.pan,temp);
wait(1);
}
}
var FR_wheel_ID;
var FL_wheel_ID;
var RL_wheel_ID;
var RR_wheel_ID;
var p1[3];
var p2[3];
var p3[3];
var p4[3];
var p5[3];
var p6[3];
var sc=1;
var dc=0.008;
//-----------------------------------------------------------------------------car
action FR_tyre
{
p_FR_tyre=my;
my.shadow=on;
phent_settype(my,PH_RIGID,PH_SPHERE);
phent_setgroup(my,2);
phent_setmass(my,10,PH_SPHERE);
phent_setfriction(my,100);
phent_setelasticity(my,50,10);
phent_setdamping(my,20,20);
vec_set(p2,vector(0,0,1));
vec_set(p3,vector(0,1,0));
vec_set(p4,vector(-40,40,0));
vec_set(p6,vector(sc,dc,0));
FR_wheel_ID=phcon_add(PH_WHEEL,p_player,my);
phcon_setparams1(FR_wheel_ID,my.x,p2,p3);
phcon_setparams2(FR_wheel_ID,p4,nullvector,p6);
}
action FL_tyre
{
p_FL_tyre=my;
my.shadow=on;
my.pan=180;
phent_settype(my,PH_RIGID,PH_SPHERE);
phent_setgroup(my,2);
phent_setmass(my,10,PH_SPHERE);
phent_setfriction(my,100);
phent_setelasticity(my,50,10);
phent_setdamping(my,20,20);
vec_set(p2,vector(0,0,1));
vec_set(p3,vector(0,1,0));
vec_set(p4,vector(-40,40,0));
vec_set(p6,vector(sc,dc,0));
FL_wheel_ID=phcon_add(PH_WHEEL,p_player,my);
phcon_setparams1(FL_wheel_ID,my.x,p2,p3);
phcon_setparams2(FL_wheel_ID,p4,nullvector,p6);
}
action RL_tyre
{
p_RL_tyre=my;
my.shadow=on;
my.pan=180;
phent_settype(my,PH_RIGID,PH_SPHERE);
phent_setgroup(my,2);
phent_setmass(my,10,PH_SPHERE);
phent_setfriction(my,100);
phent_setelasticity(my,50,10);
phent_setdamping(my,20,20);
vec_set(p2,vector(0,0,1));
vec_set(p3,vector(0,1,0));
vec_set(p4,vector(0,0,0));
vec_set(p6,vector(sc,dc,0));
RL_wheel_ID=phcon_add(PH_WHEEL,p_player,my);
phcon_setparams1(RL_wheel_ID,my.x,p2,p3);
phcon_setparams2(RL_wheel_ID,p4,nullvector,p6);
}
action RR_tyre
{
p_RR_tyre=my;
my.shadow=on;
phent_settype(my,PH_RIGID,PH_SPHERE);
phent_setgroup(my,2);
phent_setmass(my,10,PH_SPHERE);
phent_setfriction(my,100);
phent_setelasticity(my,50,10);
phent_setdamping(my,20,20);
vec_set(p2,vector(0,0,1));
vec_set(p3,vector(0,1,0));
vec_set(p4,vector(0,0,0));
vec_set(p6,vector(sc,dc,0));
RR_wheel_ID=phcon_add(PH_WHEEL,p_player,my);
phcon_setparams1(RR_wheel_ID,my.x,p2,p3);
phcon_setparams2(RR_wheel_ID,p4,nullvector,p6);
}
action car
{
p_player=my;
wait(1);
my.shadow=on;
my.material=mat_metal;
phent_settype(my,PH_RIGID,PH_BOX);
phent_setgroup(my,2);
phent_setmass(my,160,PH_BOX);
phent_setfriction(my,50);
phent_setelasticity(my,50,10);
phent_setdamping(my,20,20);
vec_for_vertex(temp,my,306);
ent_create(mdl_tyre,temp,FR_tyre);
vec_for_vertex(temp,my,36);
ent_create(mdl_tyre,temp,FL_tyre);
vec_for_vertex(temp,my,5);
ent_create(mdl_tyre,temp,RL_tyre);
vec_for_vertex(temp,my,304);
ent_create(mdl_tyre,temp,RR_tyre);
}
var m1;
var m2;
//--------------------------------------------------------------------------------keys
function k_accelerate()
{
vec_set(m2,vector(-50,500,0));
phcon_setmotor(FR_wheel_ID,m1,m2,nullvector);
phcon_setmotor(FL_wheel_ID,m1,m2,nullvector);
phcon_setmotor(RR_wheel_ID,nullvector,m2,nullvector);
phcon_setmotor(RL_wheel_ID,nullvector,m2,nullvector);
while(key_cuu)
{
wait(1);
}
vec_set(m2,vector(0,0,0));
phcon_setmotor(FR_wheel_ID,m1,m2,nullvector);
phcon_setmotor(FL_wheel_ID,m1,m2,nullvector);
phcon_setmotor(RR_wheel_ID,nullvector,m2,nullvector);
phcon_setmotor(RL_wheel_ID,nullvector,m2,nullvector);
}
function k_brake()
{
vec_set(m2,vector(0,500,0));
phcon_setmotor(FR_wheel_ID,m1,m2,nullvector);
phcon_setmotor(FL_wheel_ID,m1,m2,nullvector);
phcon_setmotor(RR_wheel_ID,nullvector,m2,nullvector);
phcon_setmotor(RL_wheel_ID,nullvector,m2,nullvector);
while(key_cud)
{
wait(1);
}
vec_set(m2,vector(0,0,0));
phcon_setmotor(FR_wheel_ID,m1,m2,nullvector);
phcon_setmotor(FL_wheel_ID,m1,m2,nullvector);
phcon_setmotor(RR_wheel_ID,nullvector,m2,nullvector);
phcon_setmotor(RL_wheel_ID,nullvector,m2,nullvector);
}
function k_left()
{
vec_set(m1,vector(-4,500,0));
phcon_setmotor(FR_wheel_ID,m1,m2,nullvector);
phcon_setmotor(FL_wheel_ID,m1,m2,nullvector);
while(key_cul)
{
wait(1);
}
vec_set(m1,vector(0,500,0));
phcon_setmotor(FR_wheel_ID,m1,m2,nullvector);
phcon_setmotor(FL_wheel_ID,m1,m2,nullvector);
}
function k_right()
{
vec_set(m1,vector(4,500,0));
phcon_setmotor(FR_wheel_ID,m1,m2,nullvector);
phcon_setmotor(FL_wheel_ID,m1,m2,nullvector);
while(key_cur)
{
wait(1);
}
vec_set(m1,vector(0,500,0));
phcon_setmotor(FR_wheel_ID,m1,m2,nullvector);
phcon_setmotor(FL_wheel_ID,m1,m2,nullvector);
}
on_cuu=k_accelerate;
on_cud=k_brake;
on_cul=k_left;
on_cur=k_right;
function initialize_physics
{
ph_setgravity(vector(0,0,-380));
ph_setcorrections(20000,0.1);
}
string s1;
string s2;
function debug()
{
while(1)
{
str_cpy(s1,"sc: ");
str_for_num(s2,sc);
str_cat(s1,s2);
draw_text(s1,20,100,vector(255,255,255));
str_cpy(s1,"dc: ");
str_for_num(s2,dc);
str_cat(s1,s2);
draw_text(s1,20,124,vector(255,255,255));
wait(1);
}
}
//--------------------------------------------------------------------------------main
function main()
{
level_load("physics_test.wmb");
wait(1);
sun_angle.pan=210;
sun_angle.tilt=25;
camera.fog_start=0.25*clip_range;
camera.fog_end=1*clip_range;
camera.clip_near=50;
camera.clip_far=50000;
camera.arc=75;
ent_create(mdl_car,vector(0,0,200),car);
initialize_physics();
while(p_player==null){wait(1);}
move_camera_player();
debug();
}