Question about Velocity calculation

Click For Summary
The discussion focuses on understanding the velocity calculations in a game's projectile code. The variable var9 represents the magnitude of the velocity vector, calculated by squaring the x, y, and z components, summing them, and taking the square root, effectively giving the length of the projectile's path in three-dimensional space. Var10, on the other hand, calculates the projectile's motion in the XZ plane by omitting the y component, and is used to determine the pitch angle of the projectile. The naming of these variables is criticized for being unclear, as they do not reflect their purpose. Overall, the calculations are essential for accurately determining the projectile's trajectory and rotation.
TheShermanTanker
Messages
13
Reaction score
4
Hello there!

I'm a newcomer and just started doing Physics as there is one part of my current project requires it to do so.

Recently, while reading up a game's source code, I came across something that I couldn't figure out. Below is the portion of the code that handles the velocity calculations when a bullet is fired:
Java:
public void launchProjectile(double motX, double motY, double motZ, float bulletPower, float dispersion) {
        float var9 = MathHelper.sqrt_double(motX * motX + motY * motY + motZ * motZ); //This part is what I cannot figure out
        motX /= (double)var9;
        motY /= (double)var9;
        motZ /= (double)var9;
        motX += this.rand.nextGaussian() * (double)(this.rand.nextBoolean() ? -1 : 1) * 0.007499999832361937D * (double)dispersion;
        motY += this.rand.nextGaussian() * (double)(this.rand.nextBoolean() ? -1 : 1) * 0.007499999832361937D * (double)dispersion;
        motZ += this.rand.nextGaussian() * (double)(this.rand.nextBoolean() ? -1 : 1) * 0.007499999832361937D * (double)dispersion; //Sets bullet dispersion
        motX *= (double)bulletPower;
        motY *= (double)bulletPower;
        motZ *= (double)bulletPower; //Modifies bullet speed
        this.motionX = motX;
        this.motionY = motY;
        this.motionZ = motZ; //Sets the motion of all three axes just to be sure
        float var10 = MathHelper.sqrt_double(motX * motX + motZ * motZ); //What does this part do??
        this.prevRotationYaw = this.rotationYaw = (float)(Math.atan2(motX, dispersion) * 180.0D / Math.PI);
        this.prevRotationPitch = this.rotationPitch = (float)(Math.atan2(motY, (double)var10) * 180.0D / Math.PI); //Some rotation calculations
        this.ticksInGround = 0;
}

I can understand most of it, but this part:
float var9 = MathHelper.sqrt_double(motX * motX + motY * motY + motZ * motZ);
I cannot. What does squaring the velocity on all 3 axes, adding them up and square rooting all of them again yield? What does the variable var9 stand for?
And again this part:
float var10 = MathHelper.sqrt_double(motX * motX + motZ * motZ); //What does this part do??
also confuses me. Its similar but leaves out the velocity on the y-axis. What do var9 and var10 stand for?

I really appreciate any help given. Thanks :D
 
Physics news on Phys.org
TheShermanTanker said:
I can understand most of it, but this part:
float var9 = MathHelper.sqrt_double(motX * motX + motY * motY + motZ * motZ);
I cannot. What does squaring the velocity on all 3 axes, adding them up and square rooting all of them again yield? What does the variable var9 stand for?
The velocity vector has three components: an x component, a y component, and a z component. var9 (a terrible name for a variable) gives the magnitude of the velocity vector.

TheShermanTanker said:
float var10 = MathHelper.sqrt_double(motX * motX + motZ * motZ); //What does this part do??
also confuses me. Its similar but leaves out the velocity on the y-axis. What do var9 and var10 stand for?
I don't have any idea why the y component is omitted. var9 and var10 are both poor choices for variable names as they don't suggest what they will be used for.
 
I don’t know code, but do you think it’s calculating kinetic energy of the bullet (perhaps as a part of assigning hit or damage points)? This would explain why the velocities are squared.
 
TheShermanTanker said:
float var9 = MathHelper.sqrt_double(motX * motX + motY * motY + motZ * motZ)
var9 is the motion (length of the projectile path) in 3-space. It is the length of a diagonal a cube. You have motion in 3 dimensions and var9 is the length of the resultant motion.

TheShermanTanker said:
float var10 = MathHelper.sqrt_double(motX * motX + motZ * motZ); //What does this part do??
Finds the projectile motion in the XZ plane.

var10 is then used two lines further down to find the pitch angle (rotationpitch) of the projectile in the Y direction.

As speculation, X is lateral horizontal axis, Y is vertical axis, Z is axis from origin to target.

Cheers,
Tom
 
  • Like
Likes mfb

Similar threads

  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
1K
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K