Apply a rotation to this that rotates the fromDir vector to point along toDir and store the result in dest . Since there can be multiple possible rotations, this method chooses the one with the shortest arc. If Q is this qu
(double fromDirX, double fromDirY, double fromDirZ, double toDirX, double toDirY, double toDirZ, Quat dest)
| 1248 | * @param dest will hold the result |
| 1249 | * @return this |
| 1250 | */ |
| 1251 | public Quat rotateTo(double fromDirX, double fromDirY, double fromDirZ, double toDirX, double toDirY, double toDirZ, Quat dest) { |
| 1252 | double fromLength = Math.sqrt(fromDirX * fromDirX + fromDirY * fromDirY + fromDirZ * fromDirZ); |
| 1253 | double fromX = fromDirX / fromLength; |
| 1254 | double fromY = fromDirY / fromLength; |
| 1255 | double fromZ = fromDirZ / fromLength; |
| 1256 | double toLength = Math.sqrt(toDirX * toDirX + toDirY * toDirY + toDirZ * toDirZ); |
| 1257 | double toX = toDirX / toLength; |
| 1258 | double toY = toDirY / toLength; |
| 1259 | double toZ = toDirZ / toLength; |
| 1260 | double dot = fromX * toX + fromY * toY + fromZ * toZ; |
| 1261 | double x, y, z, w; |
| 1262 | if (dot < 1e-6 - 1.0) { |
| 1263 | /* vectors are negation of each other */ |
| 1264 | double axisX = 0.0; |
| 1265 | double axisY = -fromZ; |
| 1266 | double axisZ = fromY; |
| 1267 | if (axisX * axisX + axisY * axisY + axisZ * axisZ < 1E-6) { |
| 1268 | axisX = fromZ; |
| 1269 | axisY = 0.0; |
| 1270 | axisZ = -fromX; |
| 1271 | } |
| 1272 | double angleR = Math.PI; |
| 1273 | double s = Math.sin(angleR / 2.0); |
| 1274 | x = axisX * s; |
| 1275 | y = axisY * s; |
| 1276 | z = axisZ * s; |
| 1277 | w = Math.cos(angleR / 2.0); |
| 1278 | } else if (dot < 1.0) { |
| 1279 | double s = Math.sqrt((1.0 + dot) * 2.0); |
| 1280 | double invs = 1.0 / s; |
| 1281 | double crossX = fromY * toZ - fromZ * toY; |
| 1282 | double crossY = fromZ * toX - fromX * toZ; |
| 1283 | double crossZ = fromX * toY - fromY * toX; |
| 1284 | x = crossX * invs; |
| 1285 | y = crossY * invs; |
| 1286 | z = crossZ * invs; |
| 1287 | w = s * 0.5; |
| 1288 | double norm = Math.sqrt(x * x + y * y + z * z + w * w); |
| 1289 | x /= norm; |
| 1290 | y /= norm; |
| 1291 | z /= norm; |
| 1292 | w /= norm; |
| 1293 | } else { |
| 1294 | /* vectors are parallel, don't change anything */ |
| 1295 | return this; |
| 1296 | } |
| 1297 | /* Multiply */ |
| 1298 | dest.set(this.w * x + this.x * w + this.y * z - this.z * y, this.w * y - this.x * z + this.y * w + this.z * x, this.w * z + this.x * y - this.y * x + this.z * w, |
| 1299 | this.w * w - this.x * x - this.y * y - this.z * z); |
| 1300 | return this; |
| 1301 | } |
| 1302 | |
| 1303 | /** |