Javascript three.js:子对象的奇怪旋转
我正在尝试实现一个2x2x2 Rubiks立方体的实验版本。我已经采纳了前两篇文章中关于子对象附着和分离问题的说明:并且能够成功地理解如何独立旋转两个面 然而,当我在代码中实现同样的功能时,当连续应用旋转时,我会得到奇怪的结果,这表明某些转换在某个地方出错,尽管我似乎不知道在哪里。但是,在旋转结束后,与父对象相关的每个矩阵都会重置,所以我真的不明白为什么会发生这种情况。但是,当独立应用时,旋转可以正常工作,这证实了我做了大约50%的事情是正确的:)。可在此处查看示例: 以下是相关的代码示例: -->用于旋转右侧面的事件处理程序Javascript three.js:子对象的奇怪旋转,javascript,rotation,three.js,Javascript,Rotation,Three.js,我正在尝试实现一个2x2x2 Rubiks立方体的实验版本。我已经采纳了前两篇文章中关于子对象附着和分离问题的说明:并且能够成功地理解如何独立旋转两个面 然而,当我在代码中实现同样的功能时,当连续应用旋转时,我会得到奇怪的结果,这表明某些转换在某个地方出错,尽管我似乎不知道在哪里。但是,在旋转结束后,与父对象相关的每个矩阵都会重置,所以我真的不明白为什么会发生这种情况。但是,当独立应用时,旋转可以正常工作,这证实了我做了大约50%的事情是正确的:)。可在此处查看示例: 以下是相关的代码示例: -
function rotateR()
{
for(var i = 0; i < cubies.length; i++)
if((pos(cubies[i]).x >= 53) && (pos(cubies[i]).x <= 55))
active.push(cubies[i]);
for(var i = 0; i < active.length; i++)
console.log(active[i]);
//reset pivot rotation
pivot.rotation.set( 0, 0, 0 );
pivot.updateMatrixWorld();
//attach active[i] cubies to the pivot
for (var i = 0; i < 4; i++)
THREE.SceneUtils.attach(active[i], scene, pivot);
console.log("attached!");
attachedR = true;
}
函数旋转器()
{
对于(变量i=0;i 如果((pos(cubies[i]).x>=53)和&(pos(cubies[i]).x我认为最大的问题是活动阵列从未清空
此外,您正在进行广泛的矩阵更新,而实际上并不需要
我重做了你的javaScript,现在它只是附加立方体,进行旋转,最后分离立方体并重置旋转
JavaScript
//Rotate Right face of cube
function rotateR()
{
//Find cubies that lie in the right face of cube and store them in active[i]
for(var i = 0; i < cubies.length; i++) {
var x = pos(cubies[i]).x;
if(x >= 54 && x <= 56)
active.push(cubies[i]);
}
if (active.length != 4) alert ("active num wrong");
//attach active[i] cubies to the pivot
for (var i = 0; i < 4; i++)
THREE.SceneUtils.attach(active[i], scene, pivot);
attachedR = true;
}
//Rotate Upper face of the cube
function rotateU()
{
//Find cubies that lie in the up face of cube and store them in active[i]
for(var i = 0; i < cubies.length; i++) {
var position = pos(cubies[i]);
if(position.y >= 54 && position.y <= 56) {
active.push(cubies[i]);
}
}
if (active.length != 4) alert ("active num wrong");
//attach active[i] cubies to the pivot
for (var i = 0; i < 4; i++)
THREE.SceneUtils.attach(active[i], scene, pivot);
attachedU = true;
}
function detachAndReset()
{
for(var i = 0; i < active.length; i++)
{
THREE.SceneUtils.detach(active[i], pivot, scene);
}
active.length = 0;
attachedR = false;
attachedU = false;
pivot.rotation.x = 0;
pivot.rotation.y = 0;
}
//get world position of cubies[i]
function pos(object)
{
var position = new THREE.Vector3();
position.setFromMatrixPosition( object.matrixWorld );
return position;
}
function render()
{
var endAnimation = false;
//Math.PI / 2 == 1.580000000000001
//Rotate Right face of cube
if(attachedR === true)
{
pivot.rotation.x += 0.02;
if(pivot.rotation.x >= Math.PI / 2.0) {
pivot.rotation.x = Math.PI / 2.0;
endAnimation = true;
}
}
//Math.PI / 2 == 1.580000000000001
//Rotate Upper face of cube
if(attachedU === true)
{
pivot.rotation.y += 0.02;
if(pivot.rotation.y >= Math.PI / 2.0) {
pivot.rotation.y = Math.PI / 2.0;
endAnimation = true;
}
}
renderer.render(scene, camera);
if (endAnimation) {
detachAndReset();
}
}
//旋转立方体的右侧
函数旋转器()
{
//找到位于立方体右侧的立方体,并将其存储在活动[i]中
对于(变量i=0;i=54&&x=54&&position.y=Math.PI/2.0){
pivot.rotation.x=Math.PI/2.0;
endAnimation=true;
}
}
//Math.PI/2==1.580000000000001
//旋转立方体的上表面
如果(附件===真)
{
枢轴旋转y+=0.02;
if(pivot.rotation.y>=Math.PI/2.0){
pivot.rotation.y=Math.PI/2.0;
endAnimation=true;
}
}
渲染器。渲染(场景、摄影机);
如果(结束动画){
分离和设置();
}
}
非常感谢您审阅和修改代码。一个小小的技术障碍导致了这么多错误。现在,程序运行得非常好!
//Rotate Right face of cube
function rotateR()
{
//Find cubies that lie in the right face of cube and store them in active[i]
for(var i = 0; i < cubies.length; i++) {
var x = pos(cubies[i]).x;
if(x >= 54 && x <= 56)
active.push(cubies[i]);
}
if (active.length != 4) alert ("active num wrong");
//attach active[i] cubies to the pivot
for (var i = 0; i < 4; i++)
THREE.SceneUtils.attach(active[i], scene, pivot);
attachedR = true;
}
//Rotate Upper face of the cube
function rotateU()
{
//Find cubies that lie in the up face of cube and store them in active[i]
for(var i = 0; i < cubies.length; i++) {
var position = pos(cubies[i]);
if(position.y >= 54 && position.y <= 56) {
active.push(cubies[i]);
}
}
if (active.length != 4) alert ("active num wrong");
//attach active[i] cubies to the pivot
for (var i = 0; i < 4; i++)
THREE.SceneUtils.attach(active[i], scene, pivot);
attachedU = true;
}
function detachAndReset()
{
for(var i = 0; i < active.length; i++)
{
THREE.SceneUtils.detach(active[i], pivot, scene);
}
active.length = 0;
attachedR = false;
attachedU = false;
pivot.rotation.x = 0;
pivot.rotation.y = 0;
}
//get world position of cubies[i]
function pos(object)
{
var position = new THREE.Vector3();
position.setFromMatrixPosition( object.matrixWorld );
return position;
}
function render()
{
var endAnimation = false;
//Math.PI / 2 == 1.580000000000001
//Rotate Right face of cube
if(attachedR === true)
{
pivot.rotation.x += 0.02;
if(pivot.rotation.x >= Math.PI / 2.0) {
pivot.rotation.x = Math.PI / 2.0;
endAnimation = true;
}
}
//Math.PI / 2 == 1.580000000000001
//Rotate Upper face of cube
if(attachedU === true)
{
pivot.rotation.y += 0.02;
if(pivot.rotation.y >= Math.PI / 2.0) {
pivot.rotation.y = Math.PI / 2.0;
endAnimation = true;
}
}
renderer.render(scene, camera);
if (endAnimation) {
detachAndReset();
}
}