Move in direction of object rotation

[https://playcanv.as/p/KMNqaQyd/]

I made an car with physics and move it with arrow keys,
up-forward, down-backwards,
Left rotatate to the left, Right rotate to the right
I managed it that the car is moving in the direction of his rotation but there is a mistake at some angles.

var winkelRad =   this.entity.getLocalEulerAngles().y * pc.math.DEG_TO_RAD;
var alphaSin = Math.sin(winkelRad);
var alphaCos = Math.cos( winkelRad);
this.xSpeed = alphaSin * this.speed;
this.zSpeed = alphaCos * this.speed; 

 if(this.app.keyboard.isPressed(pc.KEY_UP)){         
          this.entity.rigidbody.applyImpulse(this.xSpeed, 0, this.zSpeed);
}


  if(this.app.keyboard.isPressed(pc.KEY_DOWN)){         
          this.entity.rigidbody.applyImpulse(-this.xSpeed, 0, -this.zSpeed);
}   
   if(this.app.keyboard.isPressed(pc.KEY_RIGHT)){  
//this.entity.rotateLocal(0,1, 0); 
     this.entity.rigidbody.applyTorque(0,5, 0);

}

   if(this.app.keyboard.isPressed(pc.KEY_LEFT)){  
 this.entity.rigidbody.applyTorque(0, -5, 0);

   }

Rather than calculating the impulse to apply yourself. You can get the forward axis of the entity and apply impulse in that direction.

Quick code

this.impulse = new pc.Vec3();
this.impulse.copy(this.entitiy.forward).scale(this.speed);
this.entity.rigidbody.applyImpulse(this.impulse);

You should be able to get the idea from this.

Use local euler didn’t work as it is possible to represent a rotation in multiple ways. Some of them are not usable in the you wanted them to be here.

Thanks a lot that is much easier. :smile:

thanks, its working, there was just a little mistake “entity”

this.impulse = new pc.Vec3();
this.impulse.copy(this.entity.forward).scale(this.speed);
this.entity.rigidbody.applyImpulse(this.impulse);