Game Development Community

dev|Pro Game Development Curriculum

Vehicle Tutorial with Custom Vehicle

by Badguy · 01/29/2002 (9:58 am) · 47 comments

Download Code File

I posted this because some people are still having trouble
I Think this might clear some of that up :)

ok First I wanna thanks GarageGames for the great job everyone is doing on the team there
.. this is the Best thing since Linux :)(not a joke)

nuff said lets get started...

ok quite simply I think covering the basics of the vehicle are important
after resolving actually getting a shape to use as a valid vehicle
anything will do really as long as it has col-1 and a detail mesh

but if we cant get our hands on one I will provide a shape my modeler is willing to part with that I exported :)
(this means dont expect All the bells and whistles .. just enuff to werk)


the Next thing that is important is defining the script.
the script must define a datablock for the vehicle of choice
one can examine the VehicleData class and the desired vehicle type's class and the corresponding function(s) initPersistFields()
as this function exports the variables (and sometimes functions) available to the script


the script must also define functions that the code can callback upon to relate to the vehicle



the important ones are :
create()
onAdd()
onRemove()
onImpact()
onDamage()
damage()
onDestroyed()

there are more dont expect a full list here I havent even found em all yet myself im sure
there is still much more to vehicles I will delve a little deeper ..

Sounds : are easy to setup by simply declaring an AudioProfile datablock
and having it available by the time you run the vehicle script
simply associate the AudioProfile to the particular field you wish to fill
also you will notice EffectProfile has been removed it was for Force Feedback.(I think)

Particle Systems : can be defined for the vehicle are extensive
but not necessary .. tho easy to setup :)


here are some example's :

datablock AudioProfile(DroneSoftImpactSound)
{
    filename = "~/data/sound/Vehicles/Drone/SoftHit.wav";
    description = AudioClose3d;
    preload = true;
};
now simply when declaring the datablock that I wish to have this profile be used :
impactWaterEasy = DroneSoftImpactSound;
impactWaterEasy is an exported variable from the VehicleData datablock class type

Now for Particle Systems
we will need to define two seperate datablocks one for the particle we wish to use
one for the emitter that will release the particle
here is an example :
datablock ParticleData(DroneExplosionParticle)
{
    dragCoefficient = 2;
    gravityCoefficient = 0.2;
    inheritedVelFactor = 0.2;
    constantAcceleration = 0.0;
    lifetimeMS = 750;
    lifetimeVarianceMS = 150;
    textureName = "~/data/shapes/rifle/smokeParticle";
    colors[0] = "0 0.2 1 1.0";
    colors[1] = "0 0.2 1 0.0";
    sizes[0] = 0.5;
    sizes[1] = 1.0;
};

datablock ParticleEmitterData(DroneExplosionEmitter)
{
    ejectionPeriodMS = 7;
    periodVarianceMS = 0;
    ejectionVelocity = 1;
    velocityVariance = 1.0;
    ejectionOffset = 0.0;
    thetaMin = 0;
    thetaMax = 60;
    phiReferenceVel = 0;
    phiVariance = 360;
    overrideAdvances = false;
    particles = "DroneExplosionParticle";
};
now to make use of this particlar explosion emmiter Particle system we will need one more datablock :
datablock ExplosionData(DroneVehicleExplosion)
{
    explosionShape = "~/data/shapes/vehicles/Drone/explosion.dts";

    particleEmitter = DroneExplosionEmitter;
    particleDensity = 50;
    particleRadius = 0.2;

    faceViewer = true;
    explosionScale = "1 1 1";

    shakeCamera = true;
    camShakeFreq = "10.0 11.0 10.0";
    camShakeAmp = "1.0 1.0 1.0";
    camShakeDuration = 0.5;
    camShakeRadius = 10.0;
};
there now we have gone all the way to explosion im sure your all with me on this as it is so simple weve covered sounds particle systems explosions and are ready to actually dive in and define the datablock and write the functions to support it


