Jump to content

Rotate bone in Space.World coordinates


Koroldev
 Share

Recommended Posts

Hi guys, I have little problem.

There is a function, that rotates bone

// type script function 
public static rotateBone(bone: BABYLON.Bone, quat: BABYLON.Quaternion): void {

        var result = bone.getLocalMatrix();

        var xx = quat.x * quat.x;
        var yy = quat.y * quat.y;
        var zz = quat.z * quat.z;
        var xy = quat.x * quat.y;
        var zw = quat.z * quat.w;
        var zx = quat.z * quat.x;
        var yw = quat.y * quat.w;
        var yz = quat.y * quat.z;
        var xw = quat.x * quat.w;

        result.m[0] = 1.0 - (2.0 * (yy + zz));
        result.m[1] = 2.0 * (xy + zw);
        result.m[2] = 2.0 * (zx - yw);
        result.m[3] = 0;
        result.m[4] = 2.0 * (xy - zw);
        result.m[5] = 1.0 - (2.0 * (zz + xx));
        result.m[6] = 2.0 * (yz + xw);
        result.m[7] = 0;
        result.m[8] = 2.0 * (zx + yw);
        result.m[9] = 2.0 * (yz - xw);
        result.m[10] = 1.0 - (2.0 * (yy + xx));
        result.m[11] = 0;

        result.m[15] = 1.0;
    }

But this function rotates bone in local space, and I need rotetes bone in world space. What I should change in function?

Link to comment
Share on other sites

3 hours ago, Deltakosh said:

You can get a world space quaternion and transform it to a local space quaternion using inverse world matrix

Can you explain me more clearly? Because I am confusing in quternions and matrixes.

1. How can I get world space quaternion from bone?

2. When I get world quaternion, than I should change desired angle and convert new quaternion in local quaternion?

3. How to convert world quaternion to local and apply it to bone?

Thank!

Link to comment
Share on other sites

11 hours ago, Deltakosh said:

1. bone.getWorldMatrix()

2. Matrix.Invert(bone.getWorldMatrix())

3.var finalQuat = Quaternion.FromRotationMatrix(Matrix.Invert(bone.getWorldMatrix()))

Okay, I wrote code, and nothing happens

var worldMat = this.boneTorsoDown.getWorldMatrix();

worldMat = worldMat.multiply(BABYLON.Matrix.RotationX(r));

var localMat = BABYLON.Matrix.Invert(worldMat);

var quat = BABYLON.Quaternion.FromRotationMatrix(localMat);

var result = this.boneTorsoDown.getLocalMatrix();
		
var xx = quat.x * quat.x;
var yy = quat.y * quat.y;
var zz = quat.z * quat.z;
var xy = quat.x * quat.y;
var zw = quat.z * quat.w;
var zx = quat.z * quat.x;
var yw = quat.y * quat.w;
var yz = quat.y * quat.z;
var xw = quat.x * quat.w;

result.m[0] = 1.0 - (2.0 * (yy + zz));
result.m[1] = 2.0 * (xy + zw);
result.m[2] = 2.0 * (zx - yw);
result.m[3] = 0;
result.m[4] = 2.0 * (xy - zw);
result.m[5] = 1.0 - (2.0 * (zz + xx));
result.m[6] = 2.0 * (yz + xw);
result.m[7] = 0;
result.m[8] = 2.0 * (zx + yw);
result.m[9] = 2.0 * (yz - xw);
result.m[10] = 1.0 - (2.0 * (yy + xx));
result.m[11] = 0;

result.m[15] = 1.0;

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...