0
您好,感謝您的時間。我試圖移動一個對象並同時旋轉它。在執行兩個動作的同時,對象移動不規律。我已經讀過,旋轉以某種方式改變了軸。真的嗎?這是代碼。我曾嘗試使用世界軸和對象軸進行旋轉,但具有相同的問題。three.js可以同時旋轉和移動物體
var rotObjectMatrix;
function rotateAroundObjectAxis(object, axis, radians) {
rotObjectMatrix = new THREE.Matrix4();
rotObjectMatrix.makeRotationAxis(axis.normalize(), radians);
object.matrix.multiply(rotObjectMatrix);
object.rotation.setFromRotationMatrix(object.matrix);
}
var rotWorldMatrix;
// Rotate an object around an arbitrary axis in world space
function rotateAroundWorldAxis(object, axis, radians) {
rotWorldMatrix = new THREE.Matrix4();
rotWorldMatrix.makeRotationAxis(axis.normalize(), radians);
rotWorldMatrix.multiply(object.matrix);
object.matrix = rotWorldMatrix;
object.rotation.setFromRotationMatrix(object.matrix);
}
function spriteAI2() {
//var ranTen = Math.floor((Math.random()*7)+2);
var xAxis = new THREE.Vector3(1,0,0);
var yAxis = new THREE.Vector3(0,1,0);
var zAxis = new THREE.Vector3(0,0,1);
rotateAroundObjectAxis(meshSprite, zAxis, Math.PI/180);
if (meshSprite.position.x > 30 && meshSprite.position.x <= 450) {
meshSprite.translateX(-6);
} else if (meshSprite.position.y > 30 && meshSprite.position.y <= 250) {
meshSprite.translateY(-6);
} else if (meshSprite.position.z > 30 && meshSprite.position.z <= 350) {
meshSprite.translateZ(-6);
} else if (meshSprite.position.x < -30 && meshSprite.position.x >= -450) {
meshSprite.translateX(6);
} else if (meshSprite.position.y < -30 && meshSprite.position.y >= -250) {
meshSprite.translateY(6);
} else if (meshSprite.position.z < -30 && meshSprite.position.z >= -350) {
meshSprite.translateZ(6);
} else if (meshSprite.position.x < 31 && meshSprite.position.y < 31 && meshSprite.position.z < 31 && meshSprite.position.x > -31 && meshSprite.position.y > -31 && meshSprite.position.z > -31) {
var locX = Math.floor((Math.random()*450)+1);
locX *= Math.floor(Math.random()*2) == 1 ? 1 : -1;
var locY = Math.floor((Math.random()*250)+1);
locY *= Math.floor(Math.random()*2) == 1 ? 1 : -1;
var locZ = Math.floor((Math.random()*350)+1);
locZ *= Math.floor(Math.random()*2) == 1 ? 1 : -1;
scene.remove(meshSprite);
//texture.dispose();
c2ImgMaterial.dispose();
meshSprite.position.set(locX, locY, locZ);
scene.add(meshSprite);
//c2Sprite.clone;
}
}
請參閱three.js示例和three.js源代碼。從Object3D.js開始。如果您遇到問題,請學習如何設置jsfiddle,以便其他人可以看到實例。這裏是一個例子:http://jsfiddle.net/hbt9c/61/ – WestLangley
太棒了。會做。 – Kahless