OP
Senior Expert
Joined: May 2002
Posts: 7,441
|
...my current car script. use q,a,w,s to adjust the spring/damper values! use z/y,x if you have to flip the car (currently the car tends to flip over in narrow curves). if you have any ideas for improvements (or if you have a complete better script ![[Smile]](images/icons/smile.gif) ) please let us know! 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=1000; var max_ph_entites=500;
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=1500; 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),3000);
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.005; //-----------------------------------------------------------------------------car setup 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,80); 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,80); 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,90); 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,90); 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,40); 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); }
//--------------------------------------------------------------------------------control function control() { var m1_1[3]; var m1_2[3]; var m2[3]; var wheel_target=0; while(1) { if(key_cul) { wheel_target=-38; } if(key_cur) { wheel_target=38; } if((!key_cul&&!key_cur)||(key_cul&&key_cur)) { wheel_target=0; } if(key_cuu) { vec_set(m2,vector(-50,500,0)); } if(key_cud) { vec_set(m2,vector(0,500,0)); } if((!key_cuu&&!key_cud)||(key_cuu&&key_cud)) { vec_set(m2,vector(0,0,0)); } phcon_getposition(FR_wheel_ID,temp); vec_set(m1_1,vector((wheel_target-temp.x)*0.2,500,0)); phcon_getposition(FL_wheel_ID,temp); vec_set(m1_2,vector((wheel_target-temp.x)*0.2,500,0)); phcon_setmotor(FR_wheel_ID,m1_1,m2,nullvector); phcon_setmotor(FL_wheel_ID,m1_2,m2,nullvector); phcon_setmotor(RR_wheel_ID,nullvector,m2,nullvector); phcon_setmotor(RL_wheel_ID,nullvector,m2,nullvector); wait(1); } }
string s1; string s2; //--------------------------------------------------------------------------------debug 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); } }
//--------------------------------------------------------------------------------set_scdc function set_scdc() { vec_set(p6,vector(sc,dc,0)); phcon_setparams2(FR_wheel_ID,vector(-40,40,0),nullvector,p6); phcon_setparams2(FL_wheel_ID,vector(-40,40,0),nullvector,p6); phcon_setparams2(RL_wheel_ID,nullvector,nullvector,p6); phcon_setparams2(RR_wheel_ID,nullvector,nullvector,p6); } function inc_sc() { while(key_q) { sc+=0.001; sc=min(max(sc,0),100); set_scdc(); wait(1); } } function dec_sc() { while(key_a) { sc-=0.001; sc=min(max(sc,0),100); set_scdc(); wait(1); } } function inc_dc() { while(key_w) { dc+=0.001; dc=min(max(dc,0),100); set_scdc(); wait(1); } } function dec_dc() { while(key_s) { dc-=0.001; dc=min(max(dc,0),100); set_scdc(); wait(1); } } on_q=inc_sc; on_a=dec_sc; on_w=inc_dc; on_s=dec_dc;
//--------------------------------------------------------------------------------flip function flipl() { phent_addtorquelocal(p_player,vector(-100000,0,0)); }
function flipr() { phent_addtorquelocal(p_player,vector(100000,0,0)); }
on_z=flipl; on_x=flipr;
//--------------------------------------------------------------------------------initialize_physics function initialize_physics { ph_setgravity(vector(0,0,-380)); ph_setcorrections(20000,0.1); }
//--------------------------------------------------------------------------------main function main() { level_load("physics_test.wmb"); wait(1);
sun_angle.pan=210; sun_angle.tilt=25;
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(); control(); debug(); }
|