Vehicle Code, That Works Great With Terrain!

Posted By: William

Vehicle Code, That Works Great With Terrain! - 04/24/04 14:32

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);
}


Posted By: Marco_Grubert

Re: Vehicle Code, That Works Great With Terrain! - 04/29/04 10:43

I am still not clear what the problem is. One issue I have noticed with terrain is that the car sometimes hops up when driving over the edge of a terrain triangle. When the car is resting or not driving across edges it behaves normally.
Following your code example I created another arbitrary physics entity somewhere in the level after creating the car. This, however, did not make any difference.

Do you have a testlevel or AVI showing the problem?
Posted By: ventilator

Re: Vehicle Code, That Works Great With Terrain! - 04/29/04 10:50

it's strange that you can't reproduce some of our problems. were you able to reproduce the collision issue i demonstrated with an avi?

what cpu is in your office pc? does conitec use the intel compiler? there were quite similar problems with the newton engine on non-pentium4 cpus a while ago which have been caused by the intel compiler! ...just a thought...
Posted By: Marco_Grubert

Re: Vehicle Code, That Works Great With Terrain! - 04/29/04 14:52

@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.


Posted By: Phantom88

Re: Vehicle Code, That Works Great With Terrain! - 04/29/04 22:30

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~
Posted By: William

Re: Vehicle Code, That Works Great With Terrain! - 04/30/04 01:33

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.
Posted By: fastlane69

Re: Vehicle Code, That Works Great With Terrain! - 04/30/04 01:55

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.
Posted By: Marco_Grubert

Re: Vehicle Code, That Works Great With Terrain! - 04/30/04 04:12

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/
Posted By: Marco_Grubert

Re: Vehicle Code, That Works Great With Terrain! - 05/05/04 08:40

To conclude this: there were two bugs in PH_WHEEL code that caused wheels to easily bend and to not report collisions on terrain properly. This will be fixed in the next beta making your vehicle code more stable.
Posted By: ventilator

Re: Vehicle Code, That Works Great With Terrain! - 05/05/04 08:45

so the wheel bending will be fixed but ph_wheel won't be suited very well for fast vehicles nevertheless because ph_wheel still will be a fast rotating constrained rigid body?

will a6's vehicle feature just be a script which cheats like xpand rally or do you plan to integrate real vehicle physics like newton has planned? (there already is a special vehicle module in ngd for a6 but it isn't feature complete yet.)
Posted By: William

Re: Vehicle Code, That Works Great With Terrain! - 05/05/04 09:18

As most know already, rally cars tend to slide around corners and the such much more than any other vehicles. Using external forces to turn your vehicle works perfect for rally car simulations. Though, it works horrible for anyone who wants their vehicles to turn precisly. Since you have to decrease the friction, the vehicle also slides very far while performing a turn. Again, good for rally car racers, very bad for anything else according to my current observations.
Could you add a feature that allows precise turning regardless of the friction value? I would really like to be able to change a models orientation using pan while in the physics engine, since phent_addtorque and phent_addforce do not turn the vehicle very precisly. Thanks, I hope to hear what type of a solution you can come up with for this, since I'm almost all out of ideas.

Here's my current script, you will be able to understand what I mean about turning and the such by implementing this. Remember, call "Flex" from the main function. Also, be sure to have 2 completely horizontal vertex positions on your vehicle. You will notice that I use these 2 vertices to find the angle of the vehicle, that way I know what direction to apply the force in. As always, please share any insight on the script, since I am not a complete expert with external forces, and perhaps - there is a great way to have precise turning after all. Hope this helps. Thanks.

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, -316;
var mars_gravity[3] = 0,0, -316;

var wheel_friction = 90;
var wheel_mass = 90;
var wheel_bounciness = 0;
var wheel_minimumSpeed = 0;

var kart_mass = 30;
var kart_friction = 40;
var kart_bounciness = 0;
var kart_minimumSpeed = 0;

// Movement Defines
var m1_1[3];
var m1_2[3];
var m2[3];
var wheel_target;

//---------------------------------------------------------------------------------------------------------------------------
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//:::::::::::::::::::::::::::::::::::::::::PowerSlide(settings)::::::::::::::::::::::::::::::::::::::::::::::::::::::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//---------------------------------------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------------------------------------
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//:::::::::::::::::::::::::::::::::::::::::Kart(settings)::::::::::::::::::::::::::::::::::::::::::::::::::::::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//---------------------------------------------------------------------------------------------------------------------------

var air[3];
var force[3];

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_box);
phent_setgroup(my,2);
phent_setmass(my,kart_mass,PH_poly);
phent_setfriction(my,kart_friction);
phent_setelasticity(my,kart_bounciness,kart_minimumspeed);

wait(10);

