Page 1 of 1

Projectile launch from gun barrel (PhysX)

Posted: Sat Jul 12, 2008 8:03 pm
by patricklucas
It took me a good few minutes drawing out 3D graphs and vectors on paper to visualize this one... and I hope it works for other setups.

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;
Logic:

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.

Posted: Sun Jul 13, 2008 6:22 pm
by agi_shi
Very useful piece of code, I'm sure some people will find it rather helpful. Now, if only Irrlicht didn't use Euler angles for node rotations, you'd be able to do something like this:

Code: Select all

// if ISceneNode::getRotation() returned a matrix or quaternion
core::vector3df vel = barrelNode->getRotation() * core::vector3df(0, 0, SPEED);