How to get the velocity of rigid body

Post Reply
water
Posts: 27
Joined: Fri Jun 05, 2020 8:36 am

How to get the velocity of rigid body

Post by water » Thu Jul 09, 2020 1:43 am

I want to get the velocity of a rigid body in motion. How can I get it? Thank you very much.

User avatar
drleviathan
Posts: 635
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: How to get the velocity of rigid body

Post by drleviathan » Thu Jul 09, 2020 3:15 am


water
Posts: 27
Joined: Fri Jun 05, 2020 8:36 am

Re: How to get the velocity of rigid body

Post by water » Mon Jul 13, 2020 6:43 am

Thank you very much, but the return value I get is btVector3, how can I convert it to speed?

User avatar
drleviathan
Posts: 635
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: How to get the velocity of rigid body

Post by drleviathan » Mon Jul 13, 2020 2:25 pm

Speed is the magnitude of velocity --> compute the length of the btVector3.

water
Posts: 27
Joined: Fri Jun 05, 2020 8:36 am

Re: How to get the velocity of rigid body

Post by water » Tue Jul 28, 2020 3:38 am

This seems to be the speed of each frame. What should I do if I want to get the speed per second? Thank you for your time

User avatar
drleviathan
Posts: 635
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: How to get the velocity of rigid body

Post by drleviathan » Tue Jul 28, 2020 2:56 pm

speed = deltaDistance / deltaTime = meters /second

acceleration = deltaSpeed / deltaTime = meters / second^2

There is no RigidBody::getLinearAcceleration() method in the API.

There is a RigidBody::getGravity() method which would provide the free-fall acceleration sans collisions.

To measure the true effective acceleration of the object you must do it manually, which would require you to remember the lastTime and lastVelocity from the previous measurement. In pseudo code it might look something like:

Code: Select all

thisTime = getTimeNow()
thisVelocity = getLinearVelocity()
deltaVelocity = thisVelocity - lastVelocity
deltaTime = thisTime - lastTime
acceleration = deltaVelocity / deltaTime
lastTime = thisTime
lastVelocity = thisVelocity

Post Reply