2 registered members (AndrewAMD, juanex),
1,247
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Vehicle Code, That Works Great With Terrain!
#26728
04/24/04 14:32
04/24/04 14:32
|
Joined: Aug 2001
Posts: 2,320 Alberta, Canada
William
OP
Expert
|
OP
Expert
Joined: Aug 2001
Posts: 2,320
Alberta, Canada
|
Many have reported a problem with terrain and vehicles using the physics engine. I believe that the reason for this, is that the last entity enabled in the physics engine will have faulty collision on terrain. Here is some vehicle code that will keep all 4 tires working well. Cheers - to Ventilator for getting me started. Instructions: 1. Create 4 wheels, make sure there origins are set right! 2. Create your vehicle body. 3. Apply the code. Code:
//--------------------------------------------------------------------------------------------------------------------------- //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //:::::::::::::::::::::::::::::::::::::::::Variables(Defines):::::::::::::::::::::::::::::::::::::::::::::::::::::: //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //---------------------------------------------------------------------------------------------------------------------------
//Wheel & Kart Defines entity* player; entity* wheel_fr; entity* wheel_fl; entity* wheel_bl; entity* wheel_br;
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];
//Environment Defines
var earth_gravity[3] = 0,0, -2086; var mars_gravity[3] = 0,0, -600;
var wheel_friction = 120; var wheel_mass = 130; var wheel_bounciness = 1; var wheel_minimumSpeed = 7;
var kart_mass = 130; var kart_friction = 40; var kart_bounciness = 1; var kart_minimumSpeed = 5;
// Movement Defines var m1_1[3]; var m1_2[3]; var m2[3]; var wheel_target;
//--------------------------------------------------------------------------------------------------------------------------- //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //:::::::::::::::::::::::::::::::::::::::::PowerSlide(settings):::::::::::::::::::::::::::::::::::::::::::::::::::::: //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //---------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------- //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //:::::::::::::::::::::::::::::::::::::::::Kart(settings):::::::::::::::::::::::::::::::::::::::::::::::::::::: //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //---------------------------------------------------------------------------------------------------------------------------
action kart // the kart action { my.pan += 180; p_player = me;
p_player.overlay = off; p_player.flare = off; p_player.bright = off; p_player.transparent = off; p_player.ambient = 50;
ph_setgravity(earth_gravity); phent_settype(my,PH_RIGID,PH_poly); phent_setgroup(my,2); phent_setmass(my,kart_mass,PH_poly); phent_setfriction(my,kart_friction); phent_setelasticity(my,kart_bounciness,kart_minimumspeed); }
//--------------------------------------------------------------------------------------------------------------------------- //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //:::::::::::::::::::::::::::::::::::::::::FrontLeft(Wheel):::::::::::::::::::::::::::::::::::::::::::::::::::::: //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //---------------------------------------------------------------------------------------------------------------------------
action fl_wheel { my.pan += 180;
p_FL_tyre=my;
phent_settype(my,PH_RIGID,PH_sphere); phent_setgroup(my,2); phent_setmass(my,wheel_mass,PH_sphere);
phent_setfriction(my,wheel_friction); phent_setelasticity(my,wheel_bounciness ,wheel_minimumSpeed); 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(0,0,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); }
//--------------------------------------------------------------------------------------------------------------------------- //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //:::::::::::::::::::::::::::::::::::::::::FrontRight(Wheel):::::::::::::::::::::::::::::::::::::::::::::::::::::: //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //---------------------------------------------------------------------------------------------------------------------------
action fr_wheel//front right tire { my.pan += 180;
p_FR_tyre=my; phent_settype(my,PH_RIGID,PH_sphere); phent_setgroup(my,2); phent_setmass(my,wheel_mass,PH_sphere);
phent_setfriction(my,wheel_friction); phent_setelasticity(my,wheel_bounciness ,wheel_minimumSpeed); 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(0,0,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); }
//--------------------------------------------------------------------------------------------------------------------------- //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //:::::::::::::::::::::::::::::::::::::::::BackLeft(Wheel):::::::::::::::::::::::::::::::::::::::::::::::::::::: //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //---------------------------------------------------------------------------------------------------------------------------
action bl_wheel //back left tire { my.pan += 180;
p_RL_tyre=my;
phent_settype(my,PH_RIGID,PH_sphere); phent_setgroup(my,2); phent_setmass(my,wheel_mass,PH_sphere);
phent_setfriction(my,wheel_friction); phent_setelasticity(my,wheel_bounciness ,wheel_minimumSpeed); 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(0,0,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); }
//--------------------------------------------------------------------------------------------------------------------------- //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //:::::::::::::::::::::::::::::::::::::::::BackRight(Wheel):::::::::::::::::::::::::::::::::::::::::::::::::::::: //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //---------------------------------------------------------------------------------------------------------------------------
action br_wheel { my.pan += 180;
p_RR_tyre=my;
phent_settype(my,PH_RIGID,PH_sphere); phent_setgroup(my,2); phent_setmass(my,wheel_mass,PH_sphere);
phent_setfriction(my,wheel_friction); phent_setelasticity(my,wheel_bounciness ,wheel_minimumSpeed); 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(0,0,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); }
//--------------------------------------------------------------------------------------------------------------------------- //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //:::::::::::::::::::::::::::::::::::::::::Control(Input):::::::::::::::::::::::::::::::::::::::::::::::::::::: //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //---------------------------------------------------------------------------------------------------------------------------
function control() { while(1) { if(key_a == 1) { wheel_target=-30; }
if(key_d == 1) { wheel_target= 30; }
if(key_a == 1 && key_d == 1)||(key_a == 0 && key_d == 0) { wheel_target=0; } if(key_s == 1) { vec_set(m2,vector(800,1000,0)); }
if(key_w == 1) { vec_set(m2,vector(-400,500,0)); }
if(key_w == 1 && key_s == 1)||(key_w == 0 && key_s == 0) { 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); } }
//--------------------------------------------------------------------------------------------------------------------------- //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //:::::::::::::::::::::::::::::::::::::::::Create(Player):::::::::::::::::::::::::::::::::::::::::::::::::::::: //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //---------------------------------------------------------------------------------------------------------------------------
action Need_Me //Action for the last physics entity created { phent_settype(my,PH_RIGID,PH_sphere); phent_setgroup(my,2); phent_setmass(my,wheel_mass,PH_box); }
function create_player() { ph_setgravity(vector(0,0,-380)); ph_setcorrections(30000,0.05);
ent_create(mdl_car, temp, kart);
vec_for_vertex(temp,p_player,627); wheel_fr = ent_create("tirefr.mdl",temp,fr_wheel);
vec_for_vertex(temp,p_player,618); wheel_fl = ent_create("tirefl.mdl",temp,fl_wheel);
vec_for_vertex(temp,p_player,614); wheel_bl = ent_create("tirebl.mdl",temp,br_wheel);
vec_for_vertex(temp,p_player,634); wheel_br = ent_create("tirebr.mdl",temp,bl_wheel);
vec_for_vertex(temp,p_player,634); // Note that I we have to create this so the collision detection works great for the original 4 wheels on terrain. wheel_br = ent_create("tirebr.mdl",temp,Need_Me); }
|
|
|
Re: Vehicle Code, That Works Great With Terrain!
[Re: ventilator]
#26731
04/29/04 14:52
04/29/04 14:52
|
Joined: Sep 2003
Posts: 3,236 San Diego, CA
Marco_Grubert
Expert
|
Expert
Joined: Sep 2003
Posts: 3,236
San Diego, CA
|
@ventilator:
I am using a P4, 1.9GHz with MSVC.
One thing I did notice today with terrain is that not all four wheels make contact with terrain, e.g. in Frame 1 there would be 3 wheels on the floor and in Frame 2 the remaining wheel would be in contact, whereas on level geometry all 4 wheels return contacts. That bug will probably be solved by tomorrow.
A few things I noticed about your original car demo is that the vehicle tends to flip easily. This can be greatly reduced by making the car chassis very light and the wheels heavier thus lowering the center of gravity. It's also worthwhile to adjust wheel friction to current speed. Finally for future A6 versions that have auto-disabling (and a 30% framerate increase, yippee!) unnecessary calls to phcon_setparams/setmotor should be avoided, i.e. don't change the motor speed if it's already running at its minimum.
The crane AVI showed one sphere getting stuck in the crane. Fixing penetration detection is #3 on my physics list right now.
Last edited by Marco_Grubert; 04/29/04 14:58.
|
|
|
Re: Vehicle Code, That Works Great With Terrain!
[Re: Marco_Grubert]
#26732
04/29/04 22:30
04/29/04 22:30
|
Joined: Oct 2001
Posts: 1,407 Helsinki, Finland
Phantom88
Expert
|
Expert
Joined: Oct 2001
Posts: 1,407
Helsinki, Finland
|
Quote:
The crane AVI showed one sphere getting stuck in the crane. Fixing penetration detection is #3 on my physics list right now.
Could you share that list? I'd like to know what you are working on...
~Phantom88~
Programmer, Gamer
ICQ #: 157485106 | Xfire: Phantom1988 | MSN: lauri_andler@hotmail.com | AIM: FinPhantom | YAHOO: FinPhantom
|
|
|
Re: Vehicle Code, That Works Great With Terrain!
[Re: Marco_Grubert]
#26733
04/30/04 01:33
04/30/04 01:33
|
Joined: Aug 2001
Posts: 2,320 Alberta, Canada
William
OP
Expert
|
OP
Expert
Joined: Aug 2001
Posts: 2,320
Alberta, Canada
|
I agree - the vehicle does flip very easily. As of now, faster vehicles are very hard to have, due to the flipping. If you add more mass to the wheels, the kart doesn’t flip, yet it goes much slower. Another way to combat against the flipping is by adding more gravity. Adding more gravity can also be a problem since you cannot go up inclines, yet you can still maintain very high speeds. What I need is a way to keep higher speeds, go up inclines, and not flip easily. I believe this could be achieved if you could add some extra options for gravity. Such options would be the ability to “tone” down how fast something would slide on an incline due to gravity. Thank You.
|
|
|
Re: Vehicle Code, That Works Great With Terrain!
[Re: William]
#26734
04/30/04 01:55
04/30/04 01:55
|
Joined: Mar 2003
Posts: 5,377 USofA
fastlane69
Senior Expert
|
Senior Expert
Joined: Mar 2003
Posts: 5,377
USofA
|
Have you tried writing in a torque based stabiliztion routine?
Something so that if your pan tilt roll changes drastically, then you anti-torque the vehicle to keep from flipping. By programming a naively adaptive routing (torque right if spinning left; torque left if sprinning right), you should in theory prevent wild "flippings"
Or how about an adaptive setmaxspeed routine? If you start flipping beyond limits, you set phent_setmaxspeed(my,1000,0) and this will stop your rotation (ie flipping) dead in it's tracks. You can then script in or let the PE return the car to it's natural position.
Yet another solution is to pause the PE if you start flipping beyond the limit (say my.tilt or my.roll >90)....Hence, you disable the PE, readjust your vehicle and re-engage when the vehicle is where you want it. It should all be done in a frame or two, thus making this solution the best bet in my mind.
|
|
|
Re: Vehicle Code, That Works Great With Terrain!
[Re: William]
#26735
04/30/04 04:12
04/30/04 04:12
|
Joined: Sep 2003
Posts: 3,236 San Diego, CA
Marco_Grubert
Expert
|
Expert
Joined: Sep 2003
Posts: 3,236
San Diego, CA
|
I should also mention that to my knowledge there is no pure rigid body car simulation out there. When you want high speed you will have to cheat.
The upcoming racing game Xpand Rally
http://www.xpandrally.com/
uses almost the same physics system as A6, yet for the wheels they do their own calculations. In an e-mail from the lead developer:
Quote:
We used 4 spheres to simulate wheels but I fixed wheels angular speed to
zero and let car to slip over terrain by make friction coeff low.
Then I have a car which slips over whole map like a sledge - I dont worry
about collision. Then I can manage friction, speed, slide of tyres, etc. by applying forces to wheels or to car body.
I think you can get away with rigid body physics for low speeds - up to 15 kmh/mph but after that should cheat as described above.
http://home.planet.nl/~monstrous/tutcar.html
http://www.racer.nl/
Last edited by Marco_Grubert; 04/30/04 05:24.
|
|
|
|