Drive a Rigidbody to a rigid body from another simulation

pitsikoko
Posts: 11
Joined: Tue Jul 02, 2013 3:46 pm

Drive a Rigidbody to a rigid body from another simulation

Post by pitsikoko »

Hi all,

I'm using bullet for a vehicle simulation framework and i'm now trying to decouple bullet from my simulation framework. The reason is that I would like now to run my simulation at a higher frequency than the bullet world update step size.

my loop :

- retrieve bullet rigidbody transform, linear and angular vel
- inject it into my own rigidbody
- update my simulation by stepSize with a number of sub steps
- retrieve my simulation rigidbody transform, linear and angular velocity
- drive the bullet rigidbody toward my simulation rigidbody transform.

Where i'm having difficulties is for the last step.

At the moment i'm just setting the linear velocity of the bullet rigidbody directly. Which is fairly easy, I just look at what velocity I need in order to drive the bullet rigidbody to the expected transform in one step.
How do I calculate that for angular velocity ? I have the rotation as a matrix3 or quaternion to go from the current transform to the target transform but i don't know how to calculate the angular velocity out of that.

Another question i would like to ask is, what would be a better method to drive my bullet rigidbody to my simulation rigidbody ?

Obviously I need to drive it since I can then any collision response that happened in the bullet world and feed back my simulation.

Thank you
STTrife
Posts: 109
Joined: Tue May 01, 2012 10:42 am

Re: Drive a Rigidbody to a rigid body from another simulatio

Post by STTrife »

I probably misunderstand, but you say "The reason is that I would like now to run my simulation at a higher frequency than the bullet world update step size." Why not just decrease the step size in bullet? It's the third parameter in stepSimulation (fixedTimeStep).
pitsikoko
Posts: 11
Joined: Tue Jul 02, 2013 3:46 pm

Re: Drive a Rigidbody to a rigid body from another simulatio

Post by pitsikoko »

Because then collision detection is performed at the higher frequency introducing an unnecessary cost. A collision detection and response running at 60 Hz is good enough but for simulation of complex systems you may want to use a higher frequency (great with springs per instance). Plus you may use a different integrator with your own simulation, or even have a simulation based on equations of motions and want to feed the movement back into the game physics.

It's a common practice in professional games to have a simulation running in parallel with the game worlds physics. As good as bullet is (and it is excellent), it is also not "precise". Just in the way that it is targeted at building games, not simulations.

Hope this clarifies my approach.
STTrife
Posts: 109
Joined: Tue May 01, 2012 10:42 am

Re: Drive a Rigidbody to a rigid body from another simulatio

Post by STTrife »

yes it does :) unfortunately I don't know the answer to your original question, sorry :P
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: Drive a Rigidbody to a rigid body from another simulatio

Post by Basroil »

Why not just skip BulletDynamics altogether and just use the parts you need when you need them (like collision detection). Didn't realize a physics engine with updating and integration wasn't a "full" simulation though, I've been using it along with ODE for just that!

For a simple simulation (like the dynamic control demo with custom PID at joints), you can easily get 1000Hz realtime simulation on an i5 processor in bullet though. Are you making the world that much more complex or simulation time so small that you need to increase overhead elsewhere to reduce collision detection overhead?
pitsikoko
Posts: 11
Joined: Tue Jul 02, 2013 3:46 pm

Re: Drive a Rigidbody to a rigid body from another simulatio

Post by pitsikoko »

Well, no that's not the idea...
Many other things happen in the world which have nothing to do with my vehicle sim, and they are happily running at 60 Hz.

So, I want to decouple things, now the question is how to drive a rigid body to another rigid body using bullet ? 6dof ? pid ?

Thanks.
pitsikoko
Posts: 11
Joined: Tue Jul 02, 2013 3:46 pm

Re: Drive a Rigidbody to a rigid body from another simulatio

Post by pitsikoko »

hi all

Nobody can tell what are options to drive a rigid body to a given transform ?
Surely this is the most basic thing that everybody does when we drive a ragdoll to a given pose to follow an animation.

best
Neirdan
Posts: 13
Joined: Thu Jun 13, 2013 8:07 pm

Re: Drive a Rigidbody to a rigid body from another simulatio

Post by Neirdan »

Why don't you use the kinematic character controller stepForwardAndStrafe() function?
STTrife
Posts: 109
Joined: Tue May 01, 2012 10:42 am

Re: Drive a Rigidbody to a rigid body from another simulatio

Post by STTrife »

I've only seem post about how you shouldn't update rigidbodies manually. I'm sure there is a way, but you probably also have to look into collision cache etc. At least bullet doesn't seem to be designed to update rigidBodies manually. But it would be nice if someone could explain what steps you need to make to update the position of a rigidbody without problems.
One way would be to remove the old one and replace it with a new one... but that seems kinda silly. might be a temporary solution though
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: Drive a Rigidbody to a rigid body from another simulatio

