Game Development Community

Parallel Projectile Question

by p m · in General Discussion · 10/06/2009 (9:05 pm) · 4 replies

Hello GG community

How can I achieve a projectile to fire parallel to the floor at a given speed when in third person view no madder how high or low I look, keeping the accuracy of left and right aiming.

Ive searched for this in the forums and found a few threads
http://www.garagegames.com/community/forums/viewthread/72059
http://www.garagegames.com/community/forums/viewthread/33711
http://www.garagegames.com/community/forums/viewthread/76107
http://www.garagegames.com/community/forums/viewthread/21623
that pertain to this, however even with the knowledge from this I cant figure it out.

I found that initialVelocity has three values the first is the speed, second is the horizontal plane (aiming left to right), the third is the vertical (up and down) so if I was able to somehow keep the third value to 0
then I should be able to achieve my desired effect and the projectile will fire parallel to the floor. Now modifying the first value would change my initial speed of the projectile to the desired amount.


This is what is found in the onfire function:

%muzzleVector = %obj.getMuzzleVector(%slot);

// Get the player's velocity, we'll then add it to that of the projectile
%objectVelocity = %obj.getVelocity();
%muzzleVelocity = VectorAdd(
VectorScale(%muzzleVector, %this.projectile.muzzleVelocity),
VectorScale(%objectVelocity, %this.projectile.velInheritFactor));

// Create the projectile object
%p = new (%this.projectileType)()
{
dataBlock = %this.projectile;
initialVelocity = %muzzleVelocity;
initialPosition = %obj.getMuzzlePoint(%slot);
sourceObject = %obj;
sourceSlot = %slot;
client = %obj.client;
};

I believe this is what I have to modify to achieve the desired effect but im not sure how to modify this exactly, perhaps if I could find the code for the function getMuzzleVector() and other functions I could read over it and further understand how it works however I am not sure where it resides within the numerous lines of code in the engine.

I am new to torque and any help at all is greatly appreciated!
Thanks GG Community.

About the author

Recent Threads


#1
10/06/2009 (11:32 pm)
Quote:I found that initialVelocity has three values the first is the speed, second is the horizontal plane...

Not exactly. initialVelocity is a 3 dimensional vector with an x, y, and z component defining travel along each of the three axes. But you're right about setting the third value to 0. The third value represents travel along the Z axis, which is up/down in Torque. Torque script represents vectors as space-delineated strings "x y z". The function setWord(source_string, word_index, new_value) is the easiest way to alter one of the values. In this case it's the 3rd value (index 2, indexes start at 0) that you want to set to a value of "0". So:

%muzzleVector = %obj.getMuzzleVector(%slot);

// Flatten Z and re-normalize
%muzzleVector = VectorNormalize(setWord(%muzzleVector, 2, "0")); // <--
 
 // Get the player's velocity, we'll then add it to that of the projectile

// Flatten Z of inheritable velocity
 %objectVelocity = setWord(%obj.getVelocity(), 2, "0"); // <--

 %muzzleVelocity = VectorAdd(
 VectorScale(%muzzleVector, %this.projectile.muzzleVelocity),
 VectorScale(%objectVelocity, %this.projectile.velInheritFactor));
#2
10/07/2009 (12:05 am)
Thank you very much Scott, it works perfectly! I am going to try and understand it now, basically we are changeing the third value of the vector however it is index 2 because we start with 0 as the first index, is that correct?

One thing that confuses me though is you said initialVelocity is xyz, however when I tried intialVelocity = "10 90 0"; it shot parallel to the floor and at 90 degrees horizontal and it went very very slow, then when I changed it to intialVelocity = "100 90 0"; it went ten times faster.
Thus the index 0 must be the speed, Velocity has only a speed and two directions no?

Also if I wanted to change the speed I would do something similar except index 0 and change the value?

Again thank you very much Scott really appreciate the help!
#3
10/07/2009 (1:22 am)
Quote:we are changeing the third value of the vector however it is index 2 because we start with 0

That is correct.

Quote:Velocity has only a speed and two directions no?

Two directions? No. Not in a 3D engine. Although, it is possible to represent a vector as a length with two angles defining direction; say azimuth and elevation. There's nothing wrong with that approach although it can't be easily manipulated through matrix multiplication the way an "x y z" vector can. Hence why most computer 3d applications choose the latter approach.

Regardless, the Projectile class in TGE takes the initialVelocity parameter as an "x y z" vector. Of this I am certain. I cannot confirm, although I highly doubt, that anyone has changed that in TGEA/T3D.

Quote:when I changed it to intialVelocity = "100 90 0"; it went ten times faster

It will in fact go ten times faster, along the X axis. Although the total speed is not ten times faster since in both cases the projectile will be traveling at 90 units/second along the y axis. Consider that the actual speed is a combination of the component velocities. If the projectile travels at 100 u/s along x whilst it travels at 90 u/s along y, it is in fact traveling sqrt(x^2 + y^2) or 134.53 units/second. (This derives from the Pythagorean theorem wherein the axes are at right angles and the combined speed is found as the hypotenuse. This can further be extrapolated to 3 dimensions where we find the speed in 3D space as sqrt(x^2 + y^2 + z^2), although in this situation we know z to be 0).

What that all boils down to is that if you want to alter the speed without changing the direction of travel, you must scale all three components equally; Which is what the function VectorScale(xyz_vector, scale_factor) does, as in the line:
VectorScale(%muzzleVector, %this.projectile.muzzleVelocity)
Altering the second parameter (projectile.muzzleVelocity) will affect the speed of the projectile, but not its direction.
#4
10/07/2009 (2:32 pm)
Works perfectly, thank you very much Scott