Projectile launch from gun barrel (PhysX)

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
patricklucas
Posts: 34
Joined: Sun Jul 06, 2008 5:05 am
Location: NC, USA

Projectile launch from gun barrel (PhysX)

Post 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.
agi_shi
Posts: 122
Joined: Mon Feb 26, 2007 12:46 am

Post 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);
Post Reply