Set vehicle speed
by CodingChris · in Torque Game Engine · 09/25/2007 (11:14 am) · 4 replies
Hi,
is there a way in script to set the speed of a vehicle?
Thanks,
Christian
is there a way in script to set the speed of a vehicle?
Thanks,
Christian
About the author
#2
09/26/2007 (8:07 am)
I just want to stop a wheeledvehicle, so I need to set the current speed.
#3
If you want to stop it instantly, the client needs to send a new commandToServer asking it to stop the vehicle. Upon receiving the command, the server would then stop the vehicle. I didn't see a way to implement the stopping by script without adding one small method to the vehicle or WheeledVehicle class in the C++ code.
in client/scripts some file attached to a key or however
In server/scripts/commands.cs
You would also have to implement the setVelocity method on the Vehicle or WheeledVehicle C++ class. The base class ShapeBase method is empty.
I can think of problems using this would cause. Try it and see. Be sure and try it with a networked client. (Have someone stop themselves while you are in another car watching them.) Does the other car warp strangely when you use it? Why doesn't the car animate as you would expect if someone braked to a quick stop? Why does the car start up again if the player keeps the throttle on? Why didn't I just override the brake to cause him to stop inthe first place? ;) )
09/26/2007 (1:49 pm)
Wheeled vehicles are usually stopped by sending brake moves. These cause the vehicle to slow until it stops. If you want to stop it instantly, the client needs to send a new commandToServer asking it to stop the vehicle. Upon receiving the command, the server would then stop the vehicle. I didn't see a way to implement the stopping by script without adding one small method to the vehicle or WheeledVehicle class in the C++ code.
in client/scripts some file attached to a key or however
function stopMyVehicleCommand( ) {
commandToServer('StopMyVehicle');
}
someKeyMap.bind( keyboard, "ctrl s", stopMyVehicleCommand );In server/scripts/commands.cs
// done as example to solve problem of:
// http://www.garagegames.com/mg/forums/result.thread.php?qt=67633
// NOTE: This version only works if client is a player mounted in a vehicle.
// Usage: commandToServer('StopMyVehicle');
function serverCmdStopMyVehicle(%client)
{
%vehObj = %client.player.mVehicle;
if (isObject(%vehObj)) {
%vehObj.setVelocity("0 0 0");
}
}You would also have to implement the setVelocity method on the Vehicle or WheeledVehicle C++ class. The base class ShapeBase method is empty.
// MVJ added to implement "stopping" via script
void Vehicle::setVelocity(const VectorF& vel)
{
mRigid.linMomentum = vel;
setMaskBits(PositionMask);
}I can think of problems using this would cause. Try it and see. Be sure and try it with a networked client. (Have someone stop themselves while you are in another car watching them.) Does the other car warp strangely when you use it? Why doesn't the car animate as you would expect if someone braked to a quick stop? Why does the car start up again if the player keeps the throttle on? Why didn't I just override the brake to cause him to stop inthe first place? ;) )
#4
09/27/2007 (8:03 am)
You can do this script-only, by using the existing applyImpulse method rather than implementing setVelocity in the code. // done as example to solve problem of:
// http://www.garagegames.com/mg/forums/result.thread.php?qt=67633
// NOTE: only works if client is a player mounted in a vehicle.
// commandToServer('StopMyVehicle');
function serverCmdStopMyVehicle(%client)
{
%vehObj = %client.player.mVehicle;
if (isObject(%vehObj)) {
// %vehObj.setVelocity("0 0 0");
// script only solution
// impulse = momentum change to stop the vehicle
%impulse = VectorScale(%vehObj.getVelocity(), -%vehObj.getDatablock().mass);
// note: this isn't perfectly correct if vehicle uses a non-zero %vehObj.getDatablock().massCenter.
// (A small attitude bump will be created.)
// Would need to convert massCenter vector from body to world coordinates to use it.
%pos = %vehObj.getPosition();
%vehObj.applyImpulse(%pos,%impulse);
}
}
Torque 3D Owner Matthew Jessick
Set a maximum speed for the vehicle?
Set current speed (which thereafter immediately starts to change)?
Set a speed that is constant thereafter?
Set a speed that the vehicle should go to and thereafter maintain? (an autopilot)
What type of vehicle?
Do this once, or dynamically during the game?
For all vehicles of a particular type, or just one?
In general, the answer is probably: No or not easily. That is because the vehicles are controlled by physical calculations on the server affected by the drivers changing of the controls.