Game Development Community

Vehicle Damage system Assistance please

by Ronald J Nelson · in Torque Game Engine Advanced · 05/15/2008 (4:22 pm) · 2 replies

I am trying to make a good impact script that applies damage based upon the mass, velocity, and vector of each object. Primarily I am trying to set it up for vehicle to vehicle collisions so as in reality if a vehicle traveling at a higher velocity at the point of impact is traveling faster than the other is in that direction, the vehicle would take less damage than the other.

Here is my script:
function VehicleData::onImpact(%data, %vehicleObject, %collidedObject, %vec, %vecLen, %HitBoxNum)
{
   if (%vehicleObject == %collidedObject) 
      return;

   //Our car calculations
   %objMass = %data.mass;
   %objVelocity = %vehicleObject.getVelocity();
   %objSpeed = vectorLen(%objVelocity);
   %objForce = %objSpeed * %objMass;
   %speedDamageScale = 0.025;
   
   if (%collidedObject.getType() & $TypeMasks::PlayerObjectType)
   {
      %objDamageVar = 0;
      %colDamageVar = vectorLen(%objForce) * %speedDamageScale;
      %objDamageType = 0;
      %colDamageType = 11;
   }
   else if (%collidedObject.getType() & $TypeMasks::InteriorObjectType)
   {
      %objDamageVar = %objForce * %speedDamageScale;
      %colDamageVar = 0;
      %objDamageType = 13;
      %colDamageType = 0;
   }
   else if (%collidedObject.getType() & $TypeMasks::TerrainObjectType)
   {
      %zval = getWord( %vec, 2 );
      if(%zval > 12)
      {
         %objForce = %objSpeed * %objMass;
         %objDamageVar = %objForce * %speedDamageScale;
         %colDamageVar = 0;
         %objDamageType = 12;
         %colDamageType = 0;
      }
      else
      {
         %objForce = %objSpeed * %objMass;
         %objDamageVar = 0;
         %colDamageVar = 0;
         %objDamageType = 0;
         %colDamageType = 0;
      }
   }
   else if (%collidedObject.getType() & $TypeMasks::VehicleObjectType)
   {
      //Collided object calculations
      %colMass = %collidedObject.getDataBlock().mass;
      %colVelocity = %collidedObject.getVelocity();
      %colSpeed = vectorLen(%colVelocity);
      %colForce = %colSpeed * %colMass;
      
      %objSub = (%objMass/(%objMass + %colMass)) * %objVelocity;
      %colSub = (%colMass/(%objMass + %colMass)) * %colVelocity;
      
      %impactVelocity = VectorAdd(%objSub, %colSub);
      
      %impactSpeed = vectorLen(%impactVelocity);
       
      %objImpactForce = (%objSpeed/%impactSpeed) * %objForce;
      %colImpactForce = (%colSpeed/%impactSpeed) * %colForce;

      %colDamageVar = %objImpactForce * %speedDamageScale;
      %objDamageVar = %colImpactForce * %speedDamageScale;
      
      %objDamageType = 15;
      %colDamageType = 15;
   }
   else if (%collidedObject.getType() & $TypeMasks::RigidShapeObjectType)
   {
      //Collided object calculations
      %colVelocity = %collidedObject.getVelocity();
      %colSpeed = vectorLen(%colVelocity);
      %colForce = %colVelocity * %collidedObject.getDataBlock().mass;

      %objToCol = (%objForce + (%objForce - %colForce)) * %speedDamageScale;
      %colToObj = (%colForce + (%colForce - %objForce)) * %speedDamageScale;

      %objDamageVar = %colToObj;
      %colDamageVar = %objToCol;

      %objDamageType = 15;
      %colDamageType = 15;
   }
   else
   {
      %objDamageVar = vectorLen(%objForce) * %speedDamageScale;
      %colDamageVar = 0;
      %objDamageType = 14;
      %colDamageType = 14;
   }


   if(%objDamageVar > 0)
      %vehicleObject.damage(%collidedObject, %pos, %objDamageVar, %objDamageType, %HitBoxNum);
   if(%colDamageVar > 0)
      %collidedObject.damage(%vehicleObject, %pos, %colDamageVar, %colDamageType, %HitBoxNum);

   // associated "crash" sounds
   if(%vecLen > %data.hardImpactSpeed)
      %vehicleObject.playAudio(0, %data.hardImpactSound);
   else if(%vecLen > %data.softImpactSpeed)
      %vehicleObject.playAudio(0, %data.softImpactSound);
}

My problem is that while I can easily come up with equations that are based upon just the two vehicles' velocitys(In terms of Speed) and masses, I am having a great deal of trouble adding in the needed detection and use of direction of travel in respect to each other.

#1
05/16/2008 (11:20 pm)
Can some one help me a little here with how to determine The direction of travel in respect to each other and how to utilize that in an equation? I am far from a physics major.
#2
05/17/2008 (5:39 pm)
OK I have come to realize that the above is just not possible in script without a ton of code changes. You see the velocity I was getting was after the collision had already occured. Since I have added Ross Pawley and team's collision fixes, this meant the applyimpulse had already aoccured and therefore my calculations were extremely flawed.

So what I can do is based upon the differences in mass, the collision velocity already provided, and the percentage that each object is facing the other, provide a pretty good damage effect.

You see based upon this if the percentage of the objects facing each other is 100% then both objects would deliver maximum damage based upon their mass differences and the collision velocity.

My issue is how to determine the percentage of each object facing the other.