I am developing an electromechanical simulation application using C# and BulletSharp.
I have a class "MySimClass" which has, amongst other things, a RigidBody property and a CollisionShape property like this:
MySimObject.RigidBody (this is a BulletSharp RigidBody)
MySimObject.CollisionShape (this is a Bulletsharp CollisionShape)
After a DynamicsWorld.StepSimulation() I will iterate through the contact manifolds to get the ManifoldPoints, and from the ManifoldPoints I get things like AppliedImpulse, LocalPoint..., PositionWorldOn... etc. These values will then be used by the MySimObjects to which the colliding RigidBodies belong. From what I understand, the only way I can get a reference from a RigidBody to the MySimObject to which the RigidBody belongs is by putting a referens to the MySimClass into the UserObject of the RigidBody, like this:
MySimObject.RigidBody.UserObject = MySimObject;
and to get the reference from the RigidBody I do a cast like this:
mySimObject = (MySimClass)RigidBody.UserObject;
If this is the way to do it, I would think the casting affects the performance in a negative way. Is this performance loss not significant enough to be bothered about? If it is, is there a better way to get a reference from a RigidBody to the "owning" MySimObject?
If this is the way and the performance gets noticeably affected, I would like to suggest "typed" versions of the classes in BulletSharp containing UserObject members, so there would be for example a RigidBody<T> class and a CollisionObject<T> class.
Bullet and BulletSharp are indeed impressive projects, and I'm very glad to see the steady development.
Regards
///JmD
BulletSharp object referencing
-
- Posts: 72
- Joined: Wed Feb 24, 2010 9:49 pm
Re: BulletSharp object referencing
All the cast really does is that it checks that UserObject is of type MySimClass and then copies the reference, so I doubt that this will be a bottleneck.
A performance hit might occur if the types on both sides were not the same and type conversion had to be done. For example, if MySimClass had a base class MySimBase and we tried to cast the same object like this:
MySimBase mySimObject = (MySimBase)RigidBody.UserObject;
then it would have to climb the inheritance tree to check that the cast was valid.
Generics would get rid of the type check, but it's a small gain and would be confusing to those that don't use the UserObject.
Storing the reference in UserObject should be fine. Another way I can think of would be to use a Dictionary of <RigidBody, MySimClass> pairs.
I'd recommend running a profiler such as SlimTune (http://code.google.com/p/slimtune/) to see where most of the time is spent.
A performance hit might occur if the types on both sides were not the same and type conversion had to be done. For example, if MySimClass had a base class MySimBase and we tried to cast the same object like this:
MySimBase mySimObject = (MySimBase)RigidBody.UserObject;
then it would have to climb the inheritance tree to check that the cast was valid.
Generics would get rid of the type check, but it's a small gain and would be confusing to those that don't use the UserObject.
Storing the reference in UserObject should be fine. Another way I can think of would be to use a Dictionary of <RigidBody, MySimClass> pairs.
I'd recommend running a profiler such as SlimTune (http://code.google.com/p/slimtune/) to see where most of the time is spent.
-
- Posts: 3
- Joined: Sun Aug 05, 2012 5:05 pm
Re: BulletSharp object referencing
Thank you for the thorough answer. Profiling will definitely be one of the things where time will be spent on this project, so thank you also for suggesting SlimTune.
///JmD
///JmD