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:
Change it to this:
Next, look for this:
Change it to this:
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:
Add this after it:
Now, we have to add a few datablock parameters:
Add these three lines to the private section of WheeledVehicleData in WheeledVehicle.h
Add these three lines to WheeledVehicleData::WheeledVehicleData()
Add these three lines to WheeledVehicleData::initPersistFields()
These three to WheeledVehicleData::packData
Finally, these three to WheeledVehicleData::unpackData
Oh yeah, you need a small script change to make sure the wheels are setup right:
In car.cs replace onAdd with the following:
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);
}About the author
#22
cm4msaa7.com/stickyvehicle.zip
Just drop the *.cc and *.h files into your engine/game/vehicles directory, add to your Visual Studio project, compile. The stickycar.cs file is more of a stripped reference than something useful, because it references particles and stuff that aren't present, but should basically work just fine.
Basically, I just did a global find/replace on the cc, h, and cs files and replaced "Wheeled" with "Sticky", creating the StickyVehicle class. Much to my surprise, it worked fine! There is other stuff...for example, I had to edit my player.cs so that the player would mount the StickyVehicle when he collided with it...but it's pretty easy.
Down the road I'd like to have it done more elegantly within good ole WheeledVehicle as an option, perhaps a dynamic variable or something. So a car could pick up a "stickycar" item, for example, and then be able to drive on walls.
02/23/2007 (12:01 pm)
If it's convenient for anyone, I've implemented the drive on walls as a separate vehicle class. You can get it here:cm4msaa7.com/stickyvehicle.zip
Just drop the *.cc and *.h files into your engine/game/vehicles directory, add to your Visual Studio project, compile. The stickycar.cs file is more of a stripped reference than something useful, because it references particles and stuff that aren't present, but should basically work just fine.
Basically, I just did a global find/replace on the cc, h, and cs files and replaced "Wheeled" with "Sticky", creating the StickyVehicle class. Much to my surprise, it worked fine! There is other stuff...for example, I had to edit my player.cs so that the player would mount the StickyVehicle when he collided with it...but it's pretty easy.
Down the road I'd like to have it done more elegantly within good ole WheeledVehicle as an option, perhaps a dynamic variable or something. So a car could pick up a "stickycar" item, for example, and then be able to drive on walls.
#23
i was fooling around with some code, but then i discovered i dont really know how to check what the name of the texture on a surface is.
could some one point me maybe in the right direction? or maybe know if this is even possible? if not then i can get started to get it to work by writing my own surface texture checker, but why reinvent the wheel?
thanks to anyone who has something to say :)
08/11/2008 (9:14 am)
hey i was jsut wondering, could this be implememnted to check the texture of the face, and if it is a certain texture, its off, else surface gravity is on?? i was fooling around with some code, but then i discovered i dont really know how to check what the name of the texture on a surface is.
could some one point me maybe in the right direction? or maybe know if this is even possible? if not then i can get started to get it to work by writing my own surface texture checker, but why reinvent the wheel?
thanks to anyone who has something to say :)
#24
now, im kinda new to the console/source interfacing.
like im comfortable with the source code, and im comfortable with the scripting, really comfortable
but i havent really done anything like checking a datablock flag
would i use this resource?
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=7088
or does anyone know how i would do that???
08/11/2008 (4:47 pm)
k, scrap the above idea, its way easier and faster to jsut have (as the author suggested) to wrap the function in a per object datablock flagnow, im kinda new to the console/source interfacing.
like im comfortable with the source code, and im comfortable with the scripting, really comfortable
but i havent really done anything like checking a datablock flag
would i use this resource?
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=7088
or does anyone know how i would do that???
#25
is there a way to make the object have the gravity? like, right now, its a per vehicle gravity, i was wondering if it can be per track or dts gravity, so that some enviroments have surface gravity while oters do not
08/16/2008 (11:37 am)
k, i was able to get the per datablock flag going, but its not what i wanted:Sis there a way to make the object have the gravity? like, right now, its a per vehicle gravity, i was wondering if it can be per track or dts gravity, so that some enviroments have surface gravity while oters do not
#26
08/16/2008 (2:10 pm)
Tom, what you want are "physical zones". Add one in your mission editor and check out the properties--enjoy.
#27
08/17/2008 (10:15 am)
oh, so what u mean is that i could have a physical zone, and then when a player enteres that, the bike would have surface gravity, and once he exits, it would no longer have surface gravity?
#28
08/17/2008 (12:57 pm)
Not stock. You'll need to delve into the source a bit. My guess in the physical zone code. Might not be too bad. 
Torque Owner Lee Latham
Default Studio Name