Graphics 相对于鼠标位置旋转对象
目前,我正在使用鼠标位置的点积和(0,1)生成弧度来旋转对象,在three.js中 下面的代码工作正常,但对象“跳跃”,因为当Graphics 相对于鼠标位置旋转对象,graphics,geometry,three.js,dot-product,Graphics,Geometry,Three.js,Dot Product,目前,我正在使用鼠标位置的点积和(0,1)生成弧度来旋转对象,在three.js中 下面的代码工作正常,但对象“跳跃”,因为当clientX值介于window.innerWidth/2 onDocumentMouseMove : function(event) { // rotate circle relative to current mouse pos var oldPos = new THREE.Vector2(0, 1); Template.Main.mou
clientX
值介于window.innerWidth/2
onDocumentMouseMove : function(event) {
// rotate circle relative to current mouse pos
var oldPos = new THREE.Vector2(0, 1);
Template.Main.mouseCurrPos = new THREE.Vector2((event.clientX / window.innerWidth ) * 2 - 1, - (event.clientY / window.innerHeight) * 2 + 1);
Template.Main.mouseCurrPos.normalize();
//Template.Main.projector.unprojectVector(Template.Main.mouseCurrPos, Template.Main.scene);
var angle = oldPos.dot(Template.Main.mouseCurrPos);
Template.Main.mousePrevPos.x = event.clientX;
Template.Main.mousePrevPos.y = event.clientY;
if (event.clientX < window.innerWidth / 2) {
Template.Main.circle.rotation.z = -angle;
}
else {
Template.Main.circle.rotation.z = angle;
}
console.log(Template.Main.circle.rotation.z);
}
onDocumentMouseMove:函数(事件){
//相对于当前鼠标位置旋转圆
var oldPos=新的三个向量2(0,1);
Template.Main.mouseCurrPos=新的三个.Vector2((event.clientX/window.innerWidth)*2-1,-(event.clientY/window.innerHeight)*2+1);
Template.Main.mouseCurrPos.normalize();
//Template.Main.projector.unprojectVector(Template.Main.mousecurpos,Template.Main.scene);
变量角度=oldPos.dot(Template.Main.mousecurpos);
Template.Main.mousePrevPos.x=event.clientX;
Template.Main.mousePrevPos.y=event.clientY;
if(event.clientX if (event.clientX < window.innerWidth / 2) {
oldPos = new THREE.Vector2(0, -1);
}
else {
oldPos = new THREE.Vector2(0, 1);
}
if(event.clientX
然后“跳跃”会消失,但当鼠标位于窗口左侧时,旋转的效果会反转
即,鼠标向上逆时针旋转,反之亦然,这是不需要的
这令人沮丧
同样,如果我保留oldPos条件赋值,而忽略角的条件否定,跳跃会回来
您可以在此处看到演示:
非常感谢您提供的提示。为什么要将点积的结果用作角度(弧度)?点积给出角度的余弦(乘以向量的大小,但它们是单位向量和归一化向量,所以这无关紧要) 您可以将角度计算更改为
var angle = Math.acos(oldPos.dot(Template.Main.mouseCurrPos));
但是,您可能会得到错误的象限,因为可能有两个θ值满足cos(θ)=n。获取右象限中矢量角度(原点到鼠标位置)的常用方法是使用atan2()
这应该给出鼠标位置向量的角度,从(1,0)开始逆时针移动。通过一些实验可以确定零角的位置,以及正旋转的方向。为什么要使用点积的结果作为角度(弧度)?点积给出角度的余弦(乘以向量的大小,但它们是单位向量和归一化向量,所以这无关紧要) 您可以将角度计算更改为
var angle = Math.acos(oldPos.dot(Template.Main.mouseCurrPos));
但是,您可能会得到错误的象限,因为可能有两个θ值满足cos(θ)=n。获取右象限中矢量角度(原点到鼠标位置)的常用方法是使用atan2()
这应该给出鼠标位置向量的角度,从(1,0)开始逆时针移动。稍加实验就可以确定零角的位置以及正旋转的方向。您希望鼠标如何影响旋转?您希望鼠标如何影响旋转?阅读此文后,我意识到我以前做过此操作!atan2是当时的解决方案,现在也是,谢谢。@AlexW:De ja vu。。。我有时也会遇到这种情况。读完这篇文章后,我意识到我以前做过这件事!atan2是当时的解决方案,现在也是,谢谢。@AlexW:De ja vu。。。这种情况有时会发生在我身上。