b = L * cos((90-Alpha) *3.14/180); // projection of barrel onto x-z plane Lx = b * cos(Gamma * 3.14/180); // x-component of barrel length Ly = L * cos(Alpha * 3.14/180); // y-component of barrel length Lz = b * sin(Gamma * 3.14/180); // z-component of barrel lengthThis part takes two angles and converts then into a vector.
Gamma appears to be your yaw, Alpha appears to be your pitch.
When Alpha is 0 the cannon is pointing straight up. When Alpha is 90 the cannot is horizontal.
When Gamma is 0 the cannon points in the positive X direction and as it increases it starts to point in the positive Z direction.
I don't know why they use cos(90-Alpha) That is the same as putting sin(Alpha)
cosX = Lx/L; cosY = Ly/L; cosZ = Lz/L;This part calculates unit vector of L. It goes in the same direction as L but is only of length 1. cosX, cosY, cosZ aren't really good variable names where.
xe = L * cos((90-Alpha) *3.14/180) * cos(Gamma * 3.14/180); ze = L * cos((90-Alpha) *3.14/180) * sin(Gamma * 3.14/180);The same as Lx and Lz. This is just a recalculation. This could really just be xe = Lx and ze = Lz.
sx1 = xe; vx1 = Vm * cosX; sy1 = Yb + L * cos(Alpha * 3.14/180); vy1 = Vm * cosY; sz1 = ze; vz1 = Vm * cosZ;Sets s_1 to be the position at the end of the barrel. And sets v_1 to be the velocity moving in the direction of the barrel. Remember cos_ actually represents the unit vector in the direction of the barrel. By multiplying it by Vm you get a vector in the same direction only with the length of Vm.
s.i = ( (m/Cd) * exp(-(Cd * time)/m) * ((-Cw * Vw * cos(GammaW * 3.14/180))/Cd - vx1) - (Cw * Vw * cos(GammaW * 3.14/180) * time) / Cd ) - ( (m/Cd) * ((-Cw * Vw * cos(GammaW * 3.14/180))/Cd - vx1) ) + sx1; s.j = sy1 + ( -(vy1 + (m * g)/Cd) * (m/Cd) * exp(-(Cd*time)/m) - (m * g * time) / Cd ) + ( (m/Cd) * (vy1 + (m * g)/Cd) ); s.k = ( (m/Cd) * exp(-(Cd * time)/m) * ((-Cw * Vw * sin(GammaW * 3.14/180))/Cd - vz1) - (Cw * Vw * sin(GammaW * 3.14/180) * time) / Cd ) - ( (m/Cd) * ((-Cw * Vw * sin(GammaW * 3.14/180))/Cd - vz1) ) + sz1;This calculates the current position of a projection based on the starting position, starting velocity, and time.
Honestly this code is pretty bad. It is hard to read, i recalculates values all over and doesn't have good variable names. If you are just after the velocity then this might be more useful
#define DEG_2_RAD(deg) (deg * 3.14/180) double unitX = sin(DEG_2_RAD(Alpha)) * cos(DEG_2_RAD(Gamma)); double unitY = cos(DEG_2_RAD(Alpha)); double unitZ = sin(DEG_2_RAD(Alpha)) * sin(DEG_2_RAD(Gamma)); double velocityX = StartSpeed * unitX; double velocityY = StartSpeed * unitY; double velocityZ = StartSpeed * unitZ; double startX = BarrelLength * unitX + cannonX; double startY = BarrelLength * unitY + cannonY; double startZ = BarrelLength * unitZ + cannonZ;velocity_ is the start velocity. start_ is starting position of the projectile. I would also recommend either finding or making a vector class. A vector class would let you calculate all three components at once instead of calculating each on in a separate line. So calculating the speed would be Vector3 velocity = StartSpeed * unit; where unit is also of type Vector3.