My journey began with the code below, which initiates a block moving in a straight line:
const bot_geometry = new THREE.BoxGeometry(1,1,1);
const bot_material = new THREE.MeshBasicMaterial( {color: 0x7777ff, wireframe: false} );
const bot = new THREE.Mesh( bot_geometry, bot_material );
bot.position.x = 1;
bot.position.y = 0;
bot.position.z = 1;
scene.add(bot);
//create random directions for bot
let direction = new THREE.Vector3(0.001, 0, 0.002); // amount to move per frame
function animate() {
bot.position.add(direction); // add to position
requestAnimationFrame(animate); // keep looping
}
requestAnimationFrame(animate);
I attempted to make it move randomly on the plane by using the two following lines of code, but now I am faced with two blocks moving in opposite directions along a straight line. Is there a straightforward method to have just one block move randomly on the plane?:
let reverseDirection = Math.floor(Math.random()*2) == 1 ? 1 : -1;
let direction = new THREE.Vector3(0.001*reverseDirection, 0, 0.002*reverseDirection);