Use cases for rigidbody.linearVelocity

Hey,

I’ve recently had great use for the ammo.js physics engine and the entity.rigidbody.linearVelocity variable to assess an entity’s velocity, however I noticed in some cases linearVelocity is still 0 even if the entity is moving (and has a rigidbody). Namely when the entity is being moved by entity.rigidbody.teleport and entity.setPosition().

Anyone know in which cases entity.rigidbody.linearVelocity actually reflect the movement of the entity? And is there a way to accurately set the position of an entity in such a way that linearVelocity can still be used?

Hi @Astra!

I assume you don’t use a dynamic rigidbody?

Otherwise you can use it in combination with methods like applyForce and applyImpulse.

Actually I am!

But I want to move the entity along a fixed path which is pretty difficult to do accurately with applyForce and applyImpulse. So I was hoping it is possible to use something else.

(My temporary solution is to manually calculate the velocity, but that has its own set of caveats)

You can keep it kinematic then. The valocity property on the kinematic entity is not calculated. You can manually get the velocity of it like this:

// current velocity = (current frame position - previous frame position) / dt;
velocity.copy(currentPosition).sub(lastPosition).mulScalar(1/dt);

Just make sure to set both currentPosition and lastPosition to the same value, when teleporting entity a large distance (e.g. on respawn). Otherwise it will create a large velocity value.

1 Like