首页 > 解决方案 > Stretching towards a vector

问题描述

I am stumped.

I've created a line geometry in the shape of a spring, and I'd like to be able to transform this spring such that one end remains fixed in place while the other follows a vector3 position. The object3D.lookAt() method gets it pointing the right way, but I can't figure out a way to stretch it to that point.

I figure it'd be a matrix4.makeTransform() but I don't know how to calculate the amounts to scale in each direction. I thought it'd be simply dividing the x, y, and z coordinates of the endpoint of my spring and the target point, but that hasn't worked at all.

Is there a method to do this that I've somehow missed?

标签: three.js

解决方案


您的 .lookAt 调用,使弹簧点成为自己的 z 轴(希望轴沿着弹簧的长度运行!如果您的网格设置正确!)

一旦你把它指向目标,你需要弄清楚目标有多远......

使用类似的东西

distance = new THREE.Vector3().copy(targetPoint).sub( springObject.position).length()


一旦您知道该距离,并假设您知道弹簧模型的默认长度,那么只需将弹簧的 z 轴比例设置为距离/defaultLength .. 类似于:

   springObject.scale.z = defaultLength/distance

如果您已经将弹簧模型缩放了一定数量,这可能会有点复杂,您也必须考虑到这一点。


推荐阅读