Moving An Object Along It's Forward Axis

Hi all,

I am currently developing a NASCAR driving simulator game, and I can’t get the cars to move in the direction that they are facing. I am using physics and this.entity.rigidbody.applyForce and this.entity.rigidbody.applyTorque. After applying torque, and changing direction of the object, the object still continues to increment only it’s Z axis coordinates, and moves in a different direction. Please help!

Rgds,

Are you applying force along the entity’s forward vector? Can you show the code/post a link to the project please?

if (this.app.keyboard.isPressed(pc.KEY_UP){
this.entity.rigidbody.applyForce(0, 0, 7);

Sorry, didnt copy the closing }bracket

You are applying force along the world Z axis as apply force is applied in world space. You would want to apply force along the entity’s forward axis.

// Get the forward direction of the entity
var forwardForce = this.entity.forward.clone();

// Scale the direction by the amount of force you want to apply
forwardForce.scale(7);

// Apply the force
this.entity.rigidbody.applyForce(forwardForce);
2 Likes

Thanks so much, I’ll try this

Does the same thing work for backward?

Scale the vector by a negative value to go backwards.

1 Like

OK, thanks a lot @BenBean303 and @yaustar!