vectors and setvelocity
by Peter K · in Torque 3D Professional · 08/26/2012 (5:26 pm) · 4 replies
I'm trying to get a newly created item to move out in front of the player in the directions hes facing.
The code below works partially. It works great for +/-90 degrees. The item moves out in front of the player, however,turning more than +/- 90 degrees and the item just stays at the player. So, I'm pretty sure there's some vector magic I need but to be honest that's a weak area for me. Anyone have any idea's or suggestions?
The code below works partially. It works great for +/-90 degrees. The item moves out in front of the player, however,turning more than +/- 90 degrees and the item just stays at the player. So, I'm pretty sure there's some vector magic I need but to be honest that's a weak area for me. Anyone have any idea's or suggestions?
function moveitem()
{
%testo=ItemData::createItemnorotate("testobject");
%testo.settransform(LocalClientConnection.player.gettransform());
%testo.setvelocity(vectorscale(%testo.getforwardvector(),50));
}
function ItemData::createItemnorotate(%data)
{
%obj = new Item()
{
dataBlock = %data;
static = false;
rotate = false;
};
return %obj;
}About the author
#2
Essentially, when i call moveitem I want to create a new item and push it out in front of the direction the player is facing. right now with the above code, the item only moves out in front of the player if he's facing within the first 180 degrees. I'm sorry I'm having a hard time explaining it I think. So I know these directions don't mean anything but as an example, if I zoom out and look down at the player and he is facing the top of the screen and I call moveitem (which isn't really an accurately descriptive function name)the item appears and moves toward the top of the screen. If the player rotates move then +/- 90 degrees, the object no longer moves forward. it just sits at the player position. So, if the player now is facing toward the bottom of the screen, looking down from above for example, the item doesn't seem to move.
I've also tried applyimpulse in place of setvelocity as either would work but I had even less success than this.
Sorry for my confusing test coding :)
08/27/2012 (7:14 am)
Thanks Frank, That's a good idea, I'll add a check in there to be safe. Essentially, when i call moveitem I want to create a new item and push it out in front of the direction the player is facing. right now with the above code, the item only moves out in front of the player if he's facing within the first 180 degrees. I'm sorry I'm having a hard time explaining it I think. So I know these directions don't mean anything but as an example, if I zoom out and look down at the player and he is facing the top of the screen and I call moveitem (which isn't really an accurately descriptive function name)the item appears and moves toward the top of the screen. If the player rotates move then +/- 90 degrees, the object no longer moves forward. it just sits at the player position. So, if the player now is facing toward the bottom of the screen, looking down from above for example, the item doesn't seem to move.
I've also tried applyimpulse in place of setvelocity as either would work but I had even less success than this.
Sorry for my confusing test coding :)
#3
Straight from scripts/server/commands.cs:
The throw is implemented in scripts/server/inventory.cs on the ShapeBase class.
So, you can bind your command to pass your new object into the call to the server command:
Hope that gets it going - you might even overload that to allow you to pass in the desired velocity, but you might check your object's datablock for a throwForce field - if you add that field to any ShapeBase object's datablock it should affect the throw distance.
08/27/2012 (9:22 am)
Might be better off using the player class' throw() method.Straight from scripts/server/commands.cs:
// ----------------------------------------------------------------------------
// Throw/Toss
// ----------------------------------------------------------------------------
function serverCmdThrow(%client, %data)
{
%player = %client.player;
if(!isObject(%player) || %player.getState() $= "Dead" || !$Game::Running)
return;
switch$ (%data)
{
case "Weapon":
%item = (%player.getMountedImage($WeaponSlot) == 0) ? "" : %player.getMountedImage($WeaponSlot).item;
if (%item !$= "")
%player.throw(%item);
case "Ammo":
%weapon = (%player.getMountedImage($WeaponSlot) == 0) ? "" : %player.getMountedImage($WeaponSlot);
if (%weapon !$= "")
{
if(%weapon.ammo !$= "")
%player.throw(%weapon.ammo);
}
default:
if(%player.hasInventory(%data.getName()))
%player.throw(%data);
}
}The throw is implemented in scripts/server/inventory.cs on the ShapeBase class.
So, you can bind your command to pass your new object into the call to the server command:
moveItem()
{
%testo=ItemData::createItemnorotate("testobject");
%testo.settransform(LocalClientConnection.player.gettransform());
commmandToServer(Throw, %testo);
}Hope that gets it going - you might even overload that to allow you to pass in the desired velocity, but you might check your object's datablock for a throwForce field - if you add that field to any ShapeBase object's datablock it should affect the throw distance.
#4
08/27/2012 (1:18 pm)
hmmm, that's an option I hadn't considered. Thanks Richard, I'll give that a try when I get back to my machine.
Torque Owner Demolishun
DemolishunConsulting Rocks!
%name = "testobject"; %testo = 0; if(isObject(%name)){ %testo = %name.getid(); }else{ %testo = ItemData::createItemnorotate(%name); } ...See if that helps.