Game Development Community

"Tumbling" Projectile (e.g., grenades)

by DavidRM · in Torque Game Engine · 09/19/2002 (11:14 am) · 12 replies

Last night I implemented a simple mechanism for throwing a grenade. However, I can't seem to get the grenade to tumble in flight. It arcs beautifully, maintaining the same orientation until it hits something.

Here is the scripting code I use to create and "throw" the grenade:

%spread = 0.05;

   %throwForce = %owner.throwForce;
   if (!%throwForce)
      %throwForce = 7.5;

   // Get the throw vector. This is the dead straight aiming point of the gun
   %eye = %owner.getEyeVector();
   %vector = vectorScale(%eye, %throwForce);

  // Add a vertical component to give the item a better arc
   %dot = vectorDot("0 0 1",%eye);
   if (%dot < 0)
      %dot = -%dot;
   %vector = vectorAdd(%vector,vectorScale("0 0 8",1 - %dot));

   // Get our players velocity. We must ensure that the players velocity is added
   // onto the projectile
   %vector = vectorAdd(%vector,%owner.getVelocity());

   // Determine our random x, y and z points in our spread circle and create
   // a spread matrix.
   %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);

   // Alter our projectile vector with our spread matrix
   %vector = MatrixMulVector(%mat, %vector);

   // Create the projectile object
   %grenade = new Projectile() {
      dataBlock        = GrenadeProjectile;
      rotation =  "2 2 " @ (getRandom() * 360);
      initialVelocity  = %vector;
      initialPosition  = getBoxCenter(%owner.getWorldBox());
      sourceObject     = %owner;
      sourceSlot       = $WeaponSlot;
      client           = %owner.client;
   };

   MissionCleanup.add(%grenade);

I thought that the "rotation" would do the trick, but it doesn't seem to do anything at all.

Any pointers?

Thanks.

-David
Samu Games

#1
09/19/2002 (11:29 am)
Yeah hopefully somebody can code some form of much-needed physics and submit it for an update to TGE source.
#2
09/19/2002 (11:45 am)
What problem are you having? That it explodes too quickly? Or doesn't bounce in any direction but forward?
#3
09/19/2002 (12:11 pm)
My problem is that the grenade doesn't tumble during flight.

It maintains a smooth arc, always in the same position/orientation. I want it to rotate, tumble, or whatever you want to call it during flight.

Thanks.

-David
#4
09/19/2002 (12:25 pm)
ahhh... like a badly thrown football?

You may have to animated the shape in 3dsmax/milkshape/Lightwave to get it to work. If you have T2, you might try the mortar shape, as I think it tumbles.

I don't know if there is a setting for it.

Eric
#5
09/19/2002 (12:58 pm)
You should be able to rotate it via the rotation matrix. :-)
#6
09/19/2002 (1:04 pm)
Yeah, just calculate a rotation delta and apply that over time to rotate.

Of course you then need to change this when it hits the ground (applies static friction to alter the rotation).

Take a look at how the static objects are rotated in the map, perhaps thats a good example? (you know, when you place ammo boxes in the map view and they rotate).

Phil.
#7
09/25/2002 (2:21 pm)
"apply that over time"?

So I can't just set a property that says "spin like this" and let it go? I have to do some kind of processing while it travels?

Bummer.

Thanks,

-David
#8
09/25/2002 (6:48 pm)
Easiest thing would probably animate the rotation and trigger it when it is thrown. It would be less work, and maybe less expensive than calculating it on the fly.
#9
09/25/2002 (7:14 pm)
But wouldn't an animated object reduce the realism of the physics involved in moving this object?
#10
09/25/2002 (8:01 pm)
Well your problem is that you didn't define the range of random function. Syntax: getRandom() -or- getRandom(,). If there are no parameters provided I believe getRandom returns zero or an error generates in the console about not enough parameters provided. :)

--------------------------------------------
My theory was wrong as usual...
It does return a value. :(
ohh well, I tried...
#11
09/25/2002 (10:14 pm)
I seriously doubt that the "realism" of a grenade tumbling thru the air will be noticed by many people in the middle of the action in MOST action games.

Collision with the world and other such physics might need to be simulated. But the actual "tumbling" could still be a pre-defined motion.

End the end it would depend on what the requirements of the game were.
#12
09/26/2002 (7:38 am)
I'll have to check out getRandom(). I had just copied the code using getRandom(), and not verified that it was actually *working*... Silly me. =)

Thanks, all.

-David