Game Development Community

Weapon accuracy

by Duncan Perham · in Torque Game Engine · 11/15/2005 (12:55 pm) · 3 replies

Can someone tell me the easiest way to modify weapon accuracy, at the moment, its 100% hit rate from the bots, and pretty much the same from the client. I have looked all over the weapons, nearest i can see is..

//on a weapon ie crossbow
datablock ProjectileData(M16Projectile)
{
projectileShapeName = "~/data/shapes/soldier/tracer.dts";
directDamage = 10;
explosion = M16Explosion;
waterExplosion = M16WaterExplosion;

particleWaterEmitter= M16AmmoBubbleEmitter;

splash = M16Splash;

muzzleVelocity = 1000;
velInheritFactor = .3;//<<<<<<<<<<<<<<<<<<
....

can i then modify this function to randomly add to the target so that there is a chance of missing


function M16Image::onFire(%this, %obj, %slot)
{
%projectile = %this.projectile;

// Decrement inventory ammo. The image's ammo state is update
// automatically by the ammo inventory hooks.
%obj.decInventory(%this.ammo,1);

// Determin initial projectile velocity based on the
// gun's muzzle point and the object's current velocity
%muzzleVector = %obj.getMuzzleVector(%slot);
%objectVelocity = %obj.getVelocity();
%muzzleVelocity = VectorAdd(
VectorScale(%muzzleVector, %projectile.muzzleVelocity),
VectorScale(%objectVelocity, %projectile.velInheritFactor));


// Create the projectile object
%p = new (%this.projectileType)() {
dataBlock = %projectile;
initialVelocity = %muzzleVelocity;
initialPosition = %obj.getMuzzlePoint(%slot);
sourceObject = %obj;
sourceSlot = %slot;
client = %obj.client;
};
echo("m16 fired");
%p.Dump();
MissionCleanup.add(%p);
return %p;
}

#1
11/15/2005 (1:20 pm)
No, velocity inherit factor is where the projectile inherits the velocity of the object it was fired from.
I think this is what u need.
#2
11/15/2005 (1:26 pm)
What you need is a random spread thingy methinks. By default they fire perfectly straight (or as ballistics). The velInheritFactor is exactly what it says, the inheritance of the players velocity.

Now for the code ;)

Add to your weapon datablock this code:
projectileSpread = true; // True/False
   numProjectiles   = 1;    // If your weapon fires more than one (think shotgun) increace this....
   totalSpread      = 500;   // Spread...

and now add this to wherever on the server, I don't know your file structure so I can't help with that ;)
function createProjectile(%this, %obj, %slot)
{
   %projectile = %this.projectile;
   %position   = %obj.getMuzzlePoint(%slot);
   
   %muzzleVector = %obj.getMuzzleVector(%slot);
   %objectVelocity = %obj.getVelocity();
   %muzzleVelocity = VectorAdd(
      VectorScale(%muzzleVector, %projectile.muzzleVelocity),
      VectorScale(%objectVelocity, %projectile.velInheritFactor));

   
   if(!%obj.aiPlayer)
     %clientID = %obj.client;
   else
     %clientID = %obj;

   if(%projectile.projectileSpread == 1)
   {
      %spread     = %projectile.numProjectiles / %projectile.totalSpread;
  	  %x = (getRandom() - 0.5) * 2 * 3.1415926 * %spread;
      %y = (getRandom() - 0.5) * 2 * 3.1415926 * %spread;
      %z = (getRandom() - 0.5) * 2 * 3.1415926 * %spread;
      %mat = MatrixCreateFromEuler(%x @ " " @ %y @ " " @ %z);
      %muzzleVelocity = MatrixMulVector(%mat, %muzzleVelocity); // Alter our projectile vector with our spread matrix
   }

   %p = new (%this.projectileType)() {
      dataBlock        = %projectile;
      initialVelocity  = %muzzleVelocity;
      initialPosition  = %position;
      sourceObject     = %obj;
      sourceSlot       = %slot;
      client           = %clientID;
   };
   MissionCleanup.add(%p);
   return %p;
}

and finally alter your weapon::onFire to look like this:
function M16Image::onFire(%this, %obj, %slot)
{
   %obj.decInventory(%this.ammo,1);

   %p = createProjectile(%this, %obj, %slot);
   
   echo("m16 fired");
   %p.Dump();
   return %p;
}

I haven't tested the code so BACK IT UP first ;)
good luck

EDIT: Ah poo, Westy beat me to it. Mine is an edited version of that resource, you can see the differences in code for yourself if you feel like it :) You can choose which you like better ;)

[Ishbuu]
#3
11/15/2005 (1:55 pm)
Yep, thats exactly what i need, thanks very much.