ok well I think it is best for all of us if at this point I just provide a couple files as converting all the code to html is a pain...
and I dont think i can convert the *.dts ,max file and the png to html :)
ok im providing :
Drone.dts
Drone.max
Drone.png
Vehicle.cs (a Simple beggining)
Drone.cs (another Simple beggining)

found at the bottom of this page

add the vehicle.cs and the Drone.cs to be executed in
/server/scripts/game.cs I executed mine just before player.cs as such :

exec("./Vehicles/Vehicles.cs");
   exec("./Vehicles/Drone.cs");
   exec("./player.cs");


be sure to have the files in the place you specify....

you will need to mount the vehicle with the player
this can be done in :
/server/scripts/player.cs

find the function
[b]Armor::onCollision(..)[/b]
and change :
   if((%this.className $= WheeledVehicleData) && %obj.mountVehicle 
       && obj.getState() $= "Move" && %col.mountable)
[b]and the corresponding codeblock[/b]
  to...

   if((%this.className $= WheeledVehicleData || %this.className $= FlyingVehicleData) 
      && %obj.mountVehicle && %obj.getState() $= "Move")
   {
        if(%col.mountable)
       {
// Only mount drivers for now.
            %node = 0;
            %col.mountable=false;// Set Vehicle Unmountable
            %col.mountObject(%obj,%node);
            %obj.mVehicle = %col;
         }
      else
      {
         %obj.damage(0, VectorAdd(%obj.getPosition(),%vec), 20, "Impact");// to Run over players
         // %vecLen * %this.speedDamageScale
      }
   }
and then you will need to make sure you can unmount and remount over and over
this is done by fixing in player.cs first find the function doDismount()
then after :
if (%forced && %success == -1)
    %pos = %oldPos;
[/b]
[b]insert[/b] :
      %obj.unmount();
      %obj.setControlObject(%obj);
      if(%obj.mVehicle)
          %obj.mVehicle.getDataBlock().playerDismounted(%obj.mVehicle, %obj);
      %obj.mountVehicle = false;
      %obj.schedule(4000, "MountVehicles", true);
[b]Notice :
the change in the schedule function
as im sure a typo was left after the changes
was trying to call nonexhistant function setMountVehicle(..) which is now named MountVehicles(..):)

ok one more thing .. add the booster so you can right click for boost
simply find ../client/scripts/default.bind.cs
and below alttrigger(..) place :
function mouseJet(%val)
{
   $mvTriggerCount3++;
}

first Find and copy a config.cs into your ../client/.. folder (which I recommend for client binds) or you can add to the default.bind.cs

moveMap.bind(mouse, "button1", mouseJet);

as I havent done any work towards spawning the vehicle
you can either add it with the editor
...or add a function ill give thats hacker to ctrl-f for a vehicle placed at your spot :)
then add to it
moveMap.bindCmd(keyboard, "ctrl f", "commandToServer(\'addflyer\');", "");

insert in commands.cs :

function serverCmdAddFlyer(%client)
{
    if(%client.player$="")
      return;
    if(%client.player)
      if(%client.player.isMounted())
         return;
    %vehicle = new FlyingVehicle() 
    {
       dataBlock = Drone;
    };
    %vehicle.mountable = true;
    %vehicle.setEnergyLevel(60);
    %vehicle.setTransform( %client.player.getEyeTransform() );
   // %vehicle.setGravity(0);// Optional recommended :)
    %vehicle.nextWeaponFire = 1;
    %vehicle.schedule(5500, "playThread", $ActivateThread, "activate");
    messageAll('BadguyOutPut','%1 Added a %2!',%client.name, %vehicle.getDataBlock().getName());
    MissionCleanup.add(%vehicle);
}
ok so noone is perfect im sure there are maybe more errors .. I hope not :)
it should be all good tho and anyone should be able to work thru this and get satisfactory vehicles running ... Goodluck

