Augmenting btRigidBody: Composition or Subtyping?

B_old
Posts: 79
Joined: Tue Sep 26, 2006 1:10 pm

Augmenting btRigidBody: Composition or Subtyping?

Post by B_old »

Hi,

I want to augment btRigidBody with some methods that might improve the interface to my framework. Currently I store some additional info in the userpointer of btRigidBody, but I'm considering to subtype btRigidBody instead.
Could it have negative impact on the overall performance of bullet, because the object gets bigger by the stuff I add?
I'm curious what people here usually do and if I should be aware of something.
Thanks!
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Augmenting btRigidBody: Composition or Subtyping?

Post by Flix »

Tried both ways, without any problem.
(If you use a static method to perform upcasts in your class, you can just change the code inside it so that you can compare the two approaches easily.)
Could it have negative impact on the overall performance of bullet, because the object gets bigger by the stuff I add?
Well, the library handles aligned pointers to the class, so performance isn't a problem (after all the btRigidBody class inherits from btCollisionObject itself).
Memory usage will increase for sure if you use a lot of bodies...

Inheritance is better for lazy people like me that want to write less code on the "client" side, but it can be a bit more difficult to implement in some cases, where you want to process some data before creating the btRigidBody instance, and you have to do it in the initialization list of the ctr of your derived class... (to prevent this problem, some libraries design their classes so that thay have a protected "void" ctr and can be built with protected methods: but you can always use static methods to create the default arguments your base class needs).

Hope it helps.
B_old
Posts: 79
Joined: Tue Sep 26, 2006 1:10 pm

Re: Augmenting btRigidBody: Composition or Subtyping?

Post by B_old »

Thanks for the reply!
I agree with you, that the inheritance way is convenient in a lot of cases.
Flix wrote: Well, the library handles aligned pointers to the class, so performance isn't a problem (after all the btRigidBody class inherits from btCollisionObject itself).
Memory usage will increase for sure if you use a lot of bodies...
Memory usage should be pretty similar, right? In one case I store stuff in the subclassed rigidbody. In the other case I store stuff another class that gets pointed to by the user pointer. But the stuff is still there, somewhere in memory. Thats why I was only worried about performance. But maybe I'm missing something here.
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Augmenting btRigidBody: Composition or Subtyping?

Post by Flix »

B_old wrote:But maybe I'm missing something here.
No, you're right; I was thinking that with composition you could possibly pass the some pointer to different btRigidBodies, but this (if possible) is probably useless anyway.
B_old wrote:the inheritance way is convenient in a lot of cases
Yes, but with composition you can probably "bind and unbind" your objects after creation too ( I remember I needed to do it sometimes when using composition in general, not in the btRigidBody case).
B_old wrote:I was only worried about performance.
I just don't think that a performance loss happens but, to tell the truth, I've never run time test to see the difference: so your warries might be right.
B_old
Posts: 79
Joined: Tue Sep 26, 2006 1:10 pm

Re: Augmenting btRigidBody: Composition or Subtyping?

Post by B_old »

Thanks for your comments!