Proposal: Non-core joints

At the moment, each joint type is in core, i.e. it is an intrinsic part of the box2d engine. It is not possible to add more without editing files in the Box2D folder. This is undesirable if we want to allow contribution joints, and easy hackability. I know of two prospective joints: Jorrit Rouwe's FixedJoint (in SVN), and Mike Lewis's Top Down Dry Friction Joint. There are several more potential joints, some are described in the TODO list.

I propose the following simple changes, which I designed for use with controllers, but the same principle applies.
Basically, joints are currently constrained to a finite set because they are part of an enumeration. If all uses of this enumeration were eliminated, and virtual methods used instead, there would be no problem.

Fortunately, the enumeration is only used in one place: b2Joint::Create. I suggest refactoring this by adding the following method to b2JointDef.
Code:
virtual b2Joint* Create(b2BlockAllocator* allocator) const = 0;
Each class deriving from b2JointDef contains the corresponding bit of b2Joint::Create.

Note this requires some forward declarations to work properly. It may be a good to provide an abstract interface to the allocator. At the least, it makes box2d's memory allocation a non-private detail. As only experienced users should be creating joints, and it is extending functionality rather than using functionality, I'd like to refer to allocators in box2d as of protected visibility (as opposed to public/private). They would not need to be documented to the extent of Box2D's public interface. The virtual methods of b2Joint should have a similar protected status.

Also note this makes b2JointDef a polymorphic class. I think this appropriate, as it already was in effect, using the mechanism of b2JointDef::type.

The joint enumeration values would remain, as they are useful for determining the type of a joint at runtime. For contributed joints, GetType should either return e_unknownJoint (forcing dynamic_cast, or userdata information to be used to safely downcast the joint), or the user is responsible for ensuring that it returns a unique value, and that the value makes sense. Joints added to contribution will be assigned GetType values in some special range.

The performance impact is an extra virtual function call on joint creation. I think that can safely be assumed to be negligible.

Compatibility issues are very low. It is unlikely any user code will be broken by this change. Thus, I do not think it necessary to block 2.1's release on this proposal.

I estimate the changes to be simple to make, but I have not prepared a sample patch.

Please post any comments, and if you think it a good idea. I shall post an issue to the tracker once I'm satisfied there is no further discussion being made here.
I think this is a good idea. I have wanted to implement custom joints in other engines and have been unable to do so because their APIs did not support it.

I would like to nail down how down-casting would work. I don't use RTTI in Box2D and it is generally not used in game development. I'm fine with extending the joint type enumeration with user joint place-holders: e_userJoint01, e_userJoint02, etc.

Destruction needs to know the size of the joint, so we have to implement a virtual function that returns the size or does the destruction.

Do you want support for custom debug drawing of the user joints?
Erin Catto wrote:
I would like to nail down how down-casting would work. I don't use RTTI in Box2D and it is generally not used in game development.


From my experience the only costly operation is dynamic_cast<>. Simple RTTI to use typeid() - needed to register joint types - don't introduce any performance overhead (only spatial: a static structure per polymorphic class).

You can use a normal hash/map structure over the type_info returned by typeid(wantedJoint), the cost would be similar to using an hash/map of integers. To be honest the comparison is system-dependent, but usually involve some sort of decorated strings. Statistically the test should not be too heavy (a couple of characters for not equal type_info), and can be accelerated storing the hash of those strings.

The advantage is that don't exists limits or clashes when registering/using new joint types.
With respect to run time typing, I wasn't suggesting that dynamic_cast should be used. Rather, that managing the types of user joints is the users responsibility - Box2D should not provide a framework comprehensive enough to sort it out. In practise, users can just make up their own integer id's for each user joint type, specific to their use, as we are unlikely to have closed-source joints, but it's not the only option.
I don't see the point in placeholders, it's easy enough to make up integers, and as enumerations don't have real typing in C++, there's no issue with "adding" extra values.

And yes, I'd like DebugDraw and DestroyJoint to be virtual methods of b2Joint. I fail at grepping. I believe I chose that DestroyController knows how to do de-allocation, rather than explicitly providing a GetMemorySize() method, so I'd recommend the same for joints.
Kafu, thanks for your input.

I will choose not to use RTTI and go with Boris's proposal instead.

Boris, please file an issue. We'll get to your other feature requests as development progresses. Please keep a list for yourself for now.

Original thread: https://box2d.org/forum/viewtopic.php?t=3970