Game Development Community

dev|Pro Game Development Curriculum

Fixing item rotation

by Paul Zaczkiewicz · 04/03/2005 (9:43 am) · 10 comments

From item.cc
void Item::setTransform(const MatrixF& mat)
{
   Parent::setTransform(mat);
   setMaskBits(PositionMask | NoWarpMask);
}

.
.
.

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);
      if (stream->writeFlag(gIndex != -1))
         stream->writeInt(gIndex,NetConnection::GhostIdBitSize);
   }
   else
      stream->writeFlag(false);
   if (stream->writeFlag(mask & PositionMask)) {
      stream->writeAffineTransform(mObjToWorld);
      mathWrite(*stream, mObjScale);

      //Point3F pos;
      //mObjToWorld.getColumn(3,&pos);
      //mathWrite(*stream, pos);
      if (!stream->writeFlag(mAtRest)) {
         mathWrite(*stream, mVelocity);
      }
      stream->writeFlag(!(mask & NoWarpMask));
	}
   return retMask;
}

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);
      setCollisionTimeout(static_cast<ShapeBase*>(connection->resolveGhost(gIndex)));
   }
   if (stream->readFlag()) {
		// From StaticShape
      MatrixF mat;
      stream->readAffineTransform(&mat);

      VectorF scale;
      mathRead(*stream, &scale);
      setScale(scale);

		// Origional Code
      Point3F pos;
      mat.getColumn(3,&pos);
      F32 speed = mVelocity.len();
      if ((mAtRest = stream->readFlag()) == true)
         mVelocity.set(0,0,0);
      else
         mathRead(*stream, &mVelocity);

      if (stream->readFlag() && isProperlyAdded()) {
         // Determin number of ticks to warp based on the average
         // of the client and server velocities.
         delta.warpOffset = pos - delta.pos;
         F32 as = (speed + mVelocity.len()) * 0.5 * TickSec;
         F32 dt = (as > 0.00001f) ? delta.warpOffset.len() / as: sMaxWarpTicks;
         delta.warpTicks = (S32)((dt > sMinWarpTicks)? getMax(mFloor(dt + 0.5), 1.0f): 0.0f);

         if (delta.warpTicks) {
            // Setup the warp to start on the next tick, only the
            // object's position is warped.
            if (delta.warpTicks > sMaxWarpTicks)
               delta.warpTicks = sMaxWarpTicks;
            delta.warpOffset /= delta.warpTicks;
         }
         else {
            // Going to skip the warp, server and client are real close.
            // Adjust the frame interpolation to move smoothly to the
            // new position within the current tick.
            Point3F cp = delta.pos + delta.posVec * delta.dt;
            VectorF vec = delta.pos - cp;
            F32 vl = vec.len();
            if (vl) {
               F32 s = delta.posVec.len() / vl;
               delta.posVec = (cp - pos) * s;
            }
            delta.pos = pos;
            mat.setColumn(3,pos);
         }
      }
      else {
         // Set the item to the server position
         delta.warpTicks = 0;
         delta.posVec.set(0,0,0);
         delta.pos = pos;
         delta.dt = 0;
         mat.setColumn(3,pos);
      }
      Parent::setTransform(mat);
      Parent::setRenderTransform(mat);
	}
}

About the author

Recent Blogs


#1
04/04/2005 (10:30 am)
Great resource.
#2
04/05/2005 (7:39 am)
Anyone sucessfully got this implemented? Do you overwrite the old subs with these?
#3
04/05/2005 (11:18 am)
There seems to be something not quite right with the item code.

With these changes, item throwing no longer works (the thrown item doesnt leave the throwing object) :(
#4
04/05/2005 (1:47 pm)
@Jerry Shaw : Thanks

@FruitBatInShades : When writing this, I noticed that Item::setTransform, Item::packUpdate and Item::unpackUpdate all contained portions of code that limited rotation to the z-axis. I deleted those portions and spliced in code from the StaticShape equivalents.

@David Barr : I had mostly made this code for my own use, and hadn't tested for all cases of item use (eg, static==false, or throw()). I had this posted in a forum, hoping that other people would test my code on their games, but got no takers. Feel free to offer revisions, and I'll submit the changes after review.
#5
04/06/2005 (9:57 am)
i like this resource but one thing - cant you just change the rotation setting in the ingame mission inspector and then pick it up. once it respawns it will be standing still.

good resource BTW :)
#6
04/06/2005 (11:11 am)
This has nothing to do with items continuously rotating around the z axis. This has to do with getting static items to rotate about any other than 'z'. Go to the mission inspector, and try to rotate an object of the Item class along some axis other than the 'z' axis. Without modfications, this is impossible. to get an item to be still, all you have to do is set static=true when creating the item (eg, in the mission file).
#7
04/06/2005 (11:32 am)
oh sorry, i still have much to learn.
#8
05/22/2005 (5:06 pm)
Has anyone gotten a fix for this with regards to throwing items yet? All the items I throw end up under the world instantly.
#9
08/15/2005 (12:05 am)
I think, there is a more easy way to allow an item to rotate around x, y, z

just change the mRotate = false by mRotate = true in item.cc

Item::Item()
{
mRotate = true;
}
#10
09/20/2005 (4:52 pm)
@stephane savioz

items are forced to only rotate along the Z axis as defined in item.cc, the point of this resource is to ALLOW items to rotate along any axis.