Torque Vehicle Resource
by Mike Stoddart · 03/01/2003 (4:57 pm) · 172 comments
Download Code File
The following changes can probably be applied to any version of Torque post 1.1.2, including the current CVS head. Follow the instructions carefully. Post any problems or comments as a response to the resource. I extracted these code changes from a more complete system so I apologise profusely if I have left any code snippets out or made any mistakes. Also, this is my first resource, so forgive any obvious/stupid/blatant mistakes.
1) Add the vehicle.cs file into "fps/server/scripts". This file contains generic logic for vehicle management, including mounting and dismounting. There's also a simple function for creating a vehicle when the user presses the appropriate key.
2) Copy the car.cs file from racing/server/scripts/ to fps/server/scripts. Modify any references in the script from:
"~/data/shapes/buggy/";
to:
"~/data/shapes/car/";
3) Add the following to the end of the datablock in car.cs. Note that you must add extra code for each extra seat in your vehicle. If you have extra seats, you must setup the poses and mount point transform for them. The pose just specifies a pose that the player will take when they mount that seat. For example, the driver sits but other passengers may stand. The mount point transform specifies which direction the player faces in that seat.
5) Add these lines into the function onServerCreated() in fps/server/scripts/game.cs:
9) Add the following two lines to the function initClient() in fps/client/init.cs:
12) Comment out the following line in fps/client/scripts/default.bind.cs, otherwise you won't be able to use the 'e' key to enter/exit vehicles.
The following changes can probably be applied to any version of Torque post 1.1.2, including the current CVS head. Follow the instructions carefully. Post any problems or comments as a response to the resource. I extracted these code changes from a more complete system so I apologise profusely if I have left any code snippets out or made any mistakes. Also, this is my first resource, so forgive any obvious/stupid/blatant mistakes.
1) Add the vehicle.cs file into "fps/server/scripts". This file contains generic logic for vehicle management, including mounting and dismounting. There's also a simple function for creating a vehicle when the user presses the appropriate key.
2) Copy the car.cs file from racing/server/scripts/ to fps/server/scripts. Modify any references in the script from:
"~/data/shapes/buggy/";
to:
"~/data/shapes/car/";
3) Add the following to the end of the datablock in car.cs. Note that you must add extra code for each extra seat in your vehicle. If you have extra seats, you must setup the poses and mount point transform for them. The pose just specifies a pose that the player will take when they mount that seat. For example, the driver sits but other passengers may stand. The mount point transform specifies which direction the player faces in that seat.
maxMountSpeed = 0.1; mountDelay = 2; dismountDelay = 1; stationaryThreshold = 0.5; maxDismountSpeed = 0.1; numMountPoints = 1; mountable = true; mountPose[0] = "Sitting"; mountPointTransform[0] = "0 0 0 0 0 1 0"; isProtectedMountPoint[0] = false;4) Copy the whole "car" directory from racing/data/shapes/ into fps/data/shapes.
5) Add these lines into the function onServerCreated() in fps/server/scripts/game.cs:
exec("./vehicle.cs");
exec("./car.cs");6) Add the following to the end of fps/client/scripts/default.bind.cs:moveMap.bindCmd(keyboard, "e", "commandToServer('MountVehicle');", "");
moveMap.bindCmd(keyboard, "v", "commandToServer('AddCar');", "");7) Modify player.cs replacing the existing functions with the following. This basically prevents the player from mounting a vehicle by walking into it. It also moves the logic for vehicle management from the player file to vehicle.cs that was added earlier.function Armor::onMount(%this,%obj,%vehicle,%node)
{
onPlayerMount(%this,%obj,%vehicle,%node);
}
function Armor::onUnmount( %this, %obj, %vehicle, %node )
{
onPlayerUnmount(%this, %obj, %vehicle, %node);
}
function Armor::doDismount(%this, %obj, %forced)
{
doPlayerDismount(%this, %obj, %forced);
}
function Armor::onCollision(%this,%obj,%col,%vec,%speed)
{
if (%obj.getState() $= "Dead")
return;
// Try and pickup all items
if (%col.getClassName() $= "Item")
%obj.pickup(%col);
}8) Add the files vehicleDriverMap.cs vehiclePassengerMap.cs to fps/client/scripts.9) Add the following two lines to the function initClient() in fps/client/init.cs:
exec("./scripts/vehiclePassengerMap.cs");
exec("./scripts/vehicleDriverMap.cs");10) Add the following code to the end of fps/server/scripts/commands.cs. This code is executed when the player presses the key to dismount or change seats in the vehicle.function serverCmdMountVehicle(%client)
{
//Determine how far should the picking ray extend into the world?
%selectRange = 3;
// Only search for vehicles
%searchMasks = $TypeMasks::vehicleObjectType;
%pos = %client.player.getEyePoint();
// Start with the shape's eye vector...
%eye = %client.player.getEyeVector();
%eye = vectorNormalize(%eye);
%vec = vectorScale(%eye, %selectRange);
%end = vectorAdd(%vec, %pos);
%scanTarg = ContainerRayCast (%pos, %end, %searchMasks);
// a target in range was found so select it
if (%scanTarg)
{
%targetObject = firstWord(%scanTarg);
echo("Found a vehicle: " @ %targetObject);
onMountVehicle(%targetObject.getDataBlock(),
%client.player,
%targetObject);
}
else
{
echo("No object found");
}
}
function serverCmdDismountVehicle(%client)
{
doPlayerDismount(%client, %client.player, %true);
}
function serverCmdFindNextFreeSeat(%client)
{
echo("serverCmdFindNextFreeSeat " @ %client.nameBase);
// Is the vehicle moving? If so, prevent the player from switching seats
if (isVehicleMoving(%client.player.mvehicle) == true)
return;
%newSeat = findNextFreeSeat(%client,
%client.player.mvehicle,
%client.player.mvehicle.getDataBlock());
if (%newSeat != -1)
{
echo("Found new seat " @ %newSeat);
setActiveSeat(%client.player,
%client.player.mvehicle,
%client.player.mvehicle.getDataBlock(),
%newSeat);
}
else
{
echo("No next free seat");
}
}11) Add the following to the end of fps/client/scripts/client.cs:function clientCmdPopActionMap(%map)
{
echo("Popping action map " @ %map);
%map.pop();
}
function clientCmdPushActionMap(%map)
{
echo("Pushing action map " @ %map);
%map.push();
}12) Comment out the following line in fps/client/scripts/default.bind.cs, otherwise you won't be able to use the 'e' key to enter/exit vehicles.
moveMap.bind(keyboard, e, toggleZoom);
About the author
#2
03/01/2003 (5:02 pm)
Very cool mod... nice work.
#3
03/01/2003 (7:15 pm)
Glad to hear it. I really hope people can use and improve it. Let me know if you think I've missed anything out.
#5
03/03/2003 (9:26 am)
Thanks Joshua! :)
#6
I am a novice at this so please don't mind a few minor questions.
1) The 'MountVehicle' server call isn't in your example nor in the torque version I have. Should it be? That seems to be the key piece I missing in order to get this to work.
2) In vehicle.cs you say...
// *******************************************
// Function: onMountVehicle
//
// Inputs: Vehicle object
// Player datablock
// Vehicle object
//
// Outputs: None
//
// This function is called by the player's
// onMount function. It locates an empty
// seat in the vehicle and calls the function
// to perform the mount.
// *******************************************
But this is what is being called in the player onmount function...
function Armor::onMount(%this,%obj,%vehicle,%node)
{
onPlayerMount(%this,%obj,%vehicle,%node);
}
Should this be changed? It doesn't look like onMountVehicle is ever being called.
-------
That's it for the questions, thanks in advance for any help.
-EV
03/04/2003 (12:27 pm)
Awesome work Mike!I am a novice at this so please don't mind a few minor questions.
1) The 'MountVehicle' server call isn't in your example nor in the torque version I have. Should it be? That seems to be the key piece I missing in order to get this to work.
2) In vehicle.cs you say...
// *******************************************
// Function: onMountVehicle
//
// Inputs: Vehicle object
// Player datablock
// Vehicle object
//
// Outputs: None
//
// This function is called by the player's
// onMount function. It locates an empty
// seat in the vehicle and calls the function
// to perform the mount.
// *******************************************
But this is what is being called in the player onmount function...
function Armor::onMount(%this,%obj,%vehicle,%node)
{
onPlayerMount(%this,%obj,%vehicle,%node);
}
Should this be changed? It doesn't look like onMountVehicle is ever being called.
-------
That's it for the questions, thanks in advance for any help.
-EV
#7
Add the following to fps/server/scripts/commands.cs:
03/05/2003 (11:19 am)
Whoops, my apologies I missed that bit of code out. I've edited the resource to include it.Add the following to fps/server/scripts/commands.cs:
function serverCmdMountVehicle(%client)
{
//Determine how far should the picking ray extend into the world?
%selectRange = 3;
// Only search for vehicles
%searchMasks = $TypeMasks::vehicleObjectType;
%pos = %client.player.getEyePoint();
// Start with the shape's eye vector...
%eye = %client.player.getEyeVector();
%eye = vectorNormalize(%eye);
%vec = vectorScale(%eye, %selectRange);
%end = vectorAdd(%vec, %pos);
%scanTarg = ContainerRayCast (%pos, %end, %searchMasks);
// a target in range was found so select it
if (%scanTarg)
{
%targetObject = firstWord(%scanTarg);
echo("Found a vehicle: " @ %targetObject);
onMountVehicle(%targetObject.getDataBlock(),
%client.player,
%targetObject);
}
else
{
echo("No object found");
}
}
#8
moveMap.bindCmd(keyboard, "v", "commandToServer('AddCar');", "");
Perhaps I am missing something, but I cannot find this function anywhere.
p.s.
Thanks for this code!
EDIT!
Ooops I had exact match on. It was serverCmdAddCar I was looking for... Doh!
Sorry,
-Jeff
03/06/2003 (8:31 pm)
I did a full scope search for the function 'AddCar' from this bind:moveMap.bindCmd(keyboard, "v", "commandToServer('AddCar');", "");
Perhaps I am missing something, but I cannot find this function anywhere.
p.s.
Thanks for this code!
EDIT!
Ooops I had exact match on. It was serverCmdAddCar I was looking for... Doh!
Sorry,
-Jeff
#9
03/07/2003 (7:37 am)
I updated the resource to include a change to fps/client/scripts/default.bind.cs. You must comment out the line:moveMap.bind(keyboard, e, toggleZoom);
#10
i will on saturday test it
but have somebody from you a dont shoot script (for realm wars <= prog. with torque)??
sorry for my bad englisch i am a german young boy :-)
03/19/2003 (12:21 pm)
this is a cool scripti will on saturday test it
but have somebody from you a dont shoot script (for realm wars <= prog. with torque)??
sorry for my bad englisch i am a german young boy :-)
#11
I think you said this was your first resource and you might be happy to know that I had minimal troubles getting this to work even though it's my first mod. I also just started learning to code last week!
Thanks.
03/25/2003 (8:14 am)
First, awesome resource Mike. I think you said this was your first resource and you might be happy to know that I had minimal troubles getting this to work even though it's my first mod. I also just started learning to code last week!
Thanks.
#12
03/25/2003 (7:32 pm)
Thanks guys! Glad it worked okay for you.
#13
04/14/2003 (10:53 am)
Is there a way this could be modified to handle hover vehicles?
#14
numMountPoints = 2;
mountPose[1] = "Sitting";
mountPointTransform[1] = "0 0 0 0 0 1 0";
isProtectedMountPoint[1] = false;
04/20/2003 (11:41 pm)
Mike, I tried to add in another seat by adding the following code in car.cs but when I tried to chooose a seat, I cant. My player can only turn around but not move at all. Is there something wrong with the code I added and changed?numMountPoints = 2;
mountPose[1] = "Sitting";
mountPointTransform[1] = "0 0 0 0 0 1 0";
isProtectedMountPoint[1] = false;
#15
Anyway, did you add another mount node onto your model? If you were using Milkshape you would need to use bone joints named mount0, mount1, mount2 etc... for the extra mount nodes. mount0 for the first seating position.
-Jeff
04/21/2003 (2:04 pm)
This is odd, this morning I posted something for you and it isn't here.Anyway, did you add another mount node onto your model? If you were using Milkshape you would need to use bone joints named mount0, mount1, mount2 etc... for the extra mount nodes. mount0 for the first seating position.
-Jeff
#16
As Jeff said, did you add the extra mount point into your vehicle model?
Daniel: I imagine so. Hover cars are just vehicles and follow the same principles for seating etc..
04/23/2003 (1:51 pm)
Sorry; I didn't realise that there were extra posts here.As Jeff said, did you add the extra mount point into your vehicle model?
Daniel: I imagine so. Hover cars are just vehicles and follow the same principles for seating etc..
#17
I'm pretty sure I've transcribed the scripts from the resource correctly, but obviously something isn't working ... any idea what the problem might be? Even if you can only tell me what scripts there are that could be causing the problem (I'm assuming commands.cs and default.bind.cs are the likely culprits, correct?) that would be great, so I can kinda narrow the problem down.
Thanks for any help you can give me, I've only had Torque for a couple days so I'm still fairly new to all this.
EDIT: Nevermind, I got it working. I messed up my default.binds.cs file. *kicks self*
05/11/2003 (2:28 pm)
Whenever I try using either of the added binds (e to mount or v to make a vehicle) I get the "Syntax Error in Input" error in the console, and, of course, I can't mount and/or spawn vehicles. I've checked and all my scripts are compiling correctly, or so the console says.I'm pretty sure I've transcribed the scripts from the resource correctly, but obviously something isn't working ... any idea what the problem might be? Even if you can only tell me what scripts there are that could be causing the problem (I'm assuming commands.cs and default.bind.cs are the likely culprits, correct?) that would be great, so I can kinda narrow the problem down.
Thanks for any help you can give me, I've only had Torque for a couple days so I'm still fairly new to all this.
EDIT: Nevermind, I got it working. I messed up my default.binds.cs file. *kicks self*
#18
05/12/2003 (8:25 pm)
nice tutorial worked great. i was wondering how to implement a hover vehicle? thanks chris
#19
Since I move to linux from windows ... I said to myself, "Why not use Mike' vehicle resource?" and thats what I did. Almost immediately, I stumbled upon the missing serverCmdMountVehicle bug. So my immediate reaction was, "Heh. Mike forgot the function. Hmmm. I can do this by throwing a raycast and checking if theres a car.". Very clever I thought myself.
So, after an hour puttering around ... I came here to post my solution only to find that its already fixed!#@^%$#%^$#
Oh well, I'll just use your new function Mike ... its way simpler than what I did. :D
Thanks for the resource.
05/22/2003 (12:00 am)
Arrrggghhh! I downloaded this resource last March then I totally forgot about it since I'm using Bravetree's car pack. :DSince I move to linux from windows ... I said to myself, "Why not use Mike' vehicle resource?" and thats what I did. Almost immediately, I stumbled upon the missing serverCmdMountVehicle bug. So my immediate reaction was, "Heh. Mike forgot the function. Hmmm. I can do this by throwing a raycast and checking if theres a car.". Very clever I thought myself.
So, after an hour puttering around ... I came here to post my solution only to find that its already fixed!#@^%$#%^$#
Oh well, I'll just use your new function Mike ... its way simpler than what I did. :D
Thanks for the resource.
#20
This is just a thought ... but if I am to implement a hover vehicle ... I would use the hoverVehicle codes. I suggest you take a look at the engine/game/vehicles directory or if you have $15USD to burn ... try Bravetree: Jet Pack Pro.
05/22/2003 (12:14 am)
UCF GAMES Lab (heh. you sure your sticking with this name?)This is just a thought ... but if I am to implement a hover vehicle ... I would use the hoverVehicle codes. I suggest you take a look at the engine/game/vehicles directory or if you have $15USD to burn ... try Bravetree: Jet Pack Pro.
Torque Owner Roger Smith