Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FromToRotation working or not working? #1

Open
winxalex opened this issue Jan 24, 2019 · 1 comment
Open

FromToRotation working or not working? #1

winxalex opened this issue Jan 24, 2019 · 1 comment

Comments

@winxalex
Copy link

winxalex commented Jan 24, 2019

Not sure if I'm using correctly or there is some aprox bug.
Funciton works for rest of angles(vectors are in XZ plane), but when need to rotate from x=-0.9999999 to x=0.9999999 (z same in both 4.222,y=0) rotation gives me 180 rotation around Z instead of Y.

`function orthogonal(v) {

var x = Math.abs(v.x);

var y = Math.abs(v.y);

var z = Math.abs(v.z);

var other = x < y ? ( x < z ? pc.Vec3.RIGHT : pc.Vec3.FORWARD ) : ( y < z ? pc.Vec3.UP : pc.Vec3.FORWARD );

return new pc.Vec3().cross(v, other);

}

pc.Vec3.prototype.orthogonal = function() {

return orthogonal(this);

};

function fromToRotation(v1, v2, q) {

var kct = v1.dot(v2);

q = q || Q();

if (kct <= -0.999) {

    q.w = 0;

    var v = orthogonal(v1).normalize();

    q.x = v.x;

    q.y = v.y;

    q.z = v.z;

    return q;

}

//var half = V(v1).add(v2).scale(0.5);
//
var half = new pc.Vec3().copy(v1).add(v2).scale(0.5);

q.w = v1.dot(half);

var cross = new pc.Vec3().cross(v1, half);

q.x = cross.x;

q.y = cross.y;

q.z = cross.z;

return q.normalize();

}`

@winxalex
Copy link
Author

I can confirm there is approximation error. For example if v1==-v2, kct check fail I for example have got -0.94,...so following this link https://stackoverflow.com/questions/1171849/finding-quaternion-representing-the-rotation-from-one-vector-to-another I'm checking for

` var k= Math.sqrt(v1.lengthSq() * v2.lengthSq());

// if (kct <= -0.999) { //doesn't work when v1=-v2
// if (kct <= -0.97) {
if (kct / k == -1){
q.w = 0;

    var v = orthogonal(v1).normalize();

    q.x = v.x;

    q.y = v.y;

    q.z = v.z;

    return q;

}` 

but code can be probably even more optimized compared to half solution???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant