Get and set bone translations

I was wondering what methods I could use to get a bone from a mesh skeleton and:



  • Get its translation and rotation

  • Manually control its rotation



So far, I know of these methods mentioned in another thread:

AnimControl.getBone(String), Bone.getModelSpacePosition()



I can't find them in the docs, so I thought I'd ask what the correct and up-to-date methods are for doing the above tasks. I'd also appreciate some explanation into the process - I understand that you "take control" of a bone to manually set its rotation, which prevents it from animating normally?

The getModelSpacePosition() call will return the bone position in model space, which you can then convert to world space using Spatial.localToWorld(). You can control a bone's rotation by calling Bone.setUserControl(true) and then using the call Bone.setUserTransforms() to set the rotation.

What about getting the bone's rotation? Is there something like a getModelRotation() for the bone?

No, Bone.java  doesn’t have a getter for rotations or translations, but you could easily create one in the source, as I have done myself.  All you’ll probably need is localRot and localPos.  If this really is an issue someone could commit it… :roll:





http://code.google.com/p/jmonkeyengine/source/browse/trunk/src/com/jmex/model/ogrexml/anim/Bone.java

1 Like

Why don't these methods work?




        public Vector3f getLocalPos() {
                return localPos;
        }

        public Quaternion getLocalRot() {
                return localRot;
        }

Momoko_Fan said:

The getModelSpacePosition() call will return the bone position in model space, which you can then convert to world space using Spatial.localToWorld(). You can control a bone's rotation by calling Bone.setUserControl(true) and then using the call Bone.setUserTransforms() to set the rotation.


how can I use localtoworld method ?
in javadoc I saw this

localToWorld(Vector3f in, Vector3f store)
Convert a vector (in) from this spatials' local coordinate space to world coordinate space.

what should vector in and store be?
Spatial.localToWorld(Spatial.getlocaltranslation,spatial.getworldtranslation) ?
I dont really get it, can you show me an example or explain it a bit?

I am kind of curious, why is everybody asking how to get the world position of a bone? Perhaps this issue can be addressed by a new feature added to the animation system.



Anyways, the localToWorld method, you specify the bone's modelspace position in the "in" argument, and you can specify "null" for the store to get a new vector as a return value. The "store" is only if you're trying to avoid allocations by using a preexisting vector object so its not required.

Momoko_Fan said:

I am kind of curious, why is everybody asking how to get the world position of a bone? Perhaps this issue can be addressed by a new feature added to the animation system.

Anyways, the localToWorld method, you specify the bone's modelspace position in the "in" argument, and you can specify "null" for the store to get a new vector as a return value. The "store" is only if you're trying to avoid allocations by using a preexisting vector object so its not required.

Thanks Momoko_Fan
I will try it tomorrow.
I dont know why other asking how to get the world position of a bone.
about me, I am trying to make a fighting game . that is why I need to get the world position of bones, to put boxes there, and during the fight I will check if they collide with each other.
I managed to put boxes in their place, and they stay in their place during animation (kick, punch etc)
now I am trying to make the forward,backward animation, and at the end of animation the character should stay there and dont jump back to its place before animation. but it somehow doesnt work getlocal,getmodelspace + my tricks but none of them worked, that is why I am trying localtoworld now.

through experience I feel that the whole invisible bones (skeleton) will always stay at 000 area and dont move along with mesh.
I think it might be better if they can and always move along with the mesh, and can change the position of mesh/Spatial during animation.
Momoko_Fan said:

I am kind of curious, why is everybody asking how to get the world position of a bone? Perhaps this issue can be addressed by a new feature added to the animation system.


Getting a bone's position and rotation is the only way to have any kind of accurate collision detection. If you want the hitboxes to move with an animated model, you have to connect spatials to the bones.

Sorry for resurrecting this thread, but I’m experimenting with these methods and I’m having a problem. I’m trying to “attach” a Node (pivot) to a character’s leg. Both leg.getModelSpacePosition() and leg.getLocalPosition() only return the position relative to the rest of the character, so I had to do this:



pivot.setLocalTranslation(leg.getModelSpacePosition().add(player.getLocalTranslation()));



It works okay, but it seems overcomplicated, especially since I need to add a third correction Vector3f. My question is, can I directly get the bone’s position to the “world,” or should I attach pivot to the character instead of the rootNode?

Actually its not correct to use the player local translation, you should instead use player.localToWorld(leg.getModelSpacePosition()), this will give the correct bone position in world space.

It is not built-in into the Bone class because really the Bone class is an internal class, designed to be as efficient as possible. It seems that attachments is one feature that isn’t there yet in jME3, so I added it to the issue tracker.