get a bone for location of weapon fire

Problems building or running the engine, queries about how to use features etc.
Post Reply
Nickak2003
Goblin
Posts: 272
Joined: Thu Jun 10, 2004 4:19 am
x 26

get a bone for location of weapon fire

Post by Nickak2003 »

Do I use bone->_getDerivedPosition for the spot where a bullet should fire from? Ive tried it and it is not correct from the bone placement in blender. It must be either a blender problem, an exporter problem, or I'm not using ogre correctly?
here is what I'm doing to try to get the position to fire from:

Code: Select all

		Ogre::Entity* spaceShip1 = Graphics::get().getSceneManager()->createEntity("jet1.mesh");
		Ogre::SceneNode* sn = Graphics::get().getSceneManager()->getRootSceneNode()->createChildSceneNode();
		sn->attachObject(spaceShip1);

		Ogre::Skeleton* skel = spaceShip1->getSkeleton();
		Ogre::Bone* bone = skel->getBone("leftgun");

             
		locationOfGun = bone->_getDerivedPosition();
Is this correct?
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5299
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1279
Contact:

Re: get a bone for location of weapon fire

Post by dark_sylinc »

IIRC that transform is local to the object.

If my math doesn't fail me, you need to do:

Code: Select all

locationOfGun = sn->_getDerivedPosition() + sn->_getDerivedOrientation() * (sn->_getDerivedScale() * bone->_getDerivedPosition());
Take in mind that Ogre bones are at the head of the bone, while Blender bones have a head and a tail. If you need the position of the tail, then create another connected bone, so that the head of this new bone will be where the tail of the previous bone is.
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: get a bone for location of weapon fire

Post by c6burns »

If the bone is part of a larger skeletal heirarchy, you need to traverse upwards ... because yes as dark_sylinc is explaining the bone position is in local space

Code: Select all

Ogre::Vector3 GetBoneWorldPosition(Ogre::Entity* ent, const Ogre::String &boneName)
{
	Ogre::Bone* bone = ent->getSkeleton()->getBone(boneName);
	Ogre::Vector3 world_position = bone->_getDerivedPosition();
 
	//multiply with the parent derived transformation
	Ogre::Node *pParentNode = ent->getParentNode();
	Ogre::SceneNode *pSceneNode = ent->getParentSceneNode();
	while (pParentNode != NULL)
	{
		//process the current i_Node
		if (pParentNode != pSceneNode)
		{
			//this is a tag point (a connection point between 2 entities). which means it has a parent i_Node to be processed
			world_position = pParentNode->_getFullTransform() * world_position;
			pParentNode = pParentNode->getParent();
		}
		else
		{
			//this is the scene i_Node meaning this is the last i_Node to process
			world_position = pParentNode->_getFullTransform() * world_position;
			break;
		}
	}
	return world_position;
}
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: get a bone for location of weapon fire

Post by Kojack »

Here's the shortcut. :)

Code: Select all

locationOfGun = sn->convertLocalToWorldPosition(bone->_getDerivedPosition());
It does all the hierarchy transform stuff internally.

There's also sn->convertLocalToWorldOrientation for orientation (so you can make the bullet move in the direction of the bone, in case it's a turret), and world to local versions of both functions.
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: get a bone for location of weapon fire

Post by c6burns »

So much easier than mine! Thanks, Kojack!
Nickak2003
Goblin
Posts: 272
Joined: Thu Jun 10, 2004 4:19 am
x 26

Re: get a bone for location of weapon fire

Post by Nickak2003 »

Thanks! So in blender, the bone head is not aligning with the cursor head, what is the problem? here is a pic:
Image
Nickak2003
Goblin
Posts: 272
Joined: Thu Jun 10, 2004 4:19 am
x 26

Re: get a bone for location of weapon fire

Post by Nickak2003 »

Nevermind, figured it out, the armature had an offset
sdong21
Gnoblar
Posts: 7
Joined: Sat Oct 04, 2014 1:48 pm

Re: get a bone for location of weapon fire

Post by sdong21 »

This solution works for almost all conditions, thanks!
The other function convertLocalToWorldPosition(mBone->_getDerivedPosition()) only works when it has no large skeletal heirachy according to my test.
c6burns wrote:If the bone is part of a larger skeletal heirarchy, you need to traverse upwards ... because yes as dark_sylinc is explaining the bone position is in local space

Code: Select all

Ogre::Vector3 GetBoneWorldPosition(Ogre::Entity* ent, const Ogre::String &boneName)
{
	Ogre::Bone* bone = ent->getSkeleton()->getBone(boneName);
	Ogre::Vector3 world_position = bone->_getDerivedPosition();
 
	//multiply with the parent derived transformation
	Ogre::Node *pParentNode = ent->getParentNode();
	Ogre::SceneNode *pSceneNode = ent->getParentSceneNode();
	while (pParentNode != NULL)
	{
		//process the current i_Node
		if (pParentNode != pSceneNode)
		{
			//this is a tag point (a connection point between 2 entities). which means it has a parent i_Node to be processed
			world_position = pParentNode->_getFullTransform() * world_position;
			pParentNode = pParentNode->getParent();
		}
		else
		{
			//this is the scene i_Node meaning this is the last i_Node to process
			world_position = pParentNode->_getFullTransform() * world_position;
			break;
		}
	}
	return world_position;
}
Post Reply