Game Development Community

Mounting using mountPoint solved

by Daniel Buckmaster · in Torque Game Engine · 08/21/2007 (3:34 am) · 0 replies

Hopefully...

Well, I've got something that works for me. Basically, I had it working up to the point where the only problem was that the weapon would be mounted twice as far away as its mountPoint node was, as if the vector from the model's origin to its mountPoint was geing doubled. I solved this simply by inserting a hack to halve the vector when it gets assigned, thereby mounting the thing correctly.

So if anyone's interested in this, here's my code. I'll apply it to StaticShape for you. Note that this is not guaranteed to work - I've used this functionality in one of my own classes. However, all of the stuff is pretty much the same, so it should be fine.

staticShape.h changes:

In struct StaticShapeData:
After
F32   energyPerDamagePoint;
Add
MatrixF mountTransform;

After
DECLARE_CONOBJECT(StaticShapeData);
Add
bool preload(bool server, char errorBuffer[256]);

staticShape.cc changes:

In StaticShpeData::StaticShapeData():
After
dynamicTypeField     = 0;
Add
mountTransform.identity();

After the method StaticShapeData::initPersistFields, add the following method:
bool StaticShapData::preload(bool server, char errorBuffer[256])
{
   if(!Parent::preload(server, errorBuffer))
	   return false;

   S32 node = shape->findNode("mountPoint"); //Find mountPoint node
   if (node != -1) {
      MatrixF total(1);
      do {
         MatrixF nmat;
         QuatF q;
         TSTransform::setMatrix(shape->defaultRotations[node].getQuatF(&q),shape->defaultTranslations[node],&nmat);
         total.mul(nmat);
         node = shape->nodes[node].parentIndex;
      }
      while(node != -1);
      total.inverse();
      mountTransform.mul(total);
      //Fix it up
      Point3F position = mountTransform.getPosition();
      position.convolve(Point3F(0.5,0.5,0.5));
      mountTransform.setPosition(position);
   }

   return true;
}

In StaticShape::processTick:
Replace
MatrixF mat;
      mMount.object->getMountTransform(mMount.node,&mat);
      Parent::setTransform(mat);
      Parent::setRenderTransform(mat);
With
MatrixF nodeTransform, transform;
      //Get the transform of the mount node in world space
      mMount.object->getMountTransform(mMount.node,&nodeTransform);
      //Multiply it by the datablock's mount transform
      transform.mul(nodeTransform,mDataBlock->mountTransform);
      //Set object transform
      setTransform(transform);
      setRenderTransform(transform);

In StaticShape::interpolateTick:
Replace
MatrixF mat;
      mMount.object->getRenderMountTransform(mMount.node,&mat);
      Parent::setRenderTransform(mat);
With
MatrixF nodeTransform, transform;
      //Get the transform of the mount node in world space
      mMount.object->getRenderMountTransform(mMount.node,&nodeTransform);
      //Multiply it by the datablock's mount transform
      transform.mul(nodeTransform,mDataBlock->mountTransform);
      //Set object render transform
      setRenderTransform(transform);

I think that should be all. Your shape just needs a mountPoint node.

About the author

Studying mechatronic engineering and computer science at the University of Sydney. Game development is probably my most time-consuming hobby!