Since a bunch of people have posted one way or another asking for this, i am going to write a short tutorial on how to set up a camera with a weapon in front of it. The code is quite simple actually :
irr::scene::IAnimatedMesh* weaponMesh;
irr::scene::IAnimatedMeshSceneNode* weaponNode;
weaponMesh = mSceneMgr->getMesh("shotgun.md2"); //Your Mesh is loaded here
weaponNode = mSceneMgr->addAnimatedMeshSceneNode( weaponMesh, camera, -1); //optionaly here, you could set the initial pos, rot, and scale in that order
This code basically creates a animated mesh and coresponding node. The mesh is loaded in the third line. The fourth line creates the node, and sets its parent to the camera. Thus, any movement occuring to the camera influences the weapon. Hope that helps everybody.
Yours Truly,
The Robomaniac
Project Head / Lead Programmer Centaur Force
actually its trial and error to get the scale and position and even rotation for a weapon. The factors that effect this is how you creted the models size, and posistion in the model editor, the axis of the editor, (some modeling programs flip the Y axis for the Z axis as the newer modeling programs have corrected an incorrect axis created long ago by good old Autodesk CAD program called AutoCad). I have gotten a weapon to display in my demo for a game idea by inserting these lines of code that you can study and see if it will work for you. Now mind you it took about an hour of tweaking just to get it right!
// need to add a camera and name it camera to use it to
// attach the weapon to it for movement
scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();
// lets place a weapon in the scene
scene::IAnimatedMesh* weaponMesh = smgr->getMesh("../../media/sword.x");
// Create a node so we can attach the weapon to the camera
scene::IAnimatedMeshSceneNode* weaponNode = smgr->addAnimatedMeshSceneNode( weaponMesh, camera, -1 );
// Your animation speed may be different so play with the value
weaponNode->setAnimationSpeed(20);
// The next few lines are the ones that have to be hand
// tweaked and you may not at first see your weapon.
// The values below reflect my weapon setting to see
// it in front of me. My weapon had to be rotated 180
// degrees as thats how I modeled it and didnt want
// to rerig it and reanimate it.
weaponNode->setScale(core::vector3df(6,6,6));
weaponNode->setPosition(core::vector3df(0,-52,5));
weaponNode->setRotation(core::vector3df(0,-180,0));
// The final setup putting the camera in the level
// where it best suits the start of gameplay.
camera->setPosition(core::vector3df(-60,60,-40));
ahhhhhh.....my game keeps crashing every time i try to run it with that FPS weps view code....instead of a .x or whatever im using a .3ds file....but either way...it just keeps crashing when i start it...any ideas?