简体   繁体   English

如何使用three.js计算旋转度数和旋转到方向向量后的点?

[英]How to calculate the points after rotate degrees and rotate to direction vector by using three.js?

How to calculate the point A to H after rotate degrees and rotate to direction vector(-42,51,11) by using three.js?如何使用 three.js 旋转度数并旋转到方向矢量(-42,51,11)后计算点 A 到 H?

Thank you in advance and please excause my bad english.提前谢谢你,请原谅我的英语不好。 Best regards.此致。

const tree = [
    [   0,   0,   0],  // A
    [   2, 151,   2],  // B
    [ -62, 283,  63],  // C
    [  62, 296, -58],  // D 
    [-104, 334,  74],  // E
    [ -58, 338,  45],  // F
    [  67, 403, -55],  // G
    [ 105, 365, -86],  // H
  ] 

在此处输入图像描述

This code will apply a rotation to all the vectors in the tree of 90 degrees, around the axis defined as a vertex [-42, 51, 11] .此代码将围绕定义为顶点[-42, 51, 11]的轴对树中的所有向量应用 90 度的旋转。 So the rotation axis is defined as a line from [0,0,0] to [-42, 51, 11] , therefore point A does not change in rotation.所以旋转轴被定义为从[0,0,0][-42, 51, 11]的一条线,因此点A的旋转不会改变。

const tree = [
    [   0,   0,   0],  // A
    [   2, 151,   2],  // B
    [ -62, 283,  63],  // C
    [  62, 296, -58],  // D 
    [-104, 334,  74],  // E
    [ -58, 338,  45],  // F
    [  67, 403, -55],  // G
    [ 105, 365, -86],  // H
  ] 

// Define the rotation axis:
let axis = new THREE.Vector3(-42, 51, 11)

// Normalize the axis:
axis.normalize()

// Define the matrix:
let matrix = new THREE.Matrix4()

// Define the rotation in radians:
let radians = 90 * Math.PI / 180

// Rotate the matrix:
matrix.makeRotationAxis(axis, radians)

// Now apply the rotation to all vectors in the tree
let newTree = []
for(const vector of tree) {
  // Define the vector3:
  let vec = new THREE.Vector3(vector[0], vector[1], vector[2])
  vec.applyMatrix4(matrix)
  newTree.push(vec.toArray()) // toArray is optional, you may want to keep the original Vector3 object
}

// newTree now holds the rotated vectors
console.log(newTree)

See https://codepen.io/mtveerman/pen/PoZZpvw for working version工作版本见https://codepen.io/mtveerman/pen/PoZZpvw

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM