Oh, I verified that my suspicion is correct.
I can’t say exactly how it happened, but there’s a strong indication the issue comes from optimizing legacy C++.
That part of the code is nearly 30 years old. I’ve reused it many times across different projects for various employers back when I was doing that kind of work.
Basically, it’s a result of how C++ used to be. Back then, the language wasn’t as smart or efficient as it is today. We avoided returning objects, minimized object initialization in constructors, and used manual approaches instead.
So, in this case, instead of having the function return a contact object, it takes a pointer and initializes part of it internally. we were all doing C in Cpp.
There’s really no reason to do it that way anymore. Modern compilers are incredibly smart and can optimize parameter passing, especially with inlining and runtime optimizations. And even without that, CPUs today are hundreds of times faster than they were when I wrote that part of the library.
you can see that paternt here
- Code: Select all
ndFloat32 ndShapeCompound::RayCast
...
ndContactPoint tmpContactOut;
ndShapeInstance* const shape = me->GetShape();
const ndVector p0 (shape->GetLocalMatrix().UntransformVector (localP0) & ndVector::m_triplexMask);
const ndVector p1 (shape->GetLocalMatrix().UntransformVector (localP1) & ndVector::m_triplexMask);
ndFloat32 param = shape->RayCast(callback, p0, p1, body, tmpContactOut);
if (param < maxT)
{
...
Ray cast is called with an empty contact point and initialize only part of it.
then if it a hit is found, it initializes the rest.
All of that for saving the function not returning a full contact and that's just bad C++ today.
These functions should return an object instead of writing to the passed in pointer,
If that was the only place contact was used it is easy to fix, but it is not.
So I have to fix it case by case for now.
Newton originally started as a collision engine first, and only later became a full rigid-body simulator after David Baraff’s papers inspired that direction.
Anyway, I’ll do a major overhaul later on, but that will take some time.
For now, I’ll just fix it so it works as intended.