Articulated btMultiBody character with inverse dynamics

DivideByZero
Posts: 4
Joined: Sun Nov 29, 2015 3:48 am

Articulated btMultiBody character with inverse dynamics

Post by DivideByZero »

The master branch has some awesome inverse dynamics code. It seems to work great with a fixed base but doesn't seem to run with a floating base. I'm using the btMultiBodyTreeCreator and it says it created a valid tree. I'm sure it'll be fixed in the released version but it would be really cool if I could test it earlier.

I have a bone animated character. I first tried using the btMultiBodyJointMotor but the animation was too bouncy. It works much better with inverse dynamics but the animation seems to lag behind e.g if I move a bone continually between 0 and 90 degrees, then it animates closer to 10 and 80 degrees. I tried increasing kp to get it to move faster but then I need to increase kd to remove the oscillation which creates lag. I'm currently only setting the target angles like in InverseDynamicsExample. Is there some trick I can use to get the character movement closer to the source animation?
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Articulated btMultiBody character with inverse dynamics

Post by Erwin Coumans »

You might need to give it some time to reach the target, is that not an option?
This lag and floating base issue will more likely get addressed when you file an issue in the issue tracker and create/modify an example for the latest Bullet Example Browser.

https://github.com/bulletphysics/bullet3/issues

Do you mind helping out with that?
Cheers!
Erwin
DivideByZero
Posts: 4
Joined: Sun Nov 29, 2015 3:48 am

Re: Articulated btMultiBody character with inverse dynamics

Post by DivideByZero »

That’s cool, I don’t mind waiting for the next release to try inverse dynamics. I created a new issue for it. I might try making a stable PD control to drive the torque for now.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Articulated btMultiBody character with inverse dynamics

Post by Erwin Coumans »

DivideByZero wrote:That’s cool, I don’t mind waiting for the next release to try inverse dynamics. I created a new issue for it. I might try making a stable PD control to drive the torque for now.
The floating base works fine, I modified the example:
https://github.com/bulletphysics/bullet ... 34a5dcf855

Note that the inverse dynamics computes forces/torques, so it assumes a trajectory that is smooth on the acceleration and velocity level. If you require very abrupt changes in position (and velocity) it is better to use constraints, such as the btMultiBodyJointMotor.

You mentioned that the "the animation was too bouncy". It may be possible to improve the behavor if you can provide a small reproduction example in the Example Browser. Just file a new issue with a modified example.

Oh, and if you make progress with 'stable PD control', please consider contributing results/implementation.
DivideByZero
Posts: 4
Joined: Sun Nov 29, 2015 3:48 am

Re: Articulated btMultiBody character with inverse dynamics

Post by DivideByZero »

Cool, thanks for the example. Sorry, looks like it was a bug in my code. I'm using bullet with unreal and hadn't called b3SetCustomErrorMessageFunc. I was using multiBody->getNumDofs() and forgot to add the extra 6 dof when calling calculateInverseDynamics() which returned -1 and didn't apply any torque.

The floating base, inverse dynamics character now works but as you mentioned it didn’t react well with abrupt changes. I went back to using the motors and its much more stable after I tried tweaking some of the settings (mainly max applied impulse). If I have any luck with the stable PD control I’ll post the results.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Articulated btMultiBody character with inverse dynamics

Post by Erwin Coumans »

DivideByZero wrote:I'm using bullet with unreal and hadn't called b3SetCustomErrorMessageFunc.
Interesting. Did you integrate Bullet into UE4 yourself and is it available somewhere?
DivideByZero
Posts: 4
Joined: Sun Nov 29, 2015 3:48 am

Re: Articulated btMultiBody character with inverse dynamics

Post by DivideByZero »

I’m still new to bullet and UE4. I was mainly just trying to get something running so the code I’ve used to integrate bullet is kind of ugly. Ideally, someone could create a proper plugin like how physx is set up. I’ve included my integration code. It could be a useful starting point for someone but I wouldn’t recommend using it as is.

To use:
Add a ABTWorldActor. Set gravity (cm), enable debug draw, unit scales (UE4 in cm, bullet in meters).

Adding basic shapes: Create a shape e.g cube. Add a BTRigidBody component and a shape component e.g BTBoxShape. If the mass is zero it’s static. Make sure the mesh transform is set to movable (not static).