while(1)
{

if(key_f == 1) //Jump Up
{
wheel_target = 0;

phent_addcentralforce( p_FR_tyre, vector(0,0,90000000)); // this is the jump strength
phent_addcentralforce( p_FL_tyre, vector(0,0,90000000));
phent_addcentralforce( p_RL_tyre, vector(0,0,90000000));
phent_addcentralforce( p_RR_tyre, vector(0,0,90000000));

phent_addcentralforce( p_player, vector(0,0,900000));
}
wait(1);
}
}

//---------------------------------------------------------------------------------------------------------------------------
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//:::::::::::::::::::::::::::::::::::::::::Control::::::::::::::::::::::::::::::::::::::::::::::::::::::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//---------------------------------------------------------------------------------------------------------------------------


Function Flex() // Call this from main
{
var s_vert1[3];
var s_vert2[3];
var s_angle[3];
var s_force[3];
var z_force[3];
var f_orce[3] = 8880,0,0;
var f_orcer[3] = -8880,0,0;

phent_setdamping (p_FR_tyre, 0, 100 );
phent_setdamping (p_FL_tyre, 0, 100 );
phent_setdamping (p_RL_tyre, 0, 100 );
phent_setdamping (p_RR_tyre, 0, 100 );

phent_setdamping (p_player, 0, 100 );

while(1)
{
vec_for_vertex(s_vert1.x,p_player,49); // base vertex
vec_for_vertex(s_vert2.x,p_player,51); // tip vertex

// Get the angle from the base to the tip of the kart, and store it in angle[3]
vec_set(temp,s_vert2.x);
vec_sub(temp,s_vert1.x);
vec_to_angle(s_angle,temp);

vec_inverse(temp);
vec_scale(temp, 0.5);
vec_set(s_force.x,nullvector);
s_force.x = 90000; // how fast the skid is
vec_rotate(s_force.x,s_angle);

vec_set(z_force.x,nullvector);
z_force.x = -90000;
vec_rotate(z_force.x,s_angle);

if(key_d == 1) // Turn
{
phent_addtorqueglobal(p_player, vector(0,0,-8000));
//phent_addforceglobal (p_player, f_orce, vector(2000, 2000, 0));
}

if(key_a == 1) // Turn
{
//phent_addforceglobal (p_player, f_orcer, vector(2000, 2000, 0));
phent_addtorqueglobal(p_player, vector(0,0,8000));
}

if(key_w == 1) //Accelerate
{
wheel_target = 0;

phent_addcentralforce( p_FR_tyre, s_force.x); //Accelerate
phent_addcentralforce( p_FL_tyre, s_force.x);
phent_addcentralforce( p_RL_tyre, s_force.x);
phent_addcentralforce( p_RR_tyre, s_force.x);

phent_addcentralforce( p_player, s_force.x);
}

if(key_s == 1) //Reverse
{
wheel_target = 0;
phent_addcentralforce( p_FR_tyre, z_force.x); // Reverse
phent_addcentralforce( p_FL_tyre, z_force.x);
phent_addcentralforce( p_RL_tyre, z_force.x);
phent_addcentralforce( p_RR_tyre, z_force.x);

phent_addcentralforce( p_player, z_force.x);
}

wait(1);
}
}

//---------------------------------------------------------------------------------------------------------------------------
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//:::::::::::::::::::::::::::::::::::::::::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=-40;
}

if(key_d == 1)
{
wheel_target= 40;
}

if(key_a == 1 && key_d == 1)||(key_a == 0 && key_d == 0)
{
wheel_target=0;
}


vec_set(temp, p_player.x); // makes it so temp is always starts at my.x
temp.z -= 4000; // set how far to trace
air = c_trace(p_player.x, temp,ignore_me+ignore_sprites+ignore_models); // see's if player will hit ground sometime

if(air > 30) //if the player is still in the air
{
ph_setgravity(mars_gravity);
wheel_target=0;
}
else
{
ph_setgravity(earth_gravity);
}
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);
wait(1);
}
}

//---------------------------------------------------------------------------------------------------------------------------
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//:::::::::::::::::::::::::::::::::::::::::Create(Player)::::::::::::::::::::::::::::::::::::::::::::::::::::::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//---------------------------------------------------------------------------------------------------------------------------

action Need_Me //faulty action
{
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(mdl_tyrefr,temp,fr_wheel);

vec_for_vertex(temp,p_player,618);
wheel_fl = ent_create(mdl_tyrefl,temp,fl_wheel);

vec_for_vertex(temp,p_player,614);
wheel_bl = ent_create(mdl_tyrebl,temp,br_wheel);

vec_for_vertex(temp,p_player,634);
wheel_br = ent_create(mdl_tyrebr,temp,bl_wheel);

vec_for_vertex(temp,p_player,634);
wheel_br = ent_create(mdl_tyrebr,temp,Need_Me);
}





Posted By: Marco_Grubert

Re: Vehicle Code, That Works Great With Terrain! - 05/05/04 11:25

I tried the script, but you have made some last minute variable changes (I guess) which is why it won't compile. After doing several search and replaces I got it to compile but it's not moving at all.
Posted By: William

