It uses the rotation vector of a gun barrel to give a launched projectile a proper linear velocity on creation.
Code: Select all
float rad = 3.141593/180; // degrees->radians conversion
core::vector3df velvec = barrelNode->getRotation();
float xadjust = sin(rad*velvec.X);
float xrot = rad*velvec.X;
float yrot = rad*velvec.Y;
NxVec3 vel(xadjust*sin(yrot),cos(xrot),xadjust*cos(yrot));
vel.normalize();
vel *= 1600.0f;The Y-axis rotation is what defines the projectile's linear velocity on the XZ-plane, and drawing a picture yields sin(yrot) for X and cos(yrot) for Z.
However, you'll notice that as the gun rotates about the X-axis, the amount of the XZ-plane velocity changes. Thus, xadjust is the multiplier for that. At 0 X rotation, this value is 0, as the gun is pointing straight up. At 90, it is 1 because there the gun is lying flat, thus there is only XZ velocity.
The Y velocity depends only on the X-axis rotation - graphing results in cos(xrot) for Y.
*Note I used floats here... there's probably a better type, but I stuck with floats in testing.