Game Development Community

Throwing Objects!

by Todd Johnson · in Torque Game Engine · 11/26/2004 (7:27 pm) · 11 replies

Ok this is driving me up the wall. I could throw things just fine before now I can't do it without crashing! Here is my throwing code...


Client side...
Config.cs
moveMap.bindCmd(keyboard, "q", "commandToServer(\'throw\',0);", "");

Server side...
Item.cs
function ItemData::onThrow(%this,%user,%amount)
{
   %obj = new Item() {
      datablock = %this;
      rotation = "0 0 1 " @ (getRandom() * 360);
      count = %amount;
   };
   MissionGroup.add(%obj);
   %obj.schedulePop();
   return %obj;
}

ItemManager.cs
function ShapeBaseData::onThrow(%this,%user,%amount) { return 0; }

function serverCmdThrow(%client,%data)
{
   %client.getControlObject().throw(%data);
}

// Player's throw function
function ShapeBase::throw(%this,%slot)
{
   //itemName being the object I want to create
   %inv = %this.InventoryManager;
   %db = %inv.Slot[%Slot].itemName;   

   if ( %db $= "")
      return 0; 

   %obj = %db.onThrow(%this, 1);    

   if (%obj) 
   {
      %this.throwObject(%obj);
      return true;
   }

   return false; 
}

function ShapeBase::throwObject(%this,%obj)
{
   %throwForce = %this.throwForce;
   if (!%throwForce)
      %throwForce = 20;

   %eye = %this.getEyeVector();
   %vec = vectorScale(%eye, %throwForce);

   %verticalForce = %throwForce / 2;
   %dot = vectorDot("0 0 1",%eye);
   if (%dot < 0)
      %dot = -%dot;
   %vec = vectorAdd(%vec,vectorScale("0 0 " @ %verticalForce,1 - %dot));

   %vec = vectorAdd(%vec,%this.getVelocity());

   %pos = getBoxCenter(%this.getWorldBox());
   %obj.setTransform(%pos);
   %obj.applyImpulse(%pos,%vec);

   %obj.setCollisionTimeout(%this);
}

The code is almost identicle to the head. I don't see much difference. I keep receiving a "Error, NULL ghost encountered" crash. Please help if you can!

#1
11/26/2004 (7:28 pm)
Also, the object gets created just fine. The problem happens in the actual throwObject method.
#2
11/26/2004 (7:32 pm)
Already been covered http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=1417
#3
11/26/2004 (7:34 pm)
Actually, the issue is with "%obj.setCollisionTimeout(%this);"

www.garagegames.com/mg/forums/result.thread.php?qt=22253
#4
11/27/2004 (5:42 am)
Yea Robert, I've checked that thread out. Drew had the answer! Thanks guys!!
#5
12/24/2004 (6:37 pm)
Would the above code work if I wanted the thrown obect to act as a projectile but persist in the world after hitting an object which then could be picked up again? for example if I were to create a baseball game I need to have the player be able to pickup a ball anfd throw it and have another player either catch it or it hit the ground and roll. Any help with this would be greatly appreciated.
#6
12/25/2004 (5:19 am)
Well you'll want to do the code change in drew's post to do any throwing. For your situation, it sounds like you need not worry about projectiles.
#7
01/02/2005 (9:55 am)
How do I go about handling collisions with thrown objects with the above code change?
#8
01/02/2005 (11:39 am)
SetCollisionTimeout disables collisions between one item and one particular object. In the baseball example, you would call set collision timeout so that the pitcher doesn't collide with a ball that he just threw. That call would prevent the pitcher from colliding with the ball for a short time, but it shouldn't have any effect on the collisions between the ball and the batter or the ball and the catcher's mitt, etc.
#9
01/25/2005 (1:30 pm)
Folks,

I don't know if this thread is still being watched, but just in case, I found a bug in 1.3's setCollisionTimeOut() code while working on EGTGE Volume 1. I'm not sure if this has been fixed yet, but here is the gist of it:

... small bug (exists in the 1.3 SDK as released in auto-installer) that has been around for a bit (since the conference). In item.cc, the pack size for the mCollisionObject ghost ID doesn't match the unpack, which causes over-read in the unpack.

The side-effect is that when you do an Item::setCollisionTimeout() call, the object stops rendering. But this may have other side-effects too


It's a one line fix, I've highlighted key words so you can find it and the lines:


item.cc
U32 Item::packUpdate(NetConnection *connection, U32 mask, BitStream *stream)
{
   U32 retMask = Parent::packUpdate(connection,mask,stream);

   if (stream->writeFlag(mask & InitialUpdateMask)) {
      stream->writeFlag(mRotate);
      stream->writeFlag(mStatic);
      stream->writeFlag(mCollideable);
      if (stream->writeFlag(getScale() != Point3F(1, 1, 1)))
         mathWrite(*stream, getScale());
   }
   if (mask & ThrowSrcMask && mCollisionObject) { 
      S32 gIndex = connection->getGhostIndex(mCollisionObject);
	  sDebug = gIndex;
	  Con::warnf("gIndex == %d", gIndex);
      if (stream->writeFlag(gIndex != -1)) {
         stream->writeInt(gIndex,NetConnection::GhostIdBitSize);
	  }
   }

void Item::unpackUpdate(NetConnection *connection, BitStream *stream)
{
   Parent::unpackUpdate(connection,stream);
   if (stream->readFlag()) {
      mRotate = stream->readFlag();
      mStatic = stream->readFlag();
      mCollideable = stream->readFlag();
      if (stream->readFlag())
         mathRead(*stream, &mObjScale);
      else
         mObjScale.set(1, 1, 1);
   }
   if (stream->readFlag()) {

      S32 gIndex = stream->readInt(10);

// Should be => S32 gIndex = stream->readInt(NetConnection::GhostIdBitSize);

[HOW]EdM|EGTGE
#10
01/25/2005 (3:50 pm)
I read all threads :)
Great Edward !
#11
01/26/2005 (2:48 am)
I can't get throw working at all. I keep getting 'serverCmdThrow: unknown command' any ideas, i'm using 1.3. I'm trying to set up grenades and have a grenade.dts but can't get much further at the mo.

ps. I'm a complete newb

EDIT: never mind, solved it :oD