I spend the last 3 days trying to figure out how to calculate the quaternion between 2 vectors (or planes really) and 2 other vectors/planes … and lost a good amount of my hair over it with no luck so far ;-/ … hopefully one of you awesome math wizards can tell me what i am doing wrong!
I am trying to place connectors (in this case hinges, consisting of hinge arm and hinge plate) between 2 cabinet panels. The calculation of direction (vector going into the panel) and materialdirection (vector of which way the material should point to given by the other panels surface) is correct.
The connector (hinges) are defined as pointing in V1(0,0,1) (going into the panels => direction) and U1(1,0,0) (pointing towards the other panel => materialdirection).
The quaternion i am looking for is the one that transforms U1/V1 into direction/materialdirection.
I tried with 2 different approaches, first one seems to work the best, second one not that great … but none works perfect for every case. Usually a handful of hingeplates/arms is always the wrong way around ;-/
public place1(
position: Vector3,
direction: Vector3,
materialDirection: Vector3
) {
this.position(position.x, position.y, position.z);
var qrot = new Quaternion();
qrot.setFromUnitVectors(new Vector3(0, 0, -1), direction);
var mdir = new Vector3(-1, 0, 0).applyQuaternion(qrot);
var angle = mdir.angleTo(materialDirection);
var qrot2 = new Quaternion();
qrot2.setFromAxisAngle(direction, angle);
qrot.multiply(qrot2);
var eul = new Euler();
eul.setFromQuaternion(qrot);
this.rotation(
CabiGeometry.rad2Degree(eul.x),
CabiGeometry.rad2Degree(eul.y),
CabiGeometry.rad2Degree(eul.z)
);
}
and test 2:
public place2(
position: Vector3,
direction: Vector3,
materialDirection: Vector3
) {
this.position(position.x, position.y, position.z);
var u1 = new Vector3(1, 0, 0);
var v1 = new Vector3(0, 0, -1);
var u2 = materialDirection.clone();
var v2 = direction.clone();
var n1 = u1.cross(v1);
var n2 = u2.cross(v2);
n1.normalize();
n2.normalize();
var m: Vector3 = n1.clone().add(n2);
m.normalize();
var axis: Vector3 = m.clone().cross(n2);
var angle = m.dot(n2);
var qrot: Quaternion = new Quaternion(axis.x, axis.y, axis.z, angle);
qrot.normalize();
var eul = new Euler();
eul.setFromQuaternion(qrot);
this.rotation(
CabiGeometry.rad2Degree(eul.x),
CabiGeometry.rad2Degree(eul.y),
CabiGeometry.rad2Degree(eul.z)
);
}