## How can I rotate an object's velocity?

joehot200
Posts: 15
Joined: Wed Apr 23, 2014 9:47 am

### How can I rotate an object's velocity?

I would have put a more descriptive title, but the title space is a bit small.

I am developing a ship fighting game, and I would like to move my ship (a RigidBody) around. This works - However, when my ship changes rotation, the RigidBody simply keeps the velocity.

If this, say, is the original velocity, then...

the ship would keep that velocity, even when I rotated my ship.

I would like the ship to have the same velocity, but in a new direction. Like this:

How can I do that, without directly setting the velocity (setting the velocity overwrites the collision, and the ship will be rotating almost all the time)?
drleviathan
Posts: 839
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

### Re: How can I rotate an object's velocity?

Directly setting the velocity IS how you change it. I don't see how that "overwrites the collision" as you put it.

If you want to constantly reorient the velocity according to the direction of the ship I would suggest implementing an Action that does just that and then give the action to the btDiscreteDynamicsWorld (which I assume you're using). Research btActionInterface and how it is used (Huh, I notice there is no simple Action Demo available.)

It is possible to gradually reorient the velocity to match the target velocity rather than setting it suddenly. Perhaps this wouldn't "overwrite the collision" so bad (whatever that means). Here is some example code for how to do it:

Code: Select all

``````const btScalar adjustmentTimescale = 0.5; // seconds for velocity to get within 1/e of its targetVelocity
btVector3 velocityAdjustment = velocityWeWant - currentVelocity;
// blend current and target velocities
btVector3 newVelocity = btScalar(1.0 - adjustmentFactor) * currentVelocity + adjustmentFactor * targetVelocity;
body->setLinearVelocity(newVelocity);``````
The nice thing is that you can tweak the strength of your blending by adjusting the adjustmentTimescale. Shorter timescales make the transition faster. The btMin is there for stability in case you accidentally provide too small of a timescale, or too large of a timeStep.
joehot200
Posts: 15
Joined: Wed Apr 23, 2014 9:47 am

### Re: How can I rotate an object's velocity?

drleviathan wrote:Directly setting the velocity IS how you change it. I don't see how that "overwrites the collision" as you put it.

If you want to constantly reorient the velocity according to the direction of the ship I would suggest implementing an Action that does just that and then give the action to the btDiscreteDynamicsWorld (which I assume you're using). Research btActionInterface and how it is used (Huh, I notice there is no simple Action Demo available.)

It is possible to gradually reorient the velocity to match the target velocity rather than setting it suddenly. Perhaps this wouldn't "overwrite the collision" so bad (whatever that means). Here is some example code for how to do it:

Code: Select all

``````const btScalar adjustmentTimescale = 0.5; // seconds for velocity to get within 1/e of its targetVelocity
btVector3 velocityAdjustment = velocityWeWant - currentVelocity;