Including the static bullet lib worked fine for normal shapes. I had weird errors when I tried using featherstone. I included all the source code to try debugging it but it just worked when it was directly added. I included Bullet3Common, BulletCollision, BulletDynamics, BulletInverseDynamics, Extras/InverseDynamics and LinearMath. Any included cpp files required the project include to compile e.g BulletMultiBody project => #include "BulletMultiBody.h". Some things didn’t compile and needed small changes (double classes, alignment). I can’t remember what I had to change. I’ve include the bullet source but the #include "BulletMultiBody.h" needs to change to match the project. The bullet code is also an odd mix of the release version and master branch, so it might not be very stable.

I won’t upload the project because the file size is large (4 gb for a nearly empty zipped project?). I think a lot of the classes included aren't used.

To created a character drag an instance of BTMultiBodySkeleton to the stage. For this example I’ll use the Mannequin. Add a PoseableMesh component and assign the Mannequin skeleton. Then add a tree structure for the bones using BTMultiBodyBoneComponent. Also add one shape component per bone. The shapes can be offset in the transforms (bone space), haven’t tested rotation offsets.
example:
BTMultiBodySkeleton
+PoseableMesh
++pelvis
+++thigh_l
++++calf_l
++++calf_l_shape. Capsule. radiu 6, height 40, mass 1. location -20,0,0
+++thigh_l_shape. Capsule. radius 8, height 30, mass 1. location -20,0,0
++pelvis_Shape. Box, size 10,30,12, mass 1

Each bone has extra settings:
Axis order should always be zyx, not xyz. Axis can be removed if not required e.g knee => z only.
BulletServo: desired servo angle can be used to test each servo. The animation mapper will set this value to match the animation.
InvertAxis[X,Y,Z]. Sometimes the animation was flipped. For the mannequin I think I always set InvertAxisX and InvertAxisY to true and InvertAxisZ to false.

Select BTMultiBodySkeleton and check floating then run the game and the character will ragdoll to the ground (no constraints). Debug mode should be enabled (mesh won’t follow without BTMultiBodyAnimationMap).

Uncheck floating, check enable motors. Run the game and it should remain in the t pose. If not try increasing “Max motor impulse” and try the PID KP setting (don’t use PID KI, ok to ignore PID KD). Select a bone component and try setting “desired servo angle”.

To match an animation add a skeletal mesh component (move it to the side) and a BTMultiBodyAnimationMap. You can try switching the BTMultiBodyAnimationMap “Animate bone alignment” between physics and the source animation. Select BTMultiBodySkeleton and uncheck floating. The character should have enough torque to interact with the ground.

To test inverse dynamics select BTMultiBodySkeleton and uncheck enable motors, then check “Enable apply inverse dynamics” and set ID KP and ID KD. With a fixed base it should be able to recover from impacts. It only works with a floating base in a t-pose or when manually controlling each motor. It might require non-zero values on calculate inverse dynamics for the 6 dummy values.

Because I'm mapping the animation using Euler rotations it can get into odd states. The left side of the Mannequin was OK, the right side had issues. This is also for UE 4.9.2 with visual studio 2013. I had upgrade errors when I tried it on 4.10.0 with visual studio 2015.
You do not have the required permissions to view the files attached to this post.
darkZ
Posts: 2
Joined: Wed Feb 03, 2016 6:21 am

Re: Articulated btMultiBody character with inverse dynamics

Post by darkZ »

Hey DividebyZero,

Thanks a lot for the integration code. I will give it a go. I am the author of the full body IK plugin for UE4 (https://forums.unrealengine.com/showthr ... y-IK-Setup). I have also done some work in physics-based character simulation. I am starting to implement a basic physics-based controller in UE4 when I stumbled upon this thread. Is there a good way to keep in touch to share progress if you would like ? Not too many people out there using physics-based multi-bodied character in UE4 !

Best,
darkZ
darkZ
Posts: 2
Joined: Wed Feb 03, 2016 6:21 am

Re: Articulated btMultiBody character with inverse dynamics

Post by darkZ »

I have now managed to take a fresh copy of 2.83.7 and built as a plugin in UE4. I still need to write my own classes for using the various classes from Bullet as components in UE4, but I think this is good progress that I can now compile bullet to a dll or lib compatible to be loaded as a plugin in UE4. This will allow a lot of people to start playing with it withing UE4.

After having written some components, I will myself work with some inverse dynamics problems and will report back on my findings if they are relevant.

Best,
darkZ