Question about Velocity calculation

  • Context: Undergrad 
  • Thread starter Thread starter TheShermanTanker
  • Start date Start date
  • Tags Tags
    Calculation Velocity
Click For Summary

Discussion Overview

The discussion revolves around understanding the velocity calculations in a game's source code, specifically focusing on the mathematical operations involved in determining the magnitude of a velocity vector and its implications for projectile motion. Participants explore the code's handling of three-dimensional motion and its potential applications in game mechanics.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants explain that the calculation of var9 represents the magnitude of the velocity vector, derived from the three components of motion in x, y, and z directions.
  • Others note that var10 calculates the motion in the XZ plane, omitting the y-component, which raises questions about its purpose in the context of the code.
  • One participant speculates that the squaring of velocities might relate to calculating kinetic energy, suggesting a connection to game mechanics like damage points.
  • Another participant describes var9 as the length of the projectile path in three-dimensional space, likening it to the diagonal of a cube.
  • There is a critique regarding the variable names var9 and var10, with some participants expressing that they do not adequately convey their meanings.

Areas of Agreement / Disagreement

Participants generally agree on the interpretation of var9 as the magnitude of the velocity vector, but there is uncertainty regarding the omission of the y-component in var10 and its implications. The discussion remains unresolved regarding the exact purpose of these calculations in the context of the game's mechanics.

Contextual Notes

There are limitations in the discussion regarding the assumptions made about the relationship between the calculations and game mechanics, as well as the lack of clarity on the implications of the omitted y-component in var10.

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

Similar threads

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