oh yeah .. and stay away from the organics as thier collision detail is not valid
I recommendassert after the following :
AssertFatal(pAccel != NULL, "Error, no accel!");
found in ../engine/game/tsStatic.cc line 488
might I suggest
AssertFatal(pAccel->vertexList[0] != NULL, "Error, no accel vertexList!");
to hopefully allow the program to back out gracefully using the platform defined methods

P.S. this engine is great the couple weeks ive had it have been really exciting
as me and my team mold to fill its needs ill be here reading the forums and hopefully learning lots and helping out lots :)

not all functionality is provided ..
for example you must write your own damage(..) function
Updated Zip file with changes and max file
Page «Previous 1 2 3 Last »
#1
01/27/2002 (5:26 pm)
umm .. my file is not available .. :(
*sniffle*
how do I fix that?
*fixed*
tx GG :)
#2
01/29/2002 (1:16 pm)
Hrmmm... Not quite working for me. Whenever I tried to spawn it in the editor; in the console I would get an error somewhere along the lines of function FlyingVehicleData::onAdd is not found. Any idea what I can do?

Thank you for your help,
Andrew
#3
01/29/2002 (5:50 pm)
Can we still d/l the max file somewhere pretty please?
#4
01/29/2002 (6:25 pm)
Andrew :
so you dont get a vehicle at all?
and the only error is the onAdd?
it should be there .
the onAdd error is; im sure from my conventions in calling it


Jim:
as soon as our website is complete ill make it available there :)
#5
01/29/2002 (9:23 pm)
I'll try and go through the thing step by step and double check again tomorrow. I'll tell you how it goes.
#6
01/29/2002 (10:34 pm)
i cant seem to get this to work right either
(i have unlimited space on fileplant and permission to use this space for the developement of torque, if you want i will upload a complete zip of this tutorial for you)
oce it gets working correctly
www.planetquake.com/noescape/torque/ss/drone.jpg
[added]
i forgot to mention i can get it in game with the staic shapes ,but how s it spawed (like a weapon,and useable) course im not going to actualy use this in our game (be more like a horse and cart) but this will be a valuable learning thing :)
#7
01/29/2002 (11:11 pm)
hmm I dont get it .. right now with fresh fps extracted I will follow the tutorial myself hopefully I will stumble across these errors your having ..
Gary :
the only time ive had that happen is when I first used the tribes file with my version to see how the vehicle worked and the category was spelt different in the tribes thus not allowing it to exhist in the vehicle field :)

- tg: Renamed GameBase catagory to category
(changelog snippet line 611)
so if youve changed the spelling? hmm
other than that make sure the console log when loading the files
Vehicles.cs &
Drone.cs
there are no problems .. if there are not and you have the category spelt proper
#8
01/29/2002 (11:38 pm)
Ok sorry there are errors :)
now fixed, the movemap command Cannot go where it was and in Drone.cs the create function should not be :
function Drone::create(..)
instead :
function FlyingVehicleData::create(..)
and now the max file is in the zip as well
>:)
#9
01/30/2002 (6:17 am)
i did a search and category is spelled correct in all of torque

heh ya , i keep for getting about the console check

