Jump to content

How can I get the velocity of a Projectile object


Recommended Posts

well my reason for needing this, is because i have a projectile that being fired from another moving projectile (don't ask me this is someone elses code I'm modifying), and I want the second projectile to inherit the velocity of the object it's being fired from. I know there is a variable in the ProjectileData class but it doesn't seem to have any effect on it.

Link to comment
Share on other sites

As others have stated, projectile instances do not retain torquescript exposed variables for velocity as they typically exist for a short time period (<1000ms). A solution that you're looking for will likely use one of two scripting methods

1. Use the projectile's initial velocity, or the velocity when it leaves the weapon. This is obtained by calling %proj.initialVelocity on the projectile object (%proj) and returns a vector instance containing the velocity.

2. Calculate the velocity manually, because as you have stated, you know exactly how long it takes until the second projectile must fire. Therefore, you attach a bit to the weapon's on fire code:


function myWeapon::onFire(%this, %obj, %slot) {
  //Spawn the projectile (Save it as %p)
  %p.pos1 = %obj.getPosition(); //You may choose to be more accurate here by using the muzzle position, circa %obj.getMuzzlePosition(%slot); Up to you though


Then, when the projectile instance needs to spawn the new projectile instance, you obtain the current position of the projectile and perform a simple vector subtraction:


%pos2 = %proj.getPosition();
%diff = vectorSub(%pos2, %proj.pos1);
%pLife = %proj.getDatablock().lifetime;
%newVelocity = vectorScale(%diff, (%pLife / 1000));


This can then be normalized or scaled depending on your specific application. An obvious third solution would be to expose the variable on the torquescript by a simple C++ modification yourself, but that would just involve a little more work. Either way, either will work for you.

Good luck!

Link to comment
Share on other sites

I would probably just delete the projectile and create two new ones. Have the projectile "explode" when it should fire its second projectile and in the explosion create two new projectiles, one with the same velocity and vector as the first one.

Or you can just use the initial velocity since it will stay the same and use some other method.

The problems only really arise if you use ballistic projectiles, since ballistic projectiles change direction and speed in their flight.

But in case you want a slow ballistic projectile, I would probably use a whole other class for that, for example an item or player or vehicle, they can also fly and often have more callbacks.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...