Getting body velocity in local space

Murphy
Posts: 32
Joined: Fri Aug 31, 2007 6:36 am

Getting body velocity in local space

Post by Murphy »

I know this is probably very simple but I cannot figure it out. Is there a way to get the velocity of a body in it's local space. For example, if the body is moving forward I would expect the z component of the velocity to be +10 perhaps. If it is moving backwards I would expect -10 for the z velocity.

Currently I am trying this:

Code: Select all

getRigidBody()->getVelocityInLocalPoint(getRigidBody()->getCenterOfMassPosition()).z()
or

Code: Select all

getRigidBody()->getVelocityInLocalPoint(btVector3(0, 0, 0)).z()
and I am not getting correct results. It seems like it is perhaps returning the velocity in world coordinates...

Any ideas?
chunky
Posts: 145
Joined: Tue Oct 30, 2007 9:23 pm

Re: Getting body velocity in local space

Post by chunky »

I'm not entirely sure I understand the question, but...

I think what you want to do is get the rotation of the body, and the velocity of the body.
Normalise the velocity [so that it's just a direction], subtract one from the other.
Multiply the new vector by the speed [length of the original velocity vector]

Does that make sense?

Gary (-;
Nathanael
Posts: 78
Joined: Mon Nov 13, 2006 1:44 am

Re: Getting body velocity in local space

Post by Nathanael »

The following should work,

Code: Select all

btVector3 getVelocityAtWorldPosition(btRigidBody* body,const btVector3& worldposition,bool local)
{
/* That's the 'localpoint' */ 
const btVector3 relpos=worldposition-body->getCenterOfMassPosition();
/* That's velocity in world space */ 
const btVector3 wvel=body->getVelocityInLocalPoint(relpos);
/* Now you can transform it in body local frame */ 
if(local) return(body->getWorldTransform().getBasis().transpose()*wvel);
/* Or keep it in world frame */ 
return(wvel);
}
Hope it help.
Murphy
Posts: 32
Joined: Fri Aug 31, 2007 6:36 am

Re: Getting body velocity in local space

Post by Murphy »

Thanks guys. I tried both of your methods.

It does seem like Nathanael's method is closer to what I need. It does work the way I expect however something funny is happening...

I am using this with a raycast vehicle and it works great if I am going forward or backward only. However, whenever I turn right or left in the vehicle the forward velocity changes in unexpected ways. If I turn left it might make the z component of the local velocity very high or very low. Same if I turn right.

It seems like the x component is somehow affecting to the z component.
Nathanael
Posts: 78
Joined: Mon Nov 13, 2006 1:44 am

Re: Getting body velocity in local space

Post by Nathanael »

The function i gave you include angular velocity, call it with body->getCenterOfMassPosition() as worldposition if you only want linear velocity, or:

Code: Select all

static btVector3 getLinearVelocityInBodyFrame(btRigidBody* body)
{
return(	body->getWorldTransform().getBasis().transpose() *
		body->getLinearVelocity());
}
Is faster, but less general.
Murphy
Posts: 32
Joined: Fri Aug 31, 2007 6:36 am

Re: Getting body velocity in local space

Post by Murphy »

Exactly what I needed!

Thanks Nathanael!