How can I rotate an object's velocity?

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

How can I rotate an object's velocity?

Post by joehot200 »

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...
Image
the ship would keep that velocity, even when I rotated my ship.
Image

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

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)?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

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

Post by drleviathan »

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;
btScalar adjustmentFactor = btMin(1.0, timeStep / adjustmentTimescale);
// 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?

Post by joehot200 »

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;
btScalar adjustmentFactor = btMin(1.0, timeStep / adjustmentTimescale);
// 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.
I thought that using setLinearVelocity() removes velocities from collisions? Or am I wrong?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

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

Post by drleviathan »

I believe it is safe to change the velocity of an object directly, despite its collisions. Within reason, of course -- setting the velocity very high or pushing the object deeper into penetration can only cause problems.
Post Reply