### How can I get the velocity of a Projectile object

Scripting questions, discussions, etc

#### How can I get the velocity of a Projectile object

Andrew900460
Posts: 2
Joined: Mon Aug 14, 2017 12:58 am
I know that the Projectile object doesn't appear to have any functions for returning velocity, and the ShapeBase object does have a getVelocity function, but the Projectile object doesn't inherit from that.

#### Re: How can I get the velocity of a Projectile object

Duion
Posts: 915
Joined: Sun Feb 08, 2015 1:51 am

Probably because projectiles usually do not exist for a very long time in the game world. In most cases less than a second.

#### Re: How can I get the velocity of a Projectile object

Andrew900460
Posts: 2
Joined: Mon Aug 14, 2017 12:58 am
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.

#### Re: How can I get the velocity of a Projectile object

Caleb
Posts: 17
Joined: Tue Feb 10, 2015 5:01 pm
A projectiles velocity is set when it is created by setting "initialVelocity." Assuming you're not using ballistic settings then this shouldn't change and you should be able to reference it.

#### Re: How can I get the velocity of a Projectile object

Phantom139
Posts: 5
Joined: Tue Feb 10, 2015 12:23 am

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:

Code: Select all

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:

Code: Select all

%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!

#### Re: How can I get the velocity of a Projectile object

Duion
Posts: 915
Joined: Sun Feb 08, 2015 1:51 am

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.

#### Who is online

Users browsing this forum: No registered users and 1 guest