最近在学习threejs中向量知识,这里做个回顾
方向向量
在数学中我们获取方向向量,假设有a,b两点,要获取a到b点的方向向量,那么我们的数学公式就会是b-a;同样的道理在threejs中也是遵循数学规律的。
假设相机位置为target,物体mesh位置是position,那么threejs中拿到方向向量也为: vector = target - position;
当然threejs也有提供计算方向向量的方法给我们用:
// 保存方向向量
const dir = new THREE.Vector3();
// 获得相机方向 camera为相机参数,上述target 为 camera.position
camera.getWorldDirection(dir);
// 获得单位向量
dir.length();
速度向量
速度是一个既有大小又有方向的物理量(来自百度),速度向量用来描述单位时间内物体移动的距离
// 描述物体初速度 表示x方向初速度为10,z方向上初速度为10
const v = new THREE.Vector3(10,0,10);
// 通过速度计算位移位置,假设移动时间为t
const dis = v.clone().multiplyScalar(t)
向量旋转
// 计算向量a与b的余弦值,获得旋转夹角
const a = new THREE.Vector3(5,5,5);
const b = new THREE.Vector3(0,10,0);
// 将向量归一化然后点乘获得旋转角度
const cos = a.normalize().dot(b.normalize());
const rad = Math.acos(cos);
// 获得角度
const angle = THREE.MathUtils.radTODeg(rad);