btTransform vs custom transform representation

Post Reply
willcassella
Posts: 9
Joined: Sun Dec 01, 2013 6:53 am

btTransform vs custom transform representation

Post by willcassella »

Hi,

I've been pretty successful in hacking the bullet Hello World program into the game I'm developing, and I'm beginning to consider how I'll be integrating it into the rest of my engine. At the moment I'm using a custom math library, with all the standard types (Vec2/3/4, Mat4, Quat, etc). I've been successfully using my own Transform type as well so far, and I'm a bit reluctant to stop using it in favor of btTransform. To that end I see three options:

- Maintain separate collections of "btTransform" and "Transform" structs, and just synchronize them pre and post physics simulation. This may be a bit slow, but it allows me to continue using my own API (also abstracts away the implementation of physics, though I'm not convinced in the value of that yet).
- Have my own Transform type be just a wrapper around a 'btTransform' instance. This would allow me to expose the same API, using the Bullet implementation underneath.
- Drop my entire math library in favor of Bullet. This would certainly prove to be more performant, since I won't be copying data around unnecessarily and Bullet's math code is way faster than mine anyway.

However, before I decide on the solution I'd like to know what you guys think, and what you've done in the past. While it seems that using pure Bullet math would be the best solution for integration and performance, I'm still very new and I'd like to continue using my own API if possible as long as it's not too much of a hassle or decreases performance too much.

Thanks
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: btTransform vs custom transform representation

Post by xexuxjy »

What you probably want to do is create a subclass of btMotionState - it's main aim is to allow you to synchronize between your existing code and bullets internals.
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: btTransform vs custom transform representation

Post by drleviathan »

I think all of your proposed solutions are fine. Wrapping btTransform with your own Transform class could be a partial step toward just adopting Bullet's math library everywhere if you eventually decide that is what you want to do. Abstracting the physics libraries out completely is nice if you think maybe you'll ever want to switch to an entirely different physics engine, however doing this can be a continual process -- unless you're very careful you'll find that Bullet's API will leak into your abstraction anyway -- which will mean you'll still have to change the API of your abstraction a little bit when you switch.
willcassella
Posts: 9
Joined: Sun Dec 01, 2013 6:53 am

Re: btTransform vs custom transform representation

Post by willcassella »

Thanks for the feedback!

Looking through the API, I think I'll be extending btMotionState, since that seems to be the easiest and cleanest way to implement things while also allowing me to keep using my own API.

After some initial testing with the debugger, I noticed that "getWorldTransform" was only being called from my own code, and not from Bullet. This will be an issue, since transformation updates from gameplay code need to be communicated with Bullet. Is there something I'm missing, or is this just how things work?

Thanks
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: btTransform vs custom transform representation

Post by drleviathan »

Bullet will call btMotionState::getWorldTransform() for two conditions:

(1) When the btRigidBody is first created if the btMotionState was passed to the constructor.

(2) Once every substep if the object is KINEMATIC.

Note that for DYNAMIC objects Bullet calls btMotionState::setWorldTransform() once per substep... if they are active.
willcassella
Posts: 9
Joined: Sun Dec 01, 2013 6:53 am

Re: btTransform vs custom transform representation

Post by willcassella »

Is it possible to have objects who's position/rotation may be set arbitrarily by the application, as well as participating in the physics simulation? If 'btMotionState' had some sort of way to mark it as dirty that Bullet could detect, that would be perfect
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: btTransform vs custom transform representation

Post by drleviathan »

No, btMotionState is a pure virtual base class. You must derive your own MotionState and override the setWorldTransform() and getWorldTransform() methods to do what you want. The only guarantee is that the Bullet simulation will call those methods at the appropriate times. In short, you must implement your own dirty flags management logic.

It is ok to call the getWorldTransform() method in your own game engine outside of the simulation substeps to manually teleport the btRigidBody to a new location... assuming that your simulation is single-threaded and that you've implemented that method to do the right thing.

One little detail about how Bullet calls btMotionState::setWorldTransform() is that it will do extrapolation beyond the last fixed substep. So if your physics simulation was at t = N*substep (an exact number of substeps) and you tell it to advance some deltaTime = 1.5*substep, then Bullet will make only one substep and remember the half substep for later (to be added onto the next deltaTime). When it calls setWorldTransform() on dynamic objects it will extrapolate their transforms forward by half a substep. The reason it does this is to help smooth out the object motion when the physics simulation steps at a different rate than the render pipeline.
Post Reply