Question about Velocity calculation

In summary, the code handles velocity calculations for a bullet fired in a game. It includes calculations for bullet dispersion and modifying bullet speed. The variables var9 and var10 represent the magnitude of the velocity vector and the projectile motion in the XZ plane, respectively. The code also calculates the pitch angle of the projectile. The purpose of these calculations may be related to determining hit or damage points for the bullet.
  • #1
TheShermanTanker
13
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
  • #2
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.
 
  • #3
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.
 
  • #4
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

Related to Question about Velocity calculation

1. What is velocity and how is it different from speed?

Velocity is a vector quantity that describes the rate of change of an object's position in a specific direction. It includes both the magnitude and direction of an object's motion. In contrast, speed is a scalar quantity that only describes how fast an object is moving, without taking into account the direction.

2. How is velocity calculated?

Velocity is calculated by dividing the change in an object's position by the change in time. The formula for average velocity is v = (change in position)/(change in time), or v = (final position - initial position)/(final time - initial time).

3. What are the units of velocity?

The units of velocity are typically meters per second (m/s) in the SI system. Other common units include kilometers per hour (km/h) and miles per hour (mph).

4. Can velocity be negative?

Yes, velocity can be negative. If an object is moving in the opposite direction of the positive direction, it has a negative velocity. For example, if a car is traveling east and then turns around and starts moving west, its velocity is negative.

5. How does velocity affect an object's motion?

Velocity affects an object's motion by determining its speed and direction. A change in velocity (acceleration) will cause a change in an object's speed or direction of motion. Objects with a constant velocity will continue moving at the same speed and in the same direction until acted upon by an external force.

Similar threads

  • General Math
Replies
11
Views
1K
Replies
4
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
11
Views
774
Replies
4
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Replies
3
Views
2K
Replies
17
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Back
Top