Rotating a bone in a world space

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
JunkerKun
Posts: 97
Joined: Mon Jan 28, 2013 12:52 am

Rotating a bone in a world space

Post by JunkerKun »

Hey there. Haven't been working with irrlicht in a while.
I have a problem with bones rotation.
What I'm trying to do is rotate character's bone (torso) regardless of parent bone rotation. However when I rotate bone's local matrix, it rotates it locally (as in along parent's facing direction). Let me show you my code:

Code: Select all

for (int i = 0; i < bones.size(); i++) {
	AnimationBone* animationBone = bones.at(i);
	IBoneSceneNode* bone = animationBone->meshBone;

	if (animationBone->rotation != Util::VECTOR3DFZERO) {
		if (bone != NULL) {
			CMatrix4<f32> trans = CMatrix4<f32>(bone->getRelativeTransformation());
			CMatrix4<f32> rot; rot.setRotationDegrees(animationBone->rotation);
			CMatrix4<f32> _invParentTrans; bone->getParent()->getAbsoluteTransformation().getInverse(_invParentTrans);

			trans *= _invParentTrans;
			trans *= rot;
			trans *= bone->getParent()->getAbsoluteTransformation();

			bone->setRotation(trans.getRotationDegrees());
		};
	};
};
I was successful when I was making my own skinning with OpenGL, however it doesn't work here.
From what I understand I implemented skinning in global space. Irrlicht however uses local space.
Is there a way to rotate a bone in world space without actually changing its skinning space? Because it kinda scrambles the whole model around.
CuteAlien
Admin
Posts: 9643
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Rotating a bone in a world space

Post by CuteAlien »

Sorry, I don't think there's a way except switching between local and global skinning space. Thought I've rather little experience with the animation system unfortunately. Maybe it's somehow possible to add yet another parent and rotate that? (assuming this is about blending local and global animation - not sure I understand the exact problem you try to fix)

Tiny hint for less typing - there's a typedef "matrix4" for CMatrix4<f32>. No need for casts - it's the same.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
JunkerKun
Posts: 97
Joined: Mon Jan 28, 2013 12:52 am

Re: Rotating a bone in a world space

Post by JunkerKun »

Oof. That's a bummer.
Thanks for the answer. I guess I'll have to integrate my own skinning system into the engine.
Tiny hint for less typing - there's a typedef "matrix4" for CMatrix4<f32>. No need for casts - it's the same.
Yeah, just realised it. Thanks.
CuteAlien
Admin
Posts: 9643
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Rotating a bone in a world space

Post by CuteAlien »

OK, but don't take my word for gospel here - really an area I know little about. And I'm still not understanding the exact problem. As in - why you can't switch skinning space when you want another skinning space. Is this about blending (aka rotating part of the body to some target while keeping the normal rotation going or stuff like that?)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
JunkerKun
Posts: 97
Joined: Mon Jan 28, 2013 12:52 am

Re: Rotating a bone in a world space

Post by JunkerKun »

Not really blending, but yes. I want my character to rotate torso independent from its animation. Like aiming for third person shooter. So even if torso is rotated 90 degrees to the right I want to add some rotation forward along camera's X axis.
I'm trying to convert vector3df(1,0,0) to bone's local space so I could rotate it along this axis, however I'm not really successful in that. matrix4.inverseRotateVect (bone's absolute transformation) gives me.... strange results. It seems like it gives me local direction.
JunkerKun
Posts: 97
Joined: Mon Jan 28, 2013 12:52 am

Re: Rotating a bone in a world space

Post by JunkerKun »

I think I managed to do this. I indeed needed to calculate proper direction vector. here is the code:

Code: Select all

	skinnedController->UpdateBones();

	for (int i = 0; i < bones.size(); i++) {
		AnimationBone* animationBone = bones.at(i);
		IBoneSceneNode* bone = animationBone->meshBone;

		if (animationBone->rotation != Util::VECTOR3DFZERO) {
			if (bone != NULL) {
				matrix4 boneTrans = matrix4(bone->getAbsoluteTransformation());
				matrix4 rBoneTrans = matrix4(bone->getRelativeTransformation());

				vector3df right(1, 0, 0);
				boneTrans.inverseRotateVect(right);
				right.normalize();

				matrix4 rot; rot.setRotationAxisRadians(animationBone->rotation.X * DEGTORAD, right);
				rBoneTrans *= rot;
				bone->setRotation(rBoneTrans.getRotationDegrees());
			};
		};
	};
CuteAlien
Admin
Posts: 9643
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Rotating a bone in a world space

Post by CuteAlien »

Could be inverse transformation contains more transformations than you want to invert (like scaling/translation probably both don't need to be inverted).
But guess you figured that out youself already :-) Nice.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply