question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with makeRotationFromQuaternion

See original GitHub issue

Hello,

I believe the function makeRotationFromQuaternion gives incorrect results.

The R function below returns a rotation matrix which transforms U to V.

getRotation <- function(U,V){
  ma <- sqrt(c(crossprod(U)))
  mb <- sqrt(c(crossprod(V))) # ?? no sense if ma /= mb ...
  d <- c(tcrossprod(U, t(V)))
  c <- sqrt(ma*mb+d)
  ma2 <- sqrt(2)*ma
  r <- 1/ma2/c
  W <- crossProduct(U,V)
  q <- c(c/ma2, r*W)
  quaternion2matrix(q) 
}

I have tested it and it works.

The Javascript version:

function getRotation(U, V) {
    var ma = U.length();
    var mb = V.length(); // should be = ma
    var d = U.x * V.x + U.y * V.y + U.z * V.z;
    var c = Math.sqrt(ma * mb + d);
    var ma2 = Math.sqrt(2) * ma;
    var r = 1 / ma2 / c;
    var W = U.clone().cross(V).multiplyScalar(r);
    var quat = new THREE.Quaternion().set(c / ma2, W.x, W.y, W.z);
    return new THREE.Matrix4().makeRotationFromQuaternion(quat);
}

Test:

var u = new THREE.Vector3(1,0,0);
var v = new THREE.Vector3(1,2,3).normalize();
var R = getRotation(u,v).transpose().elements;
console.log(R[0] * u.x + R[1] * u.y + R[2] * u.z); // should be v.x, found 0.492719
console.log(R[4] * u.x + R[5] * u.y + R[6] * u.z); // should be v.y, found -0.33818
console.log(R[8] * u.x + R[9] * u.y + R[10] * u.z); // should be v.z, found -0.80178

The matricial product R*u should give v, but it does not.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
stlacommented, Nov 23, 2018

Yeah, that’s right. The order is not the same and you were right, in three.js we have to do: var quat = new THREE.Quaternion().set(W.x, W.y, W.z, c / ma2); And everything works fine! Sorry!

0reactions
stlacommented, Nov 23, 2018

Wait, I see what you meant… My quaternions in R and the three.js quaternions do not use the same order, perhaps. I check…

Read more comments on GitHub >

github_iconTop Results From Across the Web

rotation issue on physijs - javascript - Stack Overflow
I have 2 cubes, one with phisy.js and another with three.js, and i have a function for rotate them when you press the...
Read more >
SkinnedMesh transform issue applying transforms to bones
The issue comes when i try and apply these positions to a SkinnedMesh ... makeRotationFromQuaternion( quat ); lmrix.multiply(qmix) bone.
Read more >
Into Vertex Shaders Addendum 1: Matrix Math and You
If you are unfamiliar with this topic, it can seem quite daunting. But you can go a long way ... makeRotationFromQuaternion(quaternion);.
Read more >
math32 - Go Packages
MakeRotationFromQuaternion sets this matrix as a rotation matrix from the specified quaternion. Returns pointer to this updated matrix.
Read more >
Is there a way to look at position without moving the vertices ...
makeRotationFromQuaternion( mesh.quaternion ) ); ... It solved my problem, but to be more specific to the question; you'd have to store the quaternion...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found