Three.js - 如何旋转对象到LOOKAT一个点和定向对另一对象、Three、js、LOOKAT

2023-09-08 00:43:29 作者:请勿复制 ㄟ

我是新来的three.js和3D编程一般,所以这似乎是一个很简单的问题。理想情况下,我希望回答能帮助我了解的基本原则。

I'm new to three.js and 3d programming in general, so this may seem like a very simple question. Ideally, I hope the answer can help me understand the underlying principles.

我有一个对象,需要点在另一点(原点,在这种情况下,为简单起见),它可以很容易地与 Object3D.lookAt(点)来完成功能。此指向的对象的Z轴在点很好。

I have an object that needs to "point" at another point (the origin, in this case, for simplicity), which can be done easily with the Object3D.lookAt(point) function. This points the Z axis of the object at the point nicely.

我也想旋转我的对象,名为旁观者,绕Z轴,使得其X轴点大致走向另一个对象, refObj 。我知道,X轴不能在 refObj 直接点,除非该对象发生形成直角的起源。我想旁观者的X轴趴在由原产地 refObj 和旁观者,为diagramed如下:

I also want to rotate my object, called looker, around its Z axis such that its X axis points generally towards another object, refObj. I know that the X axis can't point directly at the refObj unless that object happens form a right angle with the origin. I want the X axis of looker to lie on the plane created by origin, refObj and looker, as diagramed below:

在做旋转的最简单的方法似乎是修改 looker.rotation.z ,但我不知道如何计算什么值应为。

The simplest way of doing the rotation would seem to be to modify looker.rotation.z, but I don't know how to calculate what the value should be.

在一般情况下,我想注视函数,它接受第二坐标到X轴将被导向的扩展版本。事情是这样的:

In general, I would like an extended version of the lookAt function which takes a second coordinate to which the X axis would be oriented. Something like this:

function lookAtAndOrient(objectToAdjust, pointToLookAt, pointToOrientXTowards)
{
  // First we look at the pointToLookAt
  objectToAdjust.lookAt(pointToLookAt);

  // Then we rotate the object
  objectToAdjust.rotation.z = ??;
}

我创建了一个的jsfiddle以上

推荐答案

你真正的意思是你想要的对象(的y轴对象的最高 - 矢量)被垂直于该平面。

What you are really saying is you want the y-axis of the object (the object's up-vector) to be orthogonal to the plane.

所有你需要做的就是设置对象的最高 - 矢量致电前注视(来源)

All you have to do is set the object's up-vector before you call lookAt( origin ).

您通过利用两个向量跨产品计算所需的最高矢量你知道横亘在飞机上。

You compute the desired up vector by taking a cross-product of two vectors you know lie in the plane.

下面是一个工作提琴: http://jsfiddle.net/rQasN/43/

Here is a working fiddle: http://jsfiddle.net/rQasN/43/

请注意,有两种解决方案,以您的问题,因为这两个所计算的矢量,它是否定将是正交的平面

Note that there are two solutions to your problem, as both the computed vector and it's negation will be orthogonal to the plane.

编辑:小提琴更新three.js r.71

fiddle updated to three.js r.71