the ss above shows the error i get (i have no idea what it means (new to this scripting stuff) but below is a link to my player.cs if you would like to look at it (i probaly didnt understand wher the code should go in it

click here to veiw my player.cs (it's a player.txt for this link)
#10
01/30/2002 (6:28 am)
Okay, sounds good. I had tried changing the Drone::create to FlyingVehicleData::Create yesterday thinking that that was the cause and I was able to spawn it. I was not able to mount it however. I guess I will see if I can do it from scratch. Thank you kind sir for doing a tutorial on vehicles and for answering my question quickly!

Andrew
#11
01/30/2002 (6:47 am)
yup same here ,i decided to try it for the heck of it ,and after updateing the cs , id oses spawn either way (movemao bind and through shapes) , but is un mountable. yes i appreciate the help also
#12
01/30/2002 (7:34 am)
Hmmmmmmm same problem... I can spawn it but, I can't mount. After screwing around with it for a while I did get it to damage the player on impact but, I still can't mount it. I think it has to do with your wording. I am used to tutorials being, this is exactly what you take out and this is exactly what you put in. You know, spoon-fed :). I think that is the problem that Gary and I are having. We are not exactly sure where to place your shiny new code. If I could see the bottom half of your player.cs file badguy I am sure I could track down the problem.

Andrew
#13
01/30/2002 (5:17 pm)
ok looking at the player.cs link Gary has provided I see the changes to
doDismount(..)
have not been made
in the player.cs doDismount code you Must replace :
%obj.mountVehicle = false;
   %obj.schedule(4000, "setMountVehicle", true);
with:
%obj.unmount();
      %obj.setControlObject(%obj);
      if(%obj.mVehicle)
          %obj.mVehicle.getDataBlock().playerDismounted(%obj.mVehicle, %obj);
      %obj.mountVehicle = false;
      %obj.schedule(4000, "MountVehicles", true);
tho this is for getting in and out ..
and wont help you get in :(
umm .. i see your player.cs is not compiling
and if it dont compile then you cannot expect any changes in it to work it must compile first
as for the error youll find you need one more codeblock brace (}) like this :
if((%this.className $= WheeledVehicleData || %this.className $= FlyingVehicleData) 
      && %obj.mountVehicle && %obj.getState() $= "Move")
   {
        if(%col.mountable)
       {
           %node = 0;
            %col.mountable=false;
            %col.mountObject(%obj,%node);
            %obj.mVehicle = %col;
         }
      else
      {
         %obj.damage(0, VectorAdd(%obj.getPosition(),%vec), 20, "Impact");
      }
  } // the New one you didnt have

im sure after you get this file to compile .. you wont have no more problems :)
I hope :)
And I used the player.cs in that link to mount after i fixed the codeblock error
the only things really needed to mount a vehicle are
A: the player mountVehicle flag must be true
B: the vehicle mountable flag must be true
C: the Armor::onCollision(..) function must when hitting vehicle type allow you to mount it
after checkin all of these it should work no trouble
#14
01/30/2002 (7:18 pm)
cool ,that did the trick, thanks so much , i reuploaded mu payer.cs/txt for andrew to look at
click here

now i should beable to go form there and get it to react the way i want

(the whole problem was,,, i wasnt sure where to put the code in player.cs)

thanks again :)
#15
01/30/2002 (7:57 pm)
Quote from beginning of article:

"ok quite simply I think covering the basics of the vehicle are important
after resolving actually getting a shape to use as a valid vehicle
anything will do really as long as it has col-1 and a detail mesh"

What is a col-1?
#16
01/30/2002 (9:20 pm)
Thank you Gary and Badguy! I am sure that will work for me! Emanuel col-1 is the collision box.
#17
01/30/2002 (9:38 pm)
yes Emanuel the col-1 is the collision mesh you provide for the shape in the 3dtool you are exporting the model from

Good luck andrew :)
glad to hear it's all worked out now gary!
so I read it over and I dont know how to be more clear about which files to open and which functions to change..
I guess I will bold it?
(doing it now)
#18
01/31/2002 (4:27 am)
You should probably take a longer snippet of code and make your changes bold.
Going to try the fix later on ;)
#19
01/31/2002 (11:33 am)
heheh OK im the modeller for this programming god called badguy : ) one thing we should mention is that when you export a shape from max to DTS you should have a *.cfg file in the directory where the MAX file resides this config file lets the exporter know what is to be exported IE: mountpoints , eye , mass, objects its is referred to quiet often in most of the exporting literature available on this awesome site I myself am not sure as to how much this file helps but I do it religiously and most of every shape I export seems to work hope this helps Joker
#20
01/31/2002 (11:34 am)
sorry about the ponctuation : ) me bad
Page «Previous 1 2 3 Last »