Game Development Community

CorrectMuzzleVector and Triggers

by Maddermadcat · in Torque Game Engine · 04/04/2008 (3:11 pm) · 4 replies

I'm having some bizarre problems with weapon images. While the player or vehicle holding a weapon is within a trigger, and if that weapon has correctMuzzleVector set to true, something very strange happens -- it's as though the bullets are created within the player, or they start moving into the player.

This stops as soon as the player/vehicle leaves the trigger's volume, or switches to third person. Weapons with correctMuzzleVector set to false don't experience this problem either (though they're unpleasantly imprecise :P).

One of my onFire scripts:

function AirMRImage::onFire(%data, %obj, %slot)
{
   if(%obj.getEnergyLevel() < %data.minEnergy)
      return;

   %obj.setEnergyLevel(%obj.getEnergyLevel() - %data.fireEnergy);
   %data.lightStart = getSimTime();

   if( %obj.isCloaked() )
   {
      if( %obj.getEnergyLevel() > 20 )
      {   
         %obj.setCloaked( false );
         %obj.reCloak = %obj.schedule( 1000, "setCloaked", true ); 
      } 
   }

   %vec = %obj.getMuzzleVector(%slot);
   %x = (getRandom() - 0.5) * 2 * 3.1415926 * %data.projectileSpread;
   %y = (getRandom() - 0.5) * 2 * 3.1415926 * %data.projectileSpread;
   %z = (getRandom() - 0.5) * 2 * 3.1415926 * %data.projectileSpread;
   %mat = MatrixCreateFromEuler(%x @ " " @ %y @ " " @ %z);
   %muzzleVector = MatrixMulVector(%mat, %vec);
   %objectVelocity = %obj.getVelocity();
   %muzzleVelocity = VectorAdd(VectorScale(%muzzleVector, %data.projectile.muzzleVelocity), VectorScale(%objectVelocity, %data.projectile.velInheritFactor));

   %player = %obj.getControllingObject();

   %p = new (%data.projectileType)() {
      dataBlock        = %data.projectile;
      initialVelocity  = %muzzleVelocity;
      initialDirection = %muzzleVector;
      initialPosition  = %obj.getMuzzlePoint(%slot);
      sourceObject     = %player;
      sourceSlot       = %slot;
      origin           = %player;
      client           = %player.client;
   };
   MissionCleanup.add(%p);
   %player.lastProjectile = %p;
   if ( %player.client )
      %player.client.projectile = %p;
}

Nothing I could see there myself that would cause it. Does anyone know what could've caused this, or how to fix it?

#1
04/05/2008 (1:06 am)
The reason switching to third person seems to resolve the issue is because correctMuzzleVector gets bypassed in that view mode.

My guess would be that getCorrectedAim (I think thats what its called) in the C++ code isn't ignoring the triggers object type mask when it tries to adjust the muzzle direction toward the aim location which is causing it to over-correct (since you are inside of the trigger).

The fix would be to make sure that Trigger object types are ignored when it is adjusting the muzzle direction.
#2
04/05/2008 (5:35 pm)
Thanks, Robert, that did it. :]

Dunno' if there'll be any problems with this though. What I did was in shapeImage.cc, in getCorrectedAim. Just before disableCollision();, I added:

static U32 losMask = TerrainObjectType | InteriorObjectType | ShapeBaseObjectType | StaticShapeObjectType | StaticRenderedObjectType;

Then changed the castRay from this:

if (getContainer()->castRay(eyePos, aheadPoint, 0xFFFFFFFF, &rinfo))

To this:

if (getContainer()->castRay(eyePos, aheadPoint, losMask, &rinfo))

Lo and behold, no more problems with correctMuzzleVector. :)
#3
04/06/2008 (11:13 am)
Sounds like a very good idea :)
#4
04/06/2008 (7:21 pm)
Just noticed weapons also get pushed back as though the triggers are solid objects. Going go try fixing that too.