Game Development Community

dev|Pro Game Development Curriculum

Stupid WheeledVehicle tricks

by Brian Richardson · 09/27/2005 (3:24 pm) · 28 comments

Stupid WheeledVehicle Tricks!

Here's a couple code snipplets that should answer some questions that keep popping up in the fourms and on IRC.

Drive on walls! Or, Mommy! My vehicle has sticky wheels!

In wheeledVehicle.cc, WheeledVehicle::updateForces, look for:

// Sum up spring and wheel torque forces
   for (Wheel* wheel = mWheel; wheel < wend; wheel++)  {
      if (!wheel->tire || !wheel->spring)
         continue;

      F32 Fy = 0;
      if (wheel->surface.contact) {

Change it to this:

// Add for "surfacegravity"
   Point3F surfaceGravity(0, 0, 0);

   // Sum up spring and wheel torque forces
   for (Wheel* wheel = mWheel; wheel < wend; wheel++)  {
      if (!wheel->tire || !wheel->spring)
         continue;

      F32 Fy = 0;
      if (wheel->surface.contact) {

		 // Ok, we're calculating a weird gravity
		 surfaceGravity += wheel->surface.normal;

Next, look for this:

// If we've added anything other than gravity, then we're no
   // longer at rest. Could test this a little more efficiently...
   if (mRigid.atRest && (mRigid.force.len() || mRigid.torque.len()))
      mRigid.atRest = false;

   mRigid.force += Point3F(0, 0, sWheeledVehicleGravity * mRigid.mass);

Change it to this:

surfaceGravity.normalize();
     surfaceGravity *= (mRigid.mass * sWheeledVehicleGravity);
     mRigid.force += surfaceGravity;

For extra credit, wrap this calculation with a chcek for a datablock flag, or a per object flag that will allow for normal gravity, and drive on wall gravity.



Next, very cheesy two wheeled vehicles:

This basically add's a gyroscopic force to allow vehicles to remain upright. It acts kinda like one of those clowns that you can punch over and over again, but it still
manages to upright itself.

In wheeledVehicle.cc, WheeledVehicle::updateForces, look for:
// Jet Force
   if (mJetting)
      mRigid.force += by * mDataBlock->jetForce;

Add this after it:

// Roll torque
	mRigid.torque += by * mSteering.x * mDataBlock->leanForce; // 1250
	// Gyro force
	F32 upCo = mDot(bx, Point3F(0, 0, 1));
	F32 yAngVel = mDot(by, mRigid.angVelocity);
	mRigid.torque += (by * mDataBlock->gyroForce * upCo) - (mDataBlock->gyroForceDampen * yAngVel * by); // 500

Now, we have to add a few datablock parameters:

Add these three lines to the private section of WheeledVehicleData in WheeledVehicle.h
F32 leanForce;						// Amount of lean to apply when turning
	F32 gyroForce;						// Amount of force to apply to upright the vehicle
	F32 gyroForceDampen;				// Dampen the spring force

Add these three lines to WheeledVehicleData::WheeledVehicleData()
gyroForce = 1600;
	gyroForceDampen = 500;
	leanForce = 1250;

Add these three lines to WheeledVehicleData::initPersistFields()
addField("leanForce", TypeF32, Offset(leanForce, WheeledVehicleData));
	addField("gyroForce", TypeF32, Offset(gyroForce, WheeledVehicleData));
	addField("gyroForceDampen", TypeF32, Offset(gyroForceDampen, WheeledVehicleData));

These three to WheeledVehicleData::packData
stream->write(leanForce);
	stream->write(gyroForce);
	stream->write(gyroForceDampen);

Finally, these three to WheeledVehicleData::unpackData
stream->read(&leanForce);
	stream->read(&gyroForce);
	stream->read(&gyroForceDampen);

Oh yeah, you need a small script change to make sure the wheels are setup right:

In car.cs replace onAdd with the following:

function WheeledVehicleData::onAdd(%this,%obj)
{
   // Setup the car with some defaults tires & springs
   for (%i = %obj.getWheelCount() - 1; %i >= 0; %i--) {
      %obj.setWheelTire(%i,DefaultCarTire);
      %obj.setWheelSpring(%i,DefaultCarSpring);
      %obj.setWheelPowered(%i,false);
   }
   
   // Steer front tires
   %obj.setWheelSteering(0,1);
//   %obj.setWheelSteering(1,1);

   // Only power the two rear wheels...
   %obj.setWheelPowered(1,true);
//   %obj.setWheelPowered(3,true);
}
Page «Previous 1 2
#1
09/25/2005 (1:12 am)
Do you know how to make a hover vehicle stick to walls/ceiling? I couldn't get it to work right :(
#2
09/27/2005 (4:07 pm)
cool lots of vehicle stuff.
#3
10/04/2005 (5:39 am)
Thank you Brian!
#4
10/10/2005 (4:13 pm)
This works with TSE to :)
#5
12/01/2005 (4:34 am)
Anyone get the 2 wheeled vehicle working? I'm testing it on a bike but it still falls over...
#6
12/01/2005 (5:10 am)
#7
12/01/2005 (5:16 am)
Can I ask you to check what is wrong with my current setup? I would really appreciate it...
Do you need to set other things expect for the steering and the poweredwheels?

I've been struggling all day to get it going.

Okay... I just needed to make the gyroforce more in script...

Has anyone else done more work on the Bike physics? I would like to put minds together on the biking side of things...
#8
01/16/2006 (4:03 am)
Nice resource. I made a new vehicle type for this called the StickyVehicle and i'm using it in my game do you want the stickyvehicle class?
#9
01/16/2006 (5:02 am)
#10
01/16/2006 (4:01 pm)
ok i'll post it later tonight but it works perfectly and the class is the same as the wheeled vehicle class except for this resource but I'll post it here or as a resource.

**EDIT**
Also I used to be a modder for a game called blockland and we had some really decent bikes without engine changes if someone wanted me to post the code but heres a pic for now
img390.imageshack.us/img390/9583/aior000060zc.th.png
#11
02/15/2006 (2:43 am)
#12
02/15/2006 (2:49 am)
Juan... Your mail keeps bouncing back :-(

Brian, do you have a direct link to that resource you've posted...
#13
02/15/2006 (3:03 am)
#14
02/15/2006 (3:46 am)
Juan... See mail...
#15
02/15/2006 (4:13 am)
BurNinG which one. The Sticky vehicle or the Bike?
#16
02/15/2006 (4:41 am)
#17
02/15/2006 (6:26 am)
Brian... The bike resource... didn't GarageGames send you the link... ?
I know you can access resources without them being approved, and by
just using the link they supplied.
#18
02/15/2006 (6:34 am)
here you go and you will need to add in damage stuff and other stuff because this was just for a start http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=9747
#19
02/21/2006 (6:43 am)
#20
02/04/2007 (12:06 am)
Is there a way to use this for a treaded vehicle such as tank? I'm not sure what to change in tankshape.cc/.h. Thanks
Page «Previous 1 2