Re: Vehicle Code, That Works Great With Terrain! - 05/09/04 05:45

Did you get it to work for you yet Marco? If it still does not work, please tell me what the errors are. I'd like to know, since once you do get it to work for yourself, I have a few questions about adding forces to the wheels so you can turn better. Thanks.


BTW - It works for me fine.
Posted By: Marco_Grubert

Re: Vehicle Code, That Works Great With Terrain! - 05/11/04 06:49

Can you send me a ZIP with all required files? I can't integrate into my current car script.
Posted By: William

Re: Vehicle Code, That Works Great With Terrain! - 05/11/04 08:52

Okay, i'll throw together a quick car model that works with the code and send it to you. Since I cannot send you my kart model. Expect it in 2 days or less.
Posted By: BlueBeast

Re: Vehicle Code, That Works Great With Terrain! - 05/11/04 16:09

sorry, didnt see the date of the post... Please post all findings of this script since we're dying to have a good car-on-terrain script!


BTW, is this script implementing the 'fix' that was done a week or 2 ago?

thanks
Jason
Posted By: William

Re: Vehicle Code, That Works Great With Terrain! - 05/15/04 14:33

Sorry Marco, but i've been very busy lately. I'll send the car code with car model and tires next week. Again, sorry about that.

Making major changes to it, such as getting rid of all that external vertex checking junk...
Posted By: William

Re: Vehicle Code, That Works Great With Terrain! - 05/16/04 15:10

I almost have it!!!! After about 12 hrs of work on adding to my old vehicle code(see above), I just about have it perfectly working with 0 friction! Yes that's right, 0 friction! It actually moves like there is perfect friction applied to it, and turns like the wheels are turning it. Best of all - there is no flipping, no wheel bending, and you can go up ramps with ease. Thanks Conitec for making this possible with phent_getvelocity,a variety of vector commands and most importantly the commands to add forces and torque. I'll most likely post it in a few days, just got alot of tweaking now... figured i'd keep everyone updated on my situation.
Posted By: BlueBeast

Re: Vehicle Code, That Works Great With Terrain! - 05/16/04 19:12

Awesome! Some of us are awaiting the results of your efforts! Too cool i say


Jason
Posted By: Phantom88

Re: Vehicle Code, That Works Great With Terrain! - 05/18/04 01:03

Sounds very interesting, but sorry for not paying attention while reading, is this using only 1 body or 4?(chassis+4 wheels)

~Phantom88~
Posted By: William

Re: Vehicle Code, That Works Great With Terrain! - 05/18/04 01:26

1 chassis and 4 wheels. Things are progressing very well, I hope to have it completed in a few days. It takes alot of time to fine tune things, since I currently have the mass and friction set at 0 and my mass's set at 5. So far, I am done the limiters for speed and turning, and a extra limiter for turning while stopped and not accelerating. I have finished the force friction while not accelerating. Right now I am working on, and am almost done, the friction equation I developed that takes place while accelerating. The vehicle code needs this or else when you are turning, the vehicle will slide to the right or left even though your forces should be pushing it ahead. I'll do up the brakes and reverse components tonight, that shouldn't take to long. In the end I'll have a completly force driven vehicle script which can be easily modified for all types of vehicle use, such as rally cars, trucks, cars, karts ect...
Posted By: steve52

Re: Vehicle Code, That Works Great With Terrain! - 05/18/04 18:25

Awesome William!

I'm really looking forward to it!

steve
Posted By: William

Re: Vehicle Code, That Works Great With Terrain! - 05/24/04 11:23

Okay here's where I'm at... I believe it will be 2 more good weeks of work on it before its done. I have decided to re-write certian parts and theres so much more to do, thanks for your support!
Posted By: Helghast

Re: Vehicle Code, That Works Great With Terrain! - 06/30/04 00:18

k i am using youre code that's done so far, but i need to know one thing...

which variable gets the speed stored in??

regards,
Posted By: Marco_Grubert

Re: Vehicle Code, That Works Great With Terrain! - 06/30/04 04:56

And another update- I have finished my vehicle code, but it will take a few more weeks before the artwork is done and a demo will be available.
Posted By: William

Re: Vehicle Code, That Works Great With Terrain! - 06/30/04 09:44

I am sorry that I never replied here earlier(I was waiting for Marco to confirm he would release his script to the public). As most know, I had come up with a couple scripts for high speed vehicle code. I was having alot of trouble implementing highspeed turning, and could not figure out a "fix" for my code to make it work. Since then I've seen a small video of Marcos code in action and I must say it is absolutly great, and very promising! So promising that I quit work on trying to find a "fix" for my code and began working on other much needed features in my own project(since I am now planning to use Marcos vehicle code when he releases it).
Sorry for any confusion I caused. I would advise everyone who wants a clean working high speed vehicle script to wait until Marco releases his. Since once the update is released, my current implementation will not work right due to the changes in things such as force calculation.
© 2024 lite-C Forums