Game Development Community

Velocity of projectile on collision or impact

by gamer · in Torque Game Engine · 07/26/2006 (3:47 pm) · 8 replies

Is there a way to get the velocity of a projectile on collision with a player?
I looked into
Player::updatePos() where it calls onImpact(), however it seems that only player's impact velocity is given, not the projectile's impact velocity.
//taken from Player::updatePos which is too long to post.
Con::printf("  mVelocity = %g  %g %g, minImpact = %g, this one = %g, normal = %g %g %g", mVelocity[0],mVelocity[1],mVelocity[2], mDataBlock->minImpactSpeed, bd, collision->normal);
         if (bd > mDataBlock->minImpactSpeed && !mMountPending) {
            if (!isGhost())
               onImpact(collision->object, collision->normal*bd);

the mVelocity printed is rather small, it never goes above minImpactSpeed, so onImpact never gets called. but I am firing a very high speed bullet onto the player.
1. is there a way to get the bullet's impact velocity from the Player::updatePos()?
2. if not, should I be looking into if weapon or projecile related class has an onImpact method? can someone point me to the right file?
thanks

#1
07/26/2006 (4:24 pm)
Are you calling getVelocity() on the projectile? This should return the projetile's current velocity vector.
#2
07/26/2006 (5:34 pm)
Thanks! there's a getVelocity in item class. but this getVelocity function is not made into a console method. is there a way to call getVelocity from the console?
#3
07/26/2006 (6:15 pm)
I tried calling getVelocity() on the
projectile in onCollission() in the following code, but it always returns 0 0 0.
any idea what could be wrong?
void Projectile::onCollision(const Point3F& hitPosition, const Point3F& hitNormal, SceneObject* hitObject)
{
   // No client specific code should be placed or branched from this function
   if(isClientObject())
      return;

   if (hitObject != NULL && isServerObject())
   {
      char *posArg = Con::getArgBuffer(64);
      char *normalArg = Con::getArgBuffer(64);

      dSprintf(posArg, 64, "%g %g %g", hitPosition.x, hitPosition.y, hitPosition.z);
      dSprintf(normalArg, 64, "%g %g %g", hitNormal.x, hitNormal.y, hitNormal.z);
	  Con::printf("projectile:onCollission velocity is %g %g %g\n",getVelocity().x,getVelocity().y,getVelocity().z); // prints 0 0 0

	

      Con::executef(mDataBlock, 6, "onCollision",
         scriptThis(),
         Con::getIntArg(hitObject->getId()),
         Con::getFloatArg(mFadeValue),
         posArg,
         normalArg);
   }
}
#4
07/26/2006 (6:17 pm)
Does the velocity vary from launch significantly? If it does not then store that in the projectile on launch and reference on impact.
#5
07/26/2006 (6:21 pm)
Yes the difference between initial and final velocity will be important in my case.
#6
07/26/2006 (7:38 pm)
There is a Point3F in projectile called mCurrVelocity have you tried that?
#7
07/26/2006 (7:44 pm)
Yeah getVelocity returns mCurrVelocity. The problem I discovered here is
projectile.processTick() reset the mCurrVelocity before calling onColission, so I moved that line after onColission. and then I add an extra parameter to onCollission to pass the velocity. I am not sure if it will have other side effects but I can't find an easier way.
Also a second issue is that projectile.getVelocity is a private method. Is there a reason why this is not public? I have trouble making it a console method.
#8
07/26/2006 (8:46 pm)
I see no reason why changing it to public would hurt anything though I might be wrong.