Post by Basroil »

Question is, do you actually need it to use less resources or are you just making things more complicated for yourself? If you don't have performance issues, no sense in working yourself up over it just yet. If you do, then perhaps there's other ways to go about it, like filtering out collisions other than the ones associated with your vehicle for half the frames (things will move just as far in two frames as one given the same initial velocity and time step... well, other than a bit of acceleration due to gravity, but that shouldn't be too bad unless your steps are large or gravity is strong) or even just using simpler geometry (regular box on box is much faster than convex on convex).
pitsikoko
Posts: 11
Joined: Tue Jul 02, 2013 3:46 pm

Re: Drive a Rigidbody to a rigid body from another simulatio

Post by pitsikoko »

STTrife,

Yes this is the case with all realtime engines, and it makes sense. However you manipulate them with forces. Applying the velocity is just the same as setting forces as far as I understand.

Well, it seems i haven't been explaining myself properly since i keep getting answers which are off.
There is a lot of instances where you have a simulation running, and you want to use the result to control a rigid body. Per instance a flight simulation framework. So the question is simple : what are approches to apply forces/changes of velocity to a rigid body in order to drive it around in the world ?
STTrife
Posts: 109
Joined: Tue May 01, 2012 10:42 am

Re: Drive a Rigidbody to a rigid body from another simulatio

Post by STTrife »

I think you could do the same as you do with linear velocity. Calculate the rotation needed from the current rotation to the target rotation, and apply the angular forces needed to get it there in a single timestep.
Not sure how to do that exactly, but it doesn't seem very bullet specific.
I guess... if q2 is the target rotation, and q1 is the current rotation (both quaternions), then you can do q2 * q1' (inverted q1, swap the w-component) to get the rotation between q1 and q2. Then try to apply that as a angular rotation I think... not sure how exactly. maybe convert it to euler, then multiply by 1/steptime (1 / 1/60 = 60 with standard timestep). I'm not very sure about these steps, specially the last one. someone else might know better?
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: Drive a Rigidbody to a rigid body from another simulatio

Post by Basroil »

pitsikoko wrote:Yes this is the case with all realtime engines, and it makes sense. However you manipulate them with forces. Applying the velocity is just the same as setting forces as far as I understand.
Only if contact isn't involved. If it is, applying a velocity can produce motion where setting an equivalent force wouldn't. Are your other objects two-way intractable with the "fast" one? If you are trying to pull a physx style particle system, where scene objects collide with themselves and your character, but the character does not collide with the objects, then the solution was already stated above.
pitsikoko
Posts: 11
Joined: Tue Jul 02, 2013 3:46 pm

Re: Drive a Rigidbody to a rigid body from another simulatio

Post by pitsikoko »

Right I need to apply a force and torque.

For the force i don't have any problem, but the rotation is difficult.
Here is what i have to far :

Code: Select all

  
            btQuaternion q0 = m_RigidBody->getOrientation();
            btQuaternion q1 = mTargetTransform.getOrientation();

            btQuaternion q = q1 * q0.inverse();

            // shortest arc
            q = QuatFromAToB( q0, q1 );
            
            btVector3 rotAxis = q.getAxis();
            btScalar rotAngle = q.getAngle();

            btVector3 newAngularVel = rotAxis * (rotAngle / dt);   

            m_RigidBody->applyTorque( (m_RigidBody->getInvInertiaTensorWorld().inverse() * newAngularVel - m_RigidBody->getInvInertiaTensorWorld().inverse() * m_RigidBody->getAngularVelocity()) / dt );
Obviously this is wrong, because I need to make sure that i'm applying a torque that is going to rotate the body in the correct direction. If I can get this first step working then I can think about using a PID so i'm also trying to match the target velocities.

Any help to go further ?
pitsikoko
Posts: 11
Joined: Tue Jul 02, 2013 3:46 pm

Re: Drive a Rigidbody to a rigid body from another simulatio

Post by pitsikoko »

Well i found I can just blend between the torque needed to transform from A to B in one step, and the delta of velocities between the 2 objects. If the bullet rigid body collide i just feed back the collision response to my system (transform + velocities). Since the only external forces I apply to the bullet rigid body are the ones to drive it to my own rigid body, it seems it is always able to reach the new transform in one step. I suppose the exact same logic could be used to drive ragdoll using animations. I used havok in the past to do that but they have a whole set of high level built in utilities.

For those that are interested, have a look at http://graphics.cs.williams.edu/papers/DynamoVGS06/.

I tried using a 6dof joint for the same purpose. It works but it prevents the proper response to happen.