I am attempting to manipulate an object in Three.js using Physi.js. The camera is linked to the moving mesh, and I am moving it using .setLinearVelocity();
Additionally, I rotate it using .setAngularVelocity();
However, the issue I am facing is that while I can rotate the mesh in the desired direction, when I move it forward, it does not move towards the destination I am looking at. Any help on this matter would be greatly appreciated.
Below is the code I am using for the movement:
//mover box
var geoMover = new THREE.BoxGeometry(20,20);
var matMover = new Physijs.createMaterial(new THREE.MeshPhongMaterial({color: 0xff00E3, specular: 0xffffff, shininess: 60}),.0,.2);
var mover = new Physijs.CapsuleMesh(geoMover, matMover,1);
mover.position.x =0;
mover.position.y = 30;
mover.position.z = 0;
mover.setAngularFactor(THREE.Vector3(0,0,0));
scene.add(mover);
mover.add(camera);
//render
function render(){
renderer.clear();
camera.lookAt(scene.position);
walk();
renderer.render(scene, camera);
requestAnimationFrame(render);
}
render();
//walk
function walk(){
//Listening for events
var delta = clock.getDelta();
var physisMove = 10;
if(keyboard.pressed('left')){
mover.setAngularVelocity({z:0,y:1,x:0});
istMoveLeft = true;
}
if(keyboard.pressed('up')){
mover.setLinearVelocity({z: -physisMove, y:0,x:0});
isMoveForward = true;
}
if(keyboard.pressed('right')){
mover.setAngularVelocity({z:0,y:-1,x:0});
isMoveRight = true;
}
if(keyboard.pressed('down')){
mover.setLinearVelocity({z: physisMove, y:0,x:0});
isMoveBackward = true;
}
document.addEventListener("keyup", function(event){
var code = event.keyCode;
if(code == 38) mover.setLinearVelocity({z:0, y:0,x:0});
if(code == 40) mover.setLinearVelocity({z:0, y:0,x:0});
if(code == 37){
mover.setLinearVelocity({z:0, y:0,x:0});
mover.setAngularVelocity({z:0,y:0,x:0});
}
if(code == 39){
mover.setLinearVelocity({z:0, y:0,x:0});
mover.setAngularVelocity({z:0,y:0,x:0});
}
});
mover.setAngularFactor({z:0,x:0,y:0});
/*
document.addEventListener('keyup', function(event){
var code = event.keyCode;
if(code == 37) istMoveLeft = false;
if(code == 38) isMoveForward = false;
if(code == 39) isMoveRight = false;
if(code == 40) isMoveBackward = false;
});
*/
}