jrouwe / JoltPhysics

A multi core friendly rigid body physics and collision detection library. Written in C++. Suitable for games and VR applications. Used by Horizon Forbidden West.
MIT License
6.88k stars 458 forks source link

Getting two body constraint end points in world coordinates #709

Closed jmiskovic closed 1 year ago

jmiskovic commented 1 year ago

Some context, the LOVR is a Lua-based 3D framework (graphics + audio + input controls + physics). I'm binding Jolt to the framework to replace the ODE engine integration. So far I've got far more stable behavior across many scenarios! :raised_hands:

Part of the LOVR physics API is fetching the constraint endpoints in world coordinates, for example this one. Most basic example is visualizing the constraint, or part of logic for dragging the end point to new position.

I'm struggling with Jolt API here. I can see the debug renderer smoothly reaching for private mWorldSpacePosition1, which is exactly what I'd need.

Am I supposed to work through GetConstraintToBody1Matrix and multiply it with body's center of mass myself? That seems overly complicated and wasteful calculations when the value is already (privately) available. I could also use GetConstraintSettings to obtain it, but that one is marked as debug function and it allocates memory.

Is there another way I'm not seeing?

:heart: Thank you for sharing Jolt with us, and also for your amazing work extending and supporting the community.

jrouwe commented 1 year ago

Actually, the debug drawing is not completely correct 😄

Those world space positions are the values during the last 'position constraint solve' iteration, which means that body 1 and or body 2 may have moved slightly since these values were calculated (or a lot if you moved the one of the bodies yourself), which is not reflected in those world space positions.

The intention is that you would use:

Vec3 pos1 = constraint->GetBody1()->GetCenterOfMassTransform() * constraint->GetConstraintToBody1Matrix().GetTranslation();
Vec3 pos2 = constraint->GetBody2()->GetCenterOfMassTransform() * constraint->GetConstraintToBody2Matrix().GetTranslation();

We could add getters for mLocalSpacePosition1 and 2 but you'd still need to perform that matrix multiply.

jmiskovic commented 1 year ago

Your code works great, thanks!

https://github.com/jrouwe/JoltPhysics/assets/17770782/4de1d865-48df-47aa-a6c0-a72364fb9f69