Game Development Community

Firing Vector Question

by James Laker (BurNinG) · in Torque Game Engine · 03/24/2008 (1:37 pm) · 6 replies

Hey guys,

I'm trying to get my drones to always hit it's target. Is there an easy way I can make the projectile just fire in the direction of the locked target, or does it always fire in the direction of the weapons muzzlevector?

I've tried changing the onFire function to this, but it still only fires in the mounted weapons direction:

function DroneLaserImage::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);
	
	// Returns axis angle attitude specification
   %shipAxisAngle = getWords(%obj.getTransform(),3,6); 
   
   //Determine how far should the picking ray extend into the world?
   %selectRange = %this.maxRange;

   %targetPosition = %obj.client.LockedId.getPosition();
   
	// Create the projectile object
   if (!isObject(%obj.laser[%slot])) 
   {
      // Create the projectile object
      %obj.laser[%slot] = new (%this.projectileType)() {
         dataBlock        = %projectile;
         //initialVelocity  = %muzzleVelocity;
         initialVelocity  = "0 0 0";
         initialPosition  = %targetPosition;
         initialRotation  = %shipAxisAngle;
         sourceObject     = %obj;
         sourceSlot       = %slot;
         range            = %this.maxRange;
         client           = %obj.client;
      };
	   MissionCleanup.add(%obj.laser[%slot]);
   }

   //Now that we have one for sure
   //Switch it on
   %obj.laser[%slot].setFiringState(true);
      
   return %obj.laser[%slot];
}

Can you spot anything that stands out?

#1
03/25/2008 (4:09 pm)
I think you might have to look at the fov in projectile.cpp. I think thats where the seeking/ guided projectile locks on to target. Eliminate the fov or make it 360 degrees.
#2
03/26/2008 (12:29 am)
My targetting system works fine, I'm using it for pretty much everything in Solar Battles, so my problem is not with targetting.

I dont want my projectiles to follow a target, I need my target to fire at the target where ever it is... behind, above, etc. So I need my projectile to leave the muzzlepoint in the direction of the target. As you might have noticed in the code I posted, I'm using a laser weapon.

So in short... I target a ship... and when I'm in range and fire it wont miss.

I just need the calculate the vectors correctly, but I dont know how.
#3
03/26/2008 (1:00 am)
%subVec = VectorSub ( %enemyVec , %startFirePosVec ) ;
%norm    = VectorNormalize ( %subVec ) ;
%vel  = VectorScale ( %norm , %laserSpeed ) ;
%laser.setVelocity( %vel  ) ;

Aun.

*edit : typo
#4
03/26/2008 (1:43 am)
Thanx Aun... But I dont see how that fits in with the projectile. And also the maxRange isn't used in your code. Can you please clarify.
#5
03/26/2008 (2:29 am)
I refer the %vel variable as the result you needed which is the total velocity of the laser you wanted to fire. For the whole code to use with the projectile might be something like this ...

%objectVelocity = %obj.getVelocity();
%subVec = VectorSub ( %enemyVec , %obj.getMuzzlePoint(%slot) ) ;
%norm = VectorNormalize ( %subVec ) ;

%vel  = VectorScale ( %norm , %projectile.muzzleVelocity ) ;

// AUN : maybe we could just ignore the object's velocity so the laser could fire more correctly
// %muzzleVelocity = VectorAdd ( VectorScale(%objectVelocity, %projectile.velInheritFactor) , %vel  ) ; 

%muzzleVelocity = %vel  ;

// AUN : I haven't heard of a variable name max lenght before but if you need the 
//          max range maybe we could do something like this

// %maxRange = VectorLen ( %subVec ) + 50 ; 

   %p = new (%this.projectileType)() {
      dataBlock        = %projectile;
      initialVelocity  = %muzzleVelocity;
      initialPosition  = %obj.getMuzzlePoint(%slot);
      sourceObject     = %obj;
      sourceSlot       = %slot;
      client           = %obj.client;
   };

I haven't tested the code but it should fire a bullet from %obj.getMuzzlePoint(%slot) to --> %enemyVec .

Aun.
#6
03/26/2008 (2:32 am)
Great... i'll give it a try when I get home tonight.

The Maxrange is specific to the Laser resource.