How to control the velocity of a vehicle in script?
by Matthew Genge · in Torque 3D Professional · 02/15/2012 (7:14 am) · 4 replies
Dear All,
I am trying to make a paraglider with a flying vehicle and I need to be able to control it's forward velocity in script rather than by user key presses. I have found I can control the velocity simply by calling the same function bound to the forward key...however...something very weird happens. The mounted player model's pose changes when I call the function from script, it doesn't change when the player presses the forward button and runs the function.
The function is in default.binds.cs and is:
I've tried calling the function to control the velocity from both server-side and client script, but the same result. All I can guess is that when the player presses the button and calls the script the engine realises the player model is mounted and applies the velocity to the vehicle, when it is called from script, for some reason, the player model updates its pose in response to the key press. If anyone has an idea how to prevent this from happening please tell. Or if you know of another way of controlling vehicle velocity in script (setVelocity does not seem to have any effect).
Finally, I've searched the script and can't find where $mvForwardAction is being used...so presumably the engine uses it. Is the scope of such globals restricted to the client they are running on?
Many thanks,
Matt
I am trying to make a paraglider with a flying vehicle and I need to be able to control it's forward velocity in script rather than by user key presses. I have found I can control the velocity simply by calling the same function bound to the forward key...however...something very weird happens. The mounted player model's pose changes when I call the function from script, it doesn't change when the player presses the forward button and runs the function.
The function is in default.binds.cs and is:
function moveforward(%val)
{
$mvForwardAction = %val * $movementSpeed;
}I've tried calling the function to control the velocity from both server-side and client script, but the same result. All I can guess is that when the player presses the button and calls the script the engine realises the player model is mounted and applies the velocity to the vehicle, when it is called from script, for some reason, the player model updates its pose in response to the key press. If anyone has an idea how to prevent this from happening please tell. Or if you know of another way of controlling vehicle velocity in script (setVelocity does not seem to have any effect).
Finally, I've searched the script and can't find where $mvForwardAction is being used...so presumably the engine uses it. Is the scope of such globals restricted to the client they are running on?
Many thanks,
Matt
About the author
I am an Earth Scientist who likes writing games for fun.
#2
In reply to the question on the scope of globals - it depends on where you use them. In a single-player game, they are actually global. In a multiplayer game, they're global on the machine that creates them. In other words, if you create a client-side global variable in script, will have its own machine-local version of that variable - on each client it can contain different values even when all clients are connected to the same game server. Likewise, global variables created on the server are only global to the server - the clients don't have access to it.
In order to get over these hurdles you would have to set up a way to ensure that all clients and the server shared certain global variables - or simply use the clientCmd/serverCmd mechanism and the messaging system to get data back and forth as needed.
02/15/2012 (1:10 pm)
Sweet!In reply to the question on the scope of globals - it depends on where you use them. In a single-player game, they are actually global. In a multiplayer game, they're global on the machine that creates them. In other words, if you create a client-side global variable in script, will have its own machine-local version of that variable - on each client it can contain different values even when all clients are connected to the same game server. Likewise, global variables created on the server are only global to the server - the clients don't have access to it.
In order to get over these hurdles you would have to set up a way to ensure that all clients and the server shared certain global variables - or simply use the clientCmd/serverCmd mechanism and the messaging system to get data back and forth as needed.
#3
Thanks for the info.
I've now extended my code so I can set the up, right and forwards velocity of the flying vehicle in body fixed motions. As long as the various drags are set to zero it works nicely, otherwise it will drag in between calls by schedule. You can handle acceleration with your own script using this. Thought I'd post it in case it was useful to anyone.
Matt
02/15/2012 (3:12 pm)
Cheers Richard,Thanks for the info.
I've now extended my code so I can set the up, right and forwards velocity of the flying vehicle in body fixed motions. As long as the various drags are set to zero it works nicely, otherwise it will drag in between calls by schedule. You can handle acceleration with your own script using this. Thought I'd post it in case it was useful to anyone.
function serverCmdInitParaglider(%client){
//Initialise the velocity of the paraglider
%obj = %client.player.mVehicle;
%pos = getWords(%obj.getTransform(),0,2);
%obj.ChangeSpeed("10 0 0.5");
%obj.schedule(33,updateParaglider);
}
function SceneObject::updateParaglider(%obj){
%obj.ChangeSpeed("5.0 0 0.05");
%obj.schedule(33,updateParaglider);
}
function SceneObject::ChangeSpeed(%obj,%speed){
//Get Body Centred velocity vectors
%Fvec = VectorScale(%obj.getForwardVector(), getWord(%speed,0));
%Rvec = VectorScale(%obj.getRightVector(), getWord(%speed,1));
%Uvec = VectorScale(%obj.getUpVector(), getWord(%speed,2));
%body = VectorAdd(%Fvec,%Rvec);
%body = VectorAdd(%Uvec,%body);
//Project the body vector onto x,y,z
%xvel = VectorDot(%body, "1 0 0");
%yvel = VectorDot(%body, "0 1 0");
%zvel = VectorDot(%body, "0 0 1");
//Determine the impulse to get to target velocity
%vel = %obj.getVelocity();
%veldiff = %xvel - getWord(%vel,0) SPC %yvel - getWord(%vel,1) SPC %zvel - getWord(%vel,2);
%impulse = VectorScale(%veldiff, %obj.getDatablock().mass);
%pos = getWords(%obj.getTransform(),0,3);
%obj.applyImpulse(%pos, %impulse);
}Matt
#4
02/15/2012 (4:52 pm)
Good work, thanks man!
Matthew Genge
HPTWare
function serverCmdInitParaglider(%client){ //Initialise the velocity of the paraglider %obj = %client.player.mVehicle; %pos = getWords(%obj.getTransform(),0,2); %obj.applyImpulse(%pos, "0 1000.0 0"); }