自定义rotateTowards函数未按预期工作。 [英] Custom rotateTowards function is not working as expected.

查看:116
本文介绍了自定义rotateTowards函数未按预期工作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否适合这样的话题,但我会试一试。我正在为我的WebGL项目构建一个框架,目前我遇到了一个函数问题,该函数将一个对象旋转到具有给定角速度的目标向量。在Unity引擎中,它被称为rotateTowards(),因为我从头开始编码我的框架,所以我基于我对这个函数如何工作的观察。最近我放弃了使用nLERP方法在这个函数中插入两个四元数的想法,因为角速度似乎是更合适的因素。该功能似乎几乎正常工作 - 只有在相机旋转中没有滚动或俯仰角度时它才能正常工作,否则会产生奇怪的曲线并在可笑的长时间后设置最终位置。我怀疑它可能与我在两个矢量之间搜索最短旋转路径的方法有关(基于它们之间的角度和从它们的叉积计算的旋转轴)。我一直试图解决这个问题很长一段时间,但没有任何成功。我也选择的是将旋转仅限制为俯仰和偏航轴(因此不会涉及滚动)。我关于如何做到这一点的第一个想法是将旋转转换为欧拉角度矢量,使滚动角度无效并将其转换回四元数,但这样做会引入Gimbal Lock问题,这在这种情况下是不协调的。下面这个函数的代码:

I don't know if this is an appropriate place for topics like this, but I'll give it a try. I'm building a framework for my WebGL project and at the moment I have a problem with a function which will rotate an object towards a target vector with a given angular speed. In Unity engine it's called rotateTowards(), and since I'm coding my framework from a scratch, I'm basing on my observations about how this function works. Lately I have abandoned an idea about interpolating two quaternions in this function using nLERP method, because an angular speed seems to be far more suitable factor. The function seems to be almost working - it works well only when there is no roll or pitch angle in the camera rotation involved, otherwise it makes strange curves and sets the final position after ridiculously long time. I suspect it may have something to do with me methodology of searching the shortest path of rotation between two vectors (which bases on an angle between them and a rotation axis calculated from their cross product). I've been trying to fix this for a long time, but without any success. What I also opt for is to restrict the rotation only to pitch and yaw axises (so no roll would be involved). My very first idea about how to do this was to convert the rotation to an Euler angles vector, nullify roll angle and convert it back to quaternion, but doing so will introduce Gimbal Lock problem, which is incongruous in this case. The code of this function below:

rotateTowards: function(vecTarget, angularSpeed) {
	// If is on the same position
	if (this.position.isEqual(vecTarget)) {
		return;
	}

	var vecStart = this.rotation.getForwardVector();
	// Add vectors instead of subdividing them because the camera is facing -Z direction
	vecTarget.addSelf(this.position);
	vecTarget.normalize();
	var angleLeft = Math.acos(vecStart.dot(vecTarget));

	// If already rotated towards the target
	if (Math.areScalarsSimilar(angleLeft, 0)) {
		return;
	}
	// If vectors are opposite
	else if (Math.areScalarsSimilar(angleLeft, Math.PI)) {
		// Turn around up-axis a bit
		this.rotation.multiply(this.tempQuat.axisToQuaternion(angularSpeed * deltaT, upVec()));
	}
	// If vectors casually differs
	else {
		var rotAxis = vecStart.cross(vecTarget);

		// If able to make another full rotation step
		if (angleLeft > angularSpeed * deltaT) {
			this.rotation.multiply(this.tempQuat.axisToQuaternion(angularSpeed * deltaT, rotAxis));
		}
		// If the angle left is smaller than the angular speed
		else {
			// Set the final rotation
			this.rotation.multiply(this.tempQuat.axisToQuaternion(angleLeft, rotAxis));
		}
	}

	MVMatrix.setRotation(this.rotation.quaternionToMatrix());
},



拯救世界,并给我一个提示这个代码/我关于这个函数的概念有什么问题^^


Save the world, and give me a hint what is wrong with this code / my notion about this function ^^

推荐答案

这篇关于自定义rotateTowards函数未按预期工作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