Three.js vs Ammo.js Make player move left and right in FPS mode with dynamic playerPhysicsBody

58 views Asked by At

I attach to the player(camera) physics body. I have nice working forwand and backward but lft and right always faild in some way.

I dont want kinematic like ussually controls object containe. I wanna my graphics to follow my physics body.

Here is most important code:

    if (this.moveForward == true) {
      // WORKS PERFECT IN ANY DIRECTION
      this.pos.copy(this.raycaster.ray.direction);
      this.pos.multiplyScalar(12);
      this.playerBody.userData.physicsBody.setLinearVelocity(
        new Ammo.btVector3(this.pos.x,this.pos.y,this.pos.z));
    } else if (this.moveBackward == true) {
      // WORKS PERFECT IN ANY DIRECTION
      this.pos.copy(this.raycaster.ray.direction);
      this.pos.multiplyScalar(12);
      this.playerBody.userData.physicsBody.setLinearVelocity(
        new Ammo.btVector3(-this.pos.x,this.pos.y,-this.pos.z));
    } else if (this.moveLeft == true) {
      // NOT WORKS PERFECT IN ANY DIRECTION !!!!
      this.pos.copy(this.raycaster.ray.direction);
      this.pos.multiplyScalar(12);
      this.playerBody.userData.physicsBody.setLinearVelocity(
        new Ammo.btVector3(this.pos.x, 0, this.pos.y));
     } else if (this.moveRight == true) {
      // NOT WORKS PERFECT IN ANY DIRECTION !!!!
      this.pos.copy(this.raycaster.ray.direction);
      this.pos.multiplyScalar(12);
      this.playerBody.userData.physicsBody.setLinearVelocity(
        new Ammo.btVector3(-this.pos.y,  0, -this.pos.x));
     }

I try to combine x,y,z also with -x,-y,-z in different variant but no success.

How can i modify this.raycaster.ray.direction for example angle for Y axion +90 or -90 and make work just like forwand and backward....

Any suggestion ?

1

There are 1 answers

0
Nikola Lukic On

i make clone of exist ray.direction vector then i apply new angle with applyAxisAngle. For left -90 for right 90 .

else if (this.moveLeft == true) {
      let fixedDirection1 = this.raycaster.ray.direction.clone();
      fixedDirection1.applyAxisAngle(
        new THREE.Vector3(0,1,0), MathUtils.degToRad(90))
      this.pos.copy(fixedDirection1);
      this.pos.multiplyScalar(this.config.playerController.movementSpeed.left);
      this.playerBody.userData.physicsBody.setLinearVelocity(
        new Ammo.btVector3(this.pos.x, 0, this.pos.z));
     } else if (this.moveRight == true) {
      let fixedDirection1 = this.raycaster.ray.direction.clone();
      fixedDirection1.applyAxisAngle(
        new THREE.Vector3(0,1,0), MathUtils.degToRad(-90))
      this.pos.copy(fixedDirection1);
      this.pos.multiplyScalar(this.config.playerController.movementSpeed.right);
      this.playerBody.userData.physicsBody.setLinearVelocity(
        new Ammo.btVector3(this.pos.x, 0, this.pos.z));
     }

Now i have player who walk in all directions.