Question about Velocity calculation

  • Context: Undergrad 
  • Thread starter Thread starter TheShermanTanker
  • Start date Start date
  • Tags Tags
    Calculation Velocity
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 5K views
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   Reactions: mfb