Throwing Torque Items
by Tim Gift · 09/04/2001 (8:43 pm) · 5 comments
In T2, items in an objects inventory didn't actually exists in the world, so throwing items involved creating the item object, assigning it's position to the player's position, and applying an initial velocity. Here's an example of what would be involved...
function ShapeBase::throwItem(%this,%data)
{
// Construct item to throw from the given datablock
// (with a random rotation around the z axis)
%item = new Item() {
dataBlock = %data;
rotation = "0 0 1 " @ (getRandom() * 360);
};
// Make sure the object will be deleted when the
// mission ends.
MissionCleanup.add(%item);
// Call the workhorse throw method...
%this.throwObject(%item);
}
function ShapeBase::throwObject(%this,%obj)
{
// Throw the given object in the direction the shape is
// looking. The force values are hardcoded...
%eye = %this.getEyeVector();
%vec = vectorScale(%eye, 20);
// Add a vertical component to give the item a better arc
%dot = vectorDot("0 0 1",%eye);
if (%dot < 0)
%dot = -%dot;
%vec = vectorAdd(%vec,vectorScale("0 0 8",1 - %dot));
// Add the shape's velocity
%vec = vectorAdd(%vec,%this.getVelocity());
// Set the object's position and initial velocity
%pos = getBoxCenter(%this.getWorldBox());
%obj.setTransform(%pos);
%obj.applyImpulse(%pos,%vec);
// Since the object is thrown from the center of the
// shape, the object needs to avoid colliding with it's
// thrower.
%obj.setCollisionTimeout(%this);
}You could use it as follows by binding the throw ammo server command to a key:datablock Item(Ammo)
{
shapeFile="./ammo.dts";
};
function serverCmdThrowAmmo(%client)
{
%client.player.throwItem(Ammo);
}About the author
Recent Blogs
• Plan for Tim Gift• Plan for Tim Gift
• Eclipse Project for Torque
• Scene Lighting Patch
• Plan for Tim Gift
#2
07/28/2005 (12:51 am)
why are there no comments?? this resource is awesome!
#3
06/12/2006 (5:47 pm)
FYI for those of us using the stock scripted inventory in tge 1.4 this stuff is already optimized in the inventory.cs file in starter.fps for examples
#4
I uploaded a patch to apply on a clean 1.4.2 TGE install on my website here, and I left this resource's code commented out.
Can you give me some hints on what I done wrong? Thanks in advance for anything.
Bye, Berserk.
.
09/11/2007 (5:47 pm)
Hello. I had some troubles using this resource in my project (see here for details).I uploaded a patch to apply on a clean 1.4.2 TGE install on my website here, and I left this resource's code commented out.
Can you give me some hints on what I done wrong? Thanks in advance for anything.
Bye, Berserk.
.
#5
function serverCmdthrow(%client,%data)
{
%client.getControlObject().throw(%data);
}
Once you've added this, save and open your default.bind.cs. Add this line to throw your crossbow when you press ctrl+1.
moveMap.bindCmd(keyboard, "ctrl 1", "commandToServer('throw',\"Crossbow\");", "");
I hope this helps eliminate some confusion. Still a nice resource though!
01/06/2008 (1:48 pm)
Like Tank posted above, this is already taken care of in the later versions of TGE and TGEA starter kits. In order to get them to bind, you need to add this under Inventory Server Commands heading:function serverCmdthrow(%client,%data)
{
%client.getControlObject().throw(%data);
}
Once you've added this, save and open your default.bind.cs. Add this line to throw your crossbow when you press ctrl+1.
moveMap.bindCmd(keyboard, "ctrl 1", "commandToServer('throw',\"Crossbow\");", "");
I hope this helps eliminate some confusion. Still a nice resource though!

Torque 3D Owner Keith Johnston