2017-06-12 105 views
0

我想在three.js中一次移动多个球。我做了一个创建球的功能,并使用它创建了三个球。之后,我创建了一个移动球的函数,它可以用于一个球,但每当我尝试将它们全部移动时,它都不起作用。任何帮助将非常感激。 下面是代码:在three.js中一次移动多个对象

球功能:

function Ball(valuex, valuey, valuez, ballname, color) 
{ 
    var balMaterial, balGeometry, balMesh; 
    balMaterial = new THREE.MeshLambertMaterial({ color: color}); 
    balGeometry = new THREE.SphereGeometry(0.3,50,50); 
    balMesh = new THREE.Mesh(balGeometry, balMaterial); 
    balMesh.position.set(valuex,valuey,valuez); 
    balMesh.name = ballname; 
    balMesh.ballDirection = new THREE.Vector3(); 
    balMesh.ballDirection.x = -5; 
    balMesh.ballDirection.z = 1; 
    balMesh.ballDirection.normalize(); 
    balMesh.moveSpeed = 25; 
    scene.add(balMesh); 
} 

移动球:

function moveBalls (ball) { 
var tempbal = scene.getObjectByName(ball); 
var ballDirection = tempbal.ballDirection; 
tempbal.position.add(speed.copy(ballDirection).multiplyScalar(clock.getDelta() * tempbal.moveSpeed)); 



    if (tempbal.position.x < -4.7) { 
     ballDirection.x = Math.abs(ballDirection.x); 
    } 
    if (tempbal.position.x > 4.7) { 
     ballDirection.x = -Math.abs(ballDirection.x); 
    } 
    if (tempbal.position.z > 12.2) { 
     ballDirection.z = -Math.abs(ballDirection.z); 
    } 
    if (tempbal.position.z < -12.2) { 
     ballDirection.z = Math.abs(ballDirection.z); 
    } 
    if (tempbal.moveSpeed > 0) 
    { 
     tempbal.moveSpeed = tempbal.moveSpeed - 0.002; 
    } 
} 

创建球:

Ball(0,4.5,0, "ball1", 0xffffff); 
Ball(2,4.5,0, "ball2", 0xffffff); 
Ball(0,4.5,6, "ball3", 0xff0000); 

动画场景:

function animateScene() 
{ 
    moveBalls("ball1"); 
    moveBalls("ball2"); 
    moveBalls("ball3"); 
    requestAnimationFrame(animateScene); 

    renderer.render(scene, camera); 
} 

PS:我是新来的,这是我的第一篇文章,所以如果我在这篇文章中做了任何错误,请告诉我,以便我可以从中学习。

+0

我认为“速度”是指在别处全球。什么是控制台输出? – vassiliskrikonis

+0

速度= Three.Vector3,它的输出是x = -0.022476105395295355,y = 0.0,z = -0.0044952210790590716 – seventhflame

回答

1

功能clock.getDelta()返回自上次致电clock.getDelta()以来经过的秒数,这意味着只有您的第一个球可能会移动。事实上,第一个球呼叫getDelta()(返回大于0的东西)。在相同的框架(意思是大约在同一时间),你可以拨打clock.getDelta()作为第二个球,这将返回0.同样的情况发生在第三球。

尽量做到以下几点:

function moveBalls (ball, deltaTime) { 
    var tempbal = scene.getObjectByName(ball); 
    var ballDirection = tempbal.ballDirection; 
    tempbal.position.add(speed.copy(ballDirection).multiplyScalar(deltaTime 
* tempbal.moveSpeed)); 

    if (tempbal.position.x < -4.7) { 
     ballDirection.x = Math.abs(ballDirection.x); 
    } 
    if (tempbal.position.x > 4.7) { 
     ballDirection.x = -Math.abs(ballDirection.x); 
    } 
    if (tempbal.position.z > 12.2) { 
     ballDirection.z = -Math.abs(ballDirection.z); 
    } 
    if (tempbal.position.z < -12.2) { 
     ballDirection.z = Math.abs(ballDirection.z); 
    } 
    if (tempbal.moveSpeed > 0) 
    { 
     tempbal.moveSpeed = tempbal.moveSpeed - 0.002; 
    } 
} 

// ... 

function animateScene() 
{ 
    var deltaTime = clock.getDelta() ; 
    moveBalls("ball1", deltaTime); 
    moveBalls("ball2", deltaTime); 
    moveBalls("ball3", deltaTime); 
    requestAnimationFrame(animateScene); 

    renderer.render(scene, camera); 
} 
+0

它的工作。非常感谢你 :) – seventhflame