-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Jolt Physics as an alternative 3D physics engine #99895
Conversation
d43694f
to
dc9a08e
Compare
Looking good to me! The diff between godot-jolt and this module is much bigger than I anticipated, must have been a lot of work! |
Will the addon discontinue and only this will continue? |
Yeah, I thought I had a somewhat Godot-like codebase already, but getting rid of all my not-so-Godot idiosyncrasies was a lot of work indeed. I'm sure there's a few left that I've missed.
Thanks, I'll take a look!
Assuming this module is actually merged and reaches some kind of feature parity with the extension, yes. I don't see the need to maintain both at that point.
I'll admit I had forgotten that the physics tests in the demo projects repository even existed, but yeah, they seem fine. Although the performance tests seem to crash with either engine for some reason. I did run the tests in fabriceci/Godot-Physics-Tests beforehand though, and they seem to report the same failures that I've seen before:
The collision normal generated in this test is indeed slightly off when using Jolt, but I'm not sure it's worth getting hung up on, especially considering how dense the convex polygon is. The test is also done using
These fail because the impulse is applied before the body has been added to the physics space, meaning there's no underlying Jolt body to actually apply the impulse to, which I emit an error for currently. You can make these tests pass if you just swap these two lines around. The problem is you need to calculate the mass properties in order to apply impulses correctly, which can be expensive. In fact, Godot Physics defers this calculation as well, but still lets you apply impulses, resulting in incorrect behavior, as seen in #75934.
This fails because the position error exceeds the tolerance by ~0.006 units. I'm not sure it's worth getting hung up on. It's possible that this is caused/worsened by one of the several discrepancies in
Stacked spheres do seem to oscillate more in Jolt, even when setting the Baumgarte stabilization to |
Great job mihe, jrouwe, and everyone else involved. I build this and master and spend some time testing both by running the jolt module and godot physics in parallel on the same scenes and projects. So far I am pleasantly surprised that it already works so well considering that so much code had to be changed for the module. I did not encounter anything that I would really worry about, no error spam or really broken behavior issues swapping the physics in any of my test projects. In fact a few things that I knew were a little brittle and easy to make explode felt a lot more robust with the jolt module. Yes I encountered a few random issues but ad hoc I am not sure if they are even caused by the physics or just some weird editor or script code. Need to investigate and test some more. Having the two physics run next to each other it was difficult to spot an actual difference in most situations. The most noticeable for me was the joint behavior. For some reason the joints in the "official" Godot physics tests demo project loved to spin in circles when pushed or moved with the jolt module. Do you know what would cause such a difference in behavior compared to the godot physics and if anything can be done about it? Here is the link for the demo git (or asset lib was already posted). The "official" Godot physics test demo showed some other issues. It actually crashes in the performance test of that demo as the jolt body count of I would recommend trying out all those tests in that demo project so the most "obvious" things can be ironed out before this gets into more hands. I am sure there will be some other things only discovered by more broad user testing as is always the case. I think that will be unavoidable by such a huge module change and why there will be both physics for some time to fix and adapt.
If you forward the rendering scenario RID of the viewport to the jolt module for debug rendering it can add whatever it wants with the RenderingServer API without actually "hacking" itself into the Viewport code. Users could even render the entire debug in a different debug viewport this way by just switching viewport scenarios or split things up. The current physics partly hacks its collision contacts inside Viewport nodes which is nothing but ugly. Same with the node based debug shape rendering that omits all server created objects and does not run in sync with the actual physics step and state. So those likely will need settings to not have both debugs flicker over each other.
Yes there are some tiny differences in the return values between the two physics or in general but seriously we are talking about an "error" margin that I find totally acceptable and negatable in context of game engine physics. I know some people can be really riled up about such details but a friendly reminder this is a game engine and not a scientific simulation, the physics need to work in game engine context. It is not about simulating stuff so we can shoot and bounce stuff with pinpoint accuracy to the moon, it is about making games work without the physics exploding left, right and center at every shape seam or step while also not running at 1 fps ... or at least that should imo be the "humble" goal before we worry about those other things. |
Please do try to share repros of whatever issues you run into.
EDIT: I misread. I'll take a look.
I had glossed over this due to Godot Physics also crashing for me in the broadphase performance test (which seems to be from a stack overflow) so I figured it was something with the test itself, but it seems that the crash with this module was indeed a sloppy regression on my part. That's been fixed now, and you should instead get a "graceful" outcome in the form of error spam about needing to increase the body limit. |
@ryevdokimov You should find that this works now. |
Is this still true regarding shape creation as well? |
Looks good, I'll mark the comment I made as resolved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've reviewed the thirdparty code integration, and skimmed through some of the module code, this is very solid work.
The code style seems pretty good, I could find some nitpicks but not really worth pointing out on such a massive code conversion from extension to module.
Tested briefly with the GDQuest 3D TPS demo, it seems to work well as a drop-in replacement.
I would suggest making the project setting for the physics engine a "basic" setting so it's easy to find for users who want to test Jolt. And for good measure the 2D one can likely be too, since there are some GDExtensions available for Box2D and Rapier2D I believe.
Would I do that in this PR? Also, as mentioned in the pull request description, and mentioned in passing in other conversations, this module should ideally be considered experimental for the near future. With that in mind, do we perhaps want to change the registered server name to be something like (I guess that also begs the question why there isn't a space inbetween |
Yeah I would suggest so, so it's easy to test right away. And it's a pretty minor change well justified by adding a secondary builtin physics engine for 3D.
We'll mention it in the blog post for sure, but if you want to make the visible name include
It can definitely be The name there should be the canonical name of the library. I think for Bullet Physics we just had
|
There is no integer value backing this, unfortunately. The Should I still go ahead with adding it? |
Then I wouldn't add the |
Co-authored-by: Jorrit Rouwe <[email protected]>
It would still keep the Frankly, the silent fallback that's currently happening should be an error/warning instead. Anyway, so...
I don't believe there are any more pending issues to resolve. |
Thank you @mihe and @jrouwe for this amazing contribution! Jolt has proven to be an excellent fit for Godot, especially after all the work done upstream to accommodate our high level API and ensure we can preserve as much compatibility as possible. The result as of this PR is a drop-in replacement that covers most of the API and should work well for most games. The remaining parts which don't have parity yet will be evaluated in coming months while our Godot Jolt integration goes from experimental to production ready, and eventually if all goes well the new default physics engine. Thanks also for all the work put into godot-jolt, which helped ensure and proved that GDExtension is suitable for adding major functionality like a whole new physics API, and to iterate quickly in a community project without being held back with the upstream core bureaucracy. Being able to then convert the GDExtension using godot-cpp to a core module was also part of the intended design of godot-cpp, aiming to keep as much of the API matching what Godot C++ modules can do. Let's get this well tested and ironed out for the 4.4 release! |
Awesome! I'm excited to see where things go from here. Hopefully we can smooth out whatever rough edges there are currently and eventually make Jolt the default 3D physics engine. Also, a big thank you to everyone who took the time to try out the extension and report issues during these past two years. This module would likely have been in a much rougher state (if at all) without the battle-testing from the community. |
This is very cool! My thanks goes out to @mihe for spending so much time in making this plugin. Jolt really improved due to all the issues he found and requests he made! It has been a joy working together! Looking forward to the last push to make it production ready! Feel free to tag me on any issues that are reported. Also it would be nice if there was a label that I can filter on to quickly find issues reported against this module, e.g. |
We could add this easily, but it would be quite specific compared to our other labels which typically encompass whole systems (like We do have a need for more granularity but labels won't cut it (we'd need to multiply the amount of labels by 3-5 to cover everything), instead we're working on preparing some public GitHub projects for each team/system (basically each |
First of all. Nice work. One thing that would be a game changer for my game project, which heavily relies on modifying compound, rigid bodies with sometimes hundreds of shapes would be to be able to offload the updating process of these bodies to a worker thread. This would cause the body, and then replacing the body a few frames later. This would greatly reduce the risk of frame rate drops upon adding or removing shapes to a body. The trade-off to wait for a frame or two to get the new shape in place would in this case be more than reasonable in my opinion. This could practically be achieved through adding an opt-in bool parameter in the PhysicsServer3D.body_[add/remove]_shape or by a project setting. Would you @mihe think this would be an impossibility? On another note. Will Godot's 64-bit precision option work within this framework? How will Godot integrate Jolt's ability to work with vector instructions (SSE, AVX etc)? |
It should: godot/modules/jolt_physics/SCsub Line 155 in b9437c3
See also about how it was added: jrouwe/JoltPhysics#94 (comment) |
Is the common/physics_interpolation function of Godot adapted to JOLT?@mihe |
Yes, physics interpolation works out of the box with Jolt. It's not performed in the physics engine, but within the rest of the engine, so it works with third-party physics engines too. It'll even work if you write your own physics in |
I suppose jolt being now part of the Godot engine, I should file bug reports for it in Godot Engine's issues, right? |
To be clear, the Jolt project is still a standalone library that has its own development priorities and issue tracker. If you have issues with Jolt in Godot, then yes those issues should be filed in the Godot repository, so that we can assess whether the problem comes from the Godot integration, or is a library issue that needs to bubble up upstream. |
To add to this, if the bug is manifesting with the Godot Jolt extension then you're welcome to continue reporting them to the If the bug is manifesting in this new engine module, then definitely report it to the Godot repository.
@albinaask This discussions is probably better suited for a proposal, but it's certainly not impossible. The question is more of whether there are other approaches that can help alleviate this without burdening the shared
@albinaask As far as I know, Jolt should automatically make use of whatever instruction sets are enabled/defined during compilation, which for Godot is SSE2 by default. If you want more than this (SSE4.2, AVX, etc.) you'll need to pass the appropriate compiler options (e.g. |
Closes godotengine/godot-proposals#7308.
This pull request adds a new engine module (found under
modules/jolt_physics
) which integrates Jolt Physics, a 3D physics engine developed by Jorrit Rouwe (@jrouwe) with a focus on games and VR applications, used in titles such as Horizon Forbidden West.Users of this new module should (generally, but not always) be able to expect better physics performance, both in terms of CPU performance and simulation stability, when compared to Godot Physics.
This new module is a port of the Godot Jolt extension, developed by myself over the past two years, in large part as a personal project, but with the last year (including this port) being sponsored by W4 Games. I've also received a significant amount of assistance and contributions from @jrouwe during this development, which I'm very grateful for. In fact, several major features have been added to Jolt largely to cater to the needs of Godot.
Warning
Note that while this code has gone through a fair amount of battle-testing as part of the Godot Jolt extension there have been non-trivial refactorings done as part of this port that have not been tested much at all. As such, this module should be considered experimental for now. This module is also not at feature parity with Godot Physics just yet, nor at feature parity with the Godot Jolt extension either.
This does not replace Godot Physics as the default 3D physics engine. You will need to opt in by setting the
physics/3d/physics_engine
project setting toJolt Physics
.Notable differences to Godot Physics
While the goal is to have this new module be a drop-in replacement for Godot Physics (within reason), Jolt inherently differs from it in a number of ways, which means that it is unlikely that you will see the exact same behavior when switching between the two. There are also several higher-level decisions taken within the module that deviate further from how Godot Physics does things.
Area3D
and static bodies[Click to expand/collapse]
When using Jolt,
Area3D
will not detect overlaps withStaticBody3D
(nor aRigidBody3D
frozen withFREEZE_MODE_STATIC
) by default, for performance reasons. If you have many/largeArea3D
overlapping with complex static geometry, such asConcavePolygonShape3D
orHeightMapShape3D
, you can end up wasting a significant amount of CPU performance without realizing it.For this reason this behavior is opt-in through the project setting
physics/jolt_physics_3d/simulation/areas_detect_static_bodies
, with the recommendation that you set up your collision layers/masks in such a way that only the relevantArea3D
are able to detect collisions with static bodies.This should probably be made into an
Area3D
property instead, through something like what's been suggested here.Joint properties
[Click to expand/collapse]
The current interfaces for the 3D joint nodes, which seem to be derived from the Bullet Physics library, don't quite line up with the interface of Jolt's own joints. As such, there are a number of joint properties that are not supported, mainly ones related to configuring the joint's soft limits.
The unsupported properties are:
PinJoint3D
:bias
,damping
,impulse_clamp
HingeJoint3D
:bias
,softness
,relaxation
SliderJoint3D
:angular_*
,*_limit/softness
,*_limit/restitution
,*_limit/damping
ConeTwistJoint3D
:bias
,relaxation
,softness
Generic6DOFJoint3D
:*_limit_*/softness
,*_limit_*/restitution
,*_limit_*/damping
,*_limit_*/erp
Currently an error is emitted if you set these properties to anything but their default values.
In the Godot Jolt extension I exposed alternative joint nodes that better matched Jolt's interface, but these have not been included in this module. Instead, the above mentioned properties should probably be removed and replaced by properties more fitting for Jolt, through something like what's been suggested here.
Single-body joints
[Click to expand/collapse]
You can, in Godot, omit one of the joint bodies for a two-body joint and effectively have "the world" be the other body. However, the node path that you assign your body to (
node_a
vsnode_b
) is ignored. Godot Physics will always behave as if you assigned it tonode_a
, and sincenode_a
is also what defines the frame of reference for the joint limits, you end up with inverted limits and a potentially strange limit shape, especially if your limits allow both linear and angular degrees of freedom.This is arguably a bug and should be changed. This is also not how single-body joints behaves with Bullet in Godot 3, which behaves as if you assigned the body to
node_b
instead. It's not clear why this changed in Godot 4.For this reason this module will, like Bullet in Godot 3, behave as if you assigned the body to
node_b
instead, withnode_a
representing "the world".There is a project setting called
physics/jolt_physics_3d/joints/world_node
that lets you toggle this behavior, if you need compatibility for an existing project.Collision margins
[Click to expand/collapse]
Jolt (and other similar physics engines) uses something that Jolt refers to as "convex radius" to help improve the performance and behavior of the types of collision detection that Jolt relies on for convex shapes. Other physics engines (Godot included) might refer to these as "collision margins" instead. Godot exposes these as the
margin
property on everyShape3D
-derived class, as a leftover from the Bullet integration in Godot 3, but Godot Physics itself does not use them for anything.What these collision margins sometimes do in other engines (as described in Godot's documentation) is effectively add a "shell" around the shape, slightly increasing its size while also rounding off any edges/corners. In Jolt however, these margins are first used to shrink the shape, and then the "shell" is applied, resulting in edges/corners being similarly rounded off, but without increasing the size of the shape.
To prevent having to tweak this
margin
property manually, since its default value can be problematic for smaller shapes, this module exposes a project setting calledphysics/jolt_physics_3d/collisions/collision_margin_fraction
which is multiplied with the smallest axis of the shape's AABB to calculate the actual margin. Themargin
property of the shape is then instead used as an upper bound.These margins should, for most use-cases, be more or less transparent, but can sometimes result in odd collision normals when performing shape queries. You can lower the above mentioned project setting to mitigate some of this, including setting it to
0
, but too small of a margin can also cause odd collision results, so is generally not recommended.Compound shapes
[Click to expand/collapse]
Compound shapes (i.e. bodies with more than one shape) are implemented differently from Godot Physics.
Jolt offers the choice of two compound shapes, one called
MutableCompoundShape
and one calledStaticCompoundShape
. The former trades in runtime performance for faster construction/modification time, and vice versa for the latter.Godot Physics maps closer to Jolt's
MutableCompoundShape
, but I decided to go withStaticCompoundShape
for this implementation, as it simplified things a bit (with being able to discard and rebuild the whole thing when modified) and I figured more people would benefit from the improved runtime performance as opposed to mutation performance.To mitigate the cost of this potentially expensive rebuild, I made it so that shape changes are only ever "committed" to Jolt when the body has entered into a scene tree. This means that you can make shape changes (including adding/removing shapes) very quickly so long as the body isn't attached to the scene tree. However, if you do in fact add the body to a scene tree, and then start adding/removing/changing shapes on the body, you can end up with worse performance than Godot Physics. This is prominently visible in the "Voxel Game" demo project, as one example.
The plan I have in mind for this (discussed in jrouwe/JoltPhysics#1165) is to replace
StaticCompoundShape
with a custom compound shape that wrapsStaticCompoundShape
, but which will always defer its building/committing only until absolutely necessary, meaning either right before a simulation step or when performing queries against the body.If the use of
StaticCompoundShape
within this new custom compound shape still ends up being a problem for some use-cases it would likely be trivial to expose some setting or property that lets you useMutableCompoundShape
instead.Scaling shapes/bodies/queries
[Click to expand/collapse]
Godot Physics supports scaling the transform of collision shapes, shape queries, as well as static and kinematic bodies, meaning
StaticBody3D
,CharacterBody3D
,AnimatableBody3D
and frozenRigidBody3D
. It does not however support scaling simulated/dynamic bodies, such as a non-frozenRigidBody3D
, and will effectively discard any such scaling, instead treating it as(1, 1, 1)
.Jolt does however support scaling everywhere, and I've tried my best to utilize that, which means that
RigidBody3D
will support scaling when using Jolt, despite the warning shown on the node currently.Jolt also supports non-uniform scaling, so long as the inherent primitive shape is preserved. For example, you can scale a cylinder along its height axis but not along its other axes. You will however currently see warnings on the shape node when doing this, due to Godot Physics not supporting this.
Since invalid scaling can cause a number of weird artifacts, and sometimes outright crash the simulation, there are runtime error checks that "sanitize" all scaling to be valid for that particular shape arrangement, and then reports an error if the difference is above an arbitrary threshold. These errors have however proven to be a bit frustrating for some users, who might not care about the sometimes minor corrections that this error-checking applies, so this likely needs a different approach.
Shape-casting
[Click to expand/collapse]
Due to Godot having a "safe" and "unsafe" fraction in the results of
cast_motion
(andShapeCast3D
), meaning the distances at which the cast shape was found to be colliding and not colliding respectively, it was not viable to rely on Jolt's own shape-casting (which uses conservative advancement) to implementcast_motion
. Insteadcast_motion
is implemented with a binary search, similar to how Godot Physics does it.However, in Godot Physics this binary search is hardcoded to 8 steps, which tends to result in quite jittery output over even moderate distances. With Godot Jolt (and consequently this module) I chose to instead dynamically calculate the number of steps based on the cast distance, aiming for roughly millimeter precision, and then clamp it between 4 and 16 steps, which seems to have worked out well.
This does however mean that
cast_motion
will technically, when using Jolt, cost more CPU performance the farther you cast the shape.Note that this also applies to
body_test_motion
, and consequentlytest_move
,move_and_collide
andmove_and_slide
.Baumgarte stabilization
[Click to expand/collapse]
Jolt employs a technique in its solver called Baumgarte stabilization, which is meant to mitigate constraint drift within the simulation, resulting in a more stable simulation. This technique can however result in some artifacts, like piles of bodies not separating as quickly as one might expect.
The strength of this stabilization can be tweaked using the project setting
physics/jolt_physics_3d/simulation/baumgarte_stabilization_factor
. Setting this project setting to1.0
will effectively disable the technique, resulting in a simulation that more closely resembles Godot Physics, but which is also more unstable.Motion queries (
move_and_slide
, etc.)[Click to expand/collapse]
Physics servers in Godot are meant to implement the
PhysicsServer3D.body_test_motion
method, which in turn powers methods likePhysicsBody3D.test_move
,PhysicsBody3D.move_and_collide
andCharacterBody3D.move_and_slide
, which are largely meant to be used for moving player characters around.PhysicsServer3D.body_test_motion
is split into three parts, the first being depenetration (called "recovery" in Godot), the second being a shape-cast from the "recovered" position, and the third being the actual collision check, at the "unsafe" fraction of the shape-cast.The "recovery" step in Godot Physics is hardcoded to always do 4 iterations with 40% depenetration per iteration. I figured these constants might be useful to expose, so when using this module they can be configured in the project settings as
physics/jolt_physics_3d/motion_queries/recovery_iterations
andphysics/jolt_physics_3d/motion_queries/recovery_amount
respectively.The implementation of
PhysicsServer3D.body_test_motion
in this module also differs slightly from the Godot Physics implementation, as replicating the Godot Physics version resulted in a prohibitive amount of ghost collisions formove_and_slide
.The discrepancies are as follows:
TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR
.While these discrepancies do seem to result in less ghost collisions when using Jolt, they also introduce new problems:
move_and_slide
is generally slower than in Godot Physics, due toCharacterBody3D::_snap_on_floor
triggering on every call.CharacterBody3D
will not report wall collisions unless you're moving towards them.It's not clear to me how to fix these issues, but I suspect they will require changes to
move_and_slide
on the scene side of things.Ghost collisions
[Click to expand/collapse]
Jolt employs two techniques to mitigate ghost collisions, meaning collisions with internal edges of shapes/bodies.
The first technique, called "active edge detection", marks edges of triangles in
ConcavePolygonShape3D
orHeightMapShape3D
as either "active" or "inactive", based on the angle to the neighboring triangle. When a collision happens with an inactive edge the collision normal will be replaced with the triangle's normal instead, to lessen the effect of ghost collisions.The angle threshold for this active edge detection is configurable through the project setting
physics/jolt_physics_3d/collisions/active_edge_threshold
.The second technique, called "enhanced internal edge removal", instead adds runtime checks to detect whether an edge is active or inactive, based on the contact points of the two bodies. This has the benefit of applying not only to collisions with
ConcavePolygonShape3D
andHeightMapShape3D
, but also edges between any shapes within the same body.Enhanced internal edge removal can be toggled on and off for the various contexts to which it's applied, using the
physics/jolt_physics_3d/*/use_enhanced_internal_edge_removal
project settings.Memory usage
[Click to expand/collapse]
Jolt uses a stack allocator for temporary allocations within its simulation step. This stack allocator requires allocating a set amount of memory up front, which can be configured using the
physics/jolt_physics_3d/limits/temporary_memory_buffer_size
project setting.Jolt also makes use of aligned allocations for some of its memory usage, which it acquires through function pointers that the implementer assigns. Aligned allocations were recently added to Godot in the form of
Memory::alloc_aligned_static
,Memory::realloc_aligned_static
andMemory::free_aligned_static
, which have all been hooked up to Jolt. However, these aligned allocation functions don't currently touchMemory::mem_usage
, which means that some of Jolt's memory won't be tracked, and thus the performance monitors in Godot won't be accurate.Ray-cast face index
[Click to expand/collapse]
The
face_index
property returned in the results ofintersect_ray
andRayCast3D
will by default always be-1
with Jolt, with a project setting (physics/jolt_physics_3d/queries/enable_ray_cast_face_index
) to enable them. The reason for this being that Jolt does not store these indices, and for them to be stored we need to enable the per-triangle userdata, which adds about 25% extra memory usage to the underlying Jolt implementation ofConcavePolygonShape3D
.This should maybe be made into a
ConcavePolygonShape3D
property instead, through something like what's been suggested here.Kinematic
RigidBody3D
contacts[Click to expand/collapse]
When using Jolt, a
RigidBody3D
frozen withFREEZE_MODE_KINEMATIC
will by default not report contacts from collisions with other static/kinematic bodies, for performance reasons, even when setting a non-zeromax_contacts_reported
. If you have many/large kinematic bodies overlapping with complex static geometry, such asConcavePolygonShape3D
orHeightMapShape3D
, you can end up wasting a significant amount of CPU performance without realizing it.For this reason this behavior is opt-in through the project setting
physics/jolt_physics_3d/simulation/generate_all_kinematic_contacts
.This should probably be made into an
RigidBody3D
property instead, through something like what's been suggested here.Contact impulses
[Click to expand/collapse]
Jolt is not able to provide any impulse as part of its contact data, due to how it orders its simulation step, and instead provides a helper function for estimating what the impulse would be based on various parameters. While this is probably fine for most use cases, like emitting a sound based on how hard something collided, it won't be accurate if a body is colliding with multiple bodies during a simulation step.
Area3D
andSoftBody3D
[Click to expand/collapse]
This module does not currently support any interactions between
SoftBody3D
andArea3D
, such as overlap events, or the wind properties found onArea3D
. Support for this has been added to Jolt recently, and @jrouwe also did some work towards the extension part of it, but it has not been included in this pull request.ConvexPolygonShape3D
[Click to expand/collapse]
Godot Physics currently skips calculating a proper center-of-mass and inertia for
ConvexPolygonShape3D
, and instead always uses the shape's local position as its center of mass, while crudely estimating the inertia. Jolt on the other hand does calculate a more accurate center-of-mass and inertia for them. As a result a non-frozenRigidBody3D
with such a shape in it can behave differently when comparing the two engines.WorldBoundaryShape3D
[Click to expand/collapse]
WorldBoundaryShape3D
, which is meant to represent an infinite plane, is implemented a bit differently in Jolt compared to Godot Physics. Both engines have an upper limit for how big the effective size of this plane can be, but this size is much smaller when using Jolt, in order to avoid precision issues.You can configure this size using the
physics/jolt_physics_3d/limits/world_boundary_shape_size
project setting.Axis-locking
[Click to expand/collapse]
The
PhysicsBody3D.axis_lock_*
properties in Godot Physics are implemented by simply zeroing out the velocities for the selected axes. Jolt instead implements these by calculating a new inverse mass/inertia, similar to howRigidBody3D.lock_rotation
works in Godot Physics, which seems to better mitigate energy loss during simulation.However, Jolt does not allow locking all axes, meaning both linear and angular axes, and an error will be emitted from this module when trying to do so, with the recommendation that you instead freeze the body entirely. While this is a simple enough workaround for
RigidBody3D
, you cannot currently freeze aPhysicalBone3D
, so if you want to lock all axes of aPhysicalBone3D
you're forced to resort to callingPhysicsServer3D.body_set_mode
yourself.Notable differences to Godot Jolt
While this module is largely a straight port of the Godot Jolt extension, with a lot of cosmetic changes, there are a few things that are different.
Project settings
[Click to expand/collapse]
All project settings have been moved from the
physics/jolt_3d
category tophysics/jolt_physics_3d
.On top of that, there's been some renaming and refactoring of the individual project settings as well. These include:
sleep/enabled
is nowsimulation/allow_sleep
.sleep/velocity_threshold
is nowsimulation/sleep_velocity_threshold
.sleep/time_threshold
is nowsimulation/sleep_time_threshold
.collisions/use_shape_margins
is nowcollisions/collision_margin_fraction
, where a value of 0 is equivalent to disabling it.collisions/use_enhanced_internal_edge_removal
is nowsimulation/use_enhanced_internal_edge_removal
.collisions/areas_detect_static_bodies
is nowsimulation/areas_detect_static_bodies
.collisions/report_all_kinematic_contacts
is nowsimulation/generate_all_kinematic_contacts
.collisions/soft_body_point_margin
is nowsimulation/soft_body_point_radius
.collisions/body_pair_cache_enabled
is nowsimulation/body_pair_contact_cache_enabled
.collisions/body_pair_cache_distance_threshold
is nowsimulation/body_pair_contact_cache_distance_threshold
.collisions/body_pair_cache_angle_threshold
is nowsimulation/body_pair_contact_cache_angle_threshold
.continuous_cd/movement_threshold
is nowsimulation/continuous_cd_movement_threshold
, but expressed as a fraction instead of a percentage.continuous_cd/max_penetration
is nowsimulation/continuous_cd_max_penetration
, but expressed as a fraction instead of a percentage.kinematics/use_enhanced_internal_edge_removal
is nowmotion_queries/use_enhanced_internal_edge_removal
.kinematics/recovery_iterations
is nowmotion_queries/recovery_iterations
, but expressed as a fraction instead of a percentage.kinematics/recovery_amount
is nowmotion_queries/recovery_amount
.queries/use_legacy_ray_casting
has been removed.solver/position_iterations
is nowsimulation/position_steps
.solver/velocity_iterations
is nowsimulation/velocity_steps
.solver/position_correction
is nowsimulation/baumgarte_stabilization_factor
, but expressed as a fraction instead of a percentage.solver/active_edge_threshold
is nowcollisions/active_edge_threshold
.solver/bounce_velocity_threshold
is nowsimulation/bounce_velocity_threshold
.solver/contact_speculative_distance
is nowsimulation/speculative_contact_distance
.solver/contact_allowed_penetration
is nowsimulation/penetration_slop
.limits/max_angular_velocity
is now stored as radians instead.limits/max_temporary_memory
is nowlimits/temporary_memory_buffer_size
.There might be some discussion to be had with regards to migrating the settings values for projects who have previously been relying on the extension.
Joint nodes
[Click to expand/collapse]
The joint nodes that are exposed in the Godot Jolt extension (
JoltPinJoint3D
,JoltHingeJoint3D
,JoltSliderJoint3D
,JoltConeTwistJoint3D
andJoltGeneric6DOFJoint
) have not been included with this module.Instead of exposing bespoke joint nodes, the existing joint node interfaces should be modified by the physics server, through something like what's been suggested here.
Thread-safety
[Click to expand/collapse]
Unlike the Godot Jolt extension, this module does have experimental thread-safety, including support for the
physics/3d/run_on_separate_thread
project setting. This is achieved by utilizing the same wrapper server that's used by Godot Physics, calledPhysicsServer3DWrapMT
, as well as introducing a mutex around the rebuilding of shapes, since concurrent shape-casts could otherwise trigger a race condition.This has however not been tested very thoroughly, so should be considered experimental.
Query performance
[Click to expand/collapse]
The Godot Jolt extension utilizes a custom container (typically referred to as an "inline vector" or "small vector") in order to avoid heap allocations for physics queries that return more than a single hit, meaning
intersect_point
,intersect_shape
,collide_shape
,get_rest_info
,cast_motion
,body_test_motion
,test_move
,move_and_collide
andmove_and_slide
.For the sake of simplifying this port I chose to just use
JPH::Array
for the query collectors instead, which means that these queries will now always allocate on the heap, likely making them noticeably slower.It should be trivial to make some bespoke container for the query collectors that behaves like an inline/small vector.
Debug renderer
[Click to expand/collapse]
Jolt provides an interface called
JPH::DebugRenderer
for rendering its view of the physics simulation. In the Godot Jolt extension I expose this as a customGeometryInstance3D
node calledJoltDebugGeometry3D
, which proved to be very useful during development, but I figured this module should probably take a different approach, so I have omitted that code.Debug snapshots
[Click to expand/collapse]
The Godot Jolt extension has the ability to generate what Jolt refers to as "snapshots", where it serializes the state of the physics simulation to a file. These can then be loaded in Jolt's own
Samples
application, to debug issues more closely there, without needing to deal with Godot itself, which has proved to be quite useful when reporting issues upstream to Jolt.The code for this is technically included with this module, and can be found as
JoltPhysicsServer3D::dump_debug_snapshots
, but it wasn't clear to me how to best expose this, so I've left it unexposed for now.Things left to do
Just to give people an idea about the state of this module, and perhaps encourage future contributions if/when this pull request is merged, here is a rough copy of my to-do list:
Must-have
Area3D
andSoftBody3D
.PhysicalBone3D
.Nice-to-have
dev_build
.jolt_body_accessor_3d.h
, in favor of just storing theJPH::Body*
inJoltObject3D
.JPH::PhysicsSystem::GetBodies
, to avoid the overhead of iterating over static/sleeping bodies.Jolt.natvis
file in MSVC builds for easier debugging.What about the extension?
If/when this pull request is merged, the Godot Jolt extension will officially be considered to be in maintenance mode going forward, with only bug fixes backported to it, but so long as this engine module doesn't have full feature parity with the extension there will be new releases of the extension to ensure that projects relying on its additional features can continue to function.
If/when this engine module reaches full feature parity with the extension then the extension will be discontinued and its GitHub repository archived.
Attribution
In the interest of not cluttering every file in this new module with the copyright notice of Godot Jolt, as required by its MIT license, consider this my permission to omit it entirely. I have obtained permission from the applicable copyright holders (@jrouwe) to do so as well.
(This port is based on godot-jolt/godot-jolt@8f1212e, meaning the latest commit of Godot Jolt as of writing this.)