Skip to content
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

Refactor Jolt-related project settings to only be loaded as needed #101254

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions doc/classes/ProjectSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2382,13 +2382,11 @@
[b]Note:[/b] Setting this too high can result in objects not depenetrating properly.
[b]Note:[/b] This applies to all shape queries, as well as physics bodies within the simulation.
[b]Note:[/b] This does not apply when enabling Jolt's enhanced internal edge removal, which supersedes this.
[b]Note:[/b] This setting will only be read once during the lifetime of the application.
</member>
<member name="physics/jolt_physics_3d/collisions/collision_margin_fraction" type="float" setter="" getter="" default="0.08">
The amount of collision margin to use for certain convex collision shapes, such as [BoxShape3D], [CylinderShape3D] and [ConvexPolygonShape3D], as a fraction of the shape's shortest axis, with [member Shape3D.margin] as the upper bound. This is mainly used to speed up collision detection with convex shapes.
[b]Note:[/b] Collision margins in Jolt do not add any extra size to the shape. Instead the shape is first shrunk by the margin and then expanded by the same amount, resulting in a shape with rounded corners.
[b]Note:[/b] Setting this value too close to [code]0.0[/code] may also negatively affect the accuracy of the collision detection with convex shapes.
[b]Note:[/b] This setting will only be read once during the lifetime of the application.
</member>
<member name="physics/jolt_physics_3d/joints/world_node" type="int" setter="" getter="" default="0">
Which of the two nodes bound by a joint should represent the world when one of the two is omitted, as either [member Joint3D.node_a] or [member Joint3D.node_b]. This can be thought of as having the omitted node be a [StaticBody3D] at the joint's position. Joint limits are more easily expressed when [member Joint3D.node_a] represents the world.
Expand Down Expand Up @@ -2423,28 +2421,23 @@
<member name="physics/jolt_physics_3d/motion_queries/recovery_amount" type="float" setter="" getter="" default="0.4">
Fraction of the total penetration to depenetrate per iteration during motion queries.
[b]Note:[/b] This affects methods [method CharacterBody3D.move_and_slide], [method PhysicsBody3D.move_and_collide], [method PhysicsBody3D.test_move] and [method PhysicsServer3D.body_test_motion].
[b]Note:[/b] This setting will only be read once during the lifetime of the application.
</member>
<member name="physics/jolt_physics_3d/motion_queries/recovery_iterations" type="int" setter="" getter="" default="4">
The number of iterations to run when depenetrating during motion queries.
[b]Note:[/b] This affects methods [method CharacterBody3D.move_and_slide], [method PhysicsBody3D.move_and_collide], [method PhysicsBody3D.test_move] and [method PhysicsServer3D.body_test_motion].
[b]Note:[/b] This setting will only be read once during the lifetime of the application.
</member>
<member name="physics/jolt_physics_3d/motion_queries/use_enhanced_internal_edge_removal" type="bool" setter="" getter="" default="true">
If [code]true[/code], enables Jolt's enhanced internal edge removal during motion queries. This can help alleviate ghost collisions, but only with edges within a single body, meaning edges between separate bodies can still cause ghost collisions.
[b]Note:[/b] This affects methods [method CharacterBody3D.move_and_slide], [method PhysicsBody3D.move_and_collide], [method PhysicsBody3D.test_move] and [method PhysicsServer3D.body_test_motion].
[b]Note:[/b] This setting will only be read once during the lifetime of the application.
</member>
<member name="physics/jolt_physics_3d/queries/enable_ray_cast_face_index" type="bool" setter="" getter="" default="false">
If [code]true[/code], populates the [code]face_index[/code] field in the results of [method PhysicsDirectSpaceState3D.intersect_ray], also accessed through [method RayCast3D.get_collision_face_index]. If [code]false[/code], the [code]face_index[/code] field will be left at its default value of [code]-1[/code].
[b]Note:[/b] Enabling this setting will increase Jolt's memory usage for [ConcavePolygonShape3D] by around 25%.
[b]Note:[/b] This setting will only be read once during the lifetime of the application.
</member>
<member name="physics/jolt_physics_3d/queries/use_enhanced_internal_edge_removal" type="bool" setter="" getter="" default="false">
If [code]true[/code], enables Jolt's enhanced internal edge removal during shape queries. This can help alleviate ghost collisions when using shape queries for things like character movement, but only with edges within a single body, meaning edges between separate bodies can still cause ghost collisions.
[b]Note:[/b] This affects methods [method PhysicsDirectSpaceState3D.cast_motion], [method PhysicsDirectSpaceState3D.collide_shape], [method PhysicsDirectSpaceState3D.get_rest_info] and [method PhysicsDirectSpaceState3D.intersect_shape].
[b]Note:[/b] Enabling this setting can cause certain shapes to be culled from the results entirely, but you will get at least one intersection per body.
[b]Note:[/b] This setting will only be read once during the lifetime of the application.
</member>
<member name="physics/jolt_physics_3d/simulation/allow_sleep" type="bool" setter="" getter="" default="true">
If [code]true[/code], [RigidBody3D] nodes are allowed to go to sleep if their velocity is below the threshold defined in [member physics/jolt_physics_3d/simulation/sleep_velocity_threshold] for the duration set in [member physics/jolt_physics_3d/simulation/sleep_time_threshold]. This can improve physics simulation performance when there are non-moving [RigidBody3D] nodes, at the cost of some nodes possibly failing to wake up in certain scenarios. Consider disabling this temporarily to troubleshoot [RigidBody3D] nodes not moving when they should.
Expand All @@ -2470,7 +2463,6 @@
</member>
<member name="physics/jolt_physics_3d/simulation/bounce_velocity_threshold" type="float" setter="" getter="" default="1.0">
The minimum velocity needed before a collision can be bouncy, in meters per second.
[b]Note:[/b] This setting will only be read once during the lifetime of the application.
</member>
<member name="physics/jolt_physics_3d/simulation/continuous_cd_max_penetration" type="float" setter="" getter="" default="0.25">
Fraction of a body's inner radius that may penetrate another body while using continuous collision detection.
Expand Down
2 changes: 1 addition & 1 deletion modules/jolt_physics/joints/jolt_joint_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ JoltJoint3D::JoltJoint3D(const JoltJoint3D &p_old_joint, JoltBody3D *p_body_a, J
body_b->add_joint(this);
}

if (body_b == nullptr && JoltProjectSettings::use_joint_world_node_a()) {
if (body_b == nullptr && JoltProjectSettings::joint_world_node == JOLT_JOINT_WORLD_NODE_A) {
// The joint scene nodes will, when omitting one of the two body nodes, always pass in a
// null `body_b` to indicate it being the "world node", regardless of which body node you
// leave blank. So we need to correct for that if we wish to use the arguably more intuitive
Expand Down
201 changes: 48 additions & 153 deletions modules/jolt_physics/jolt_project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@

#include "core/config/project_settings.h"

namespace {

enum JoltJointWorldNode : int {
JOLT_JOINT_WORLD_NODE_A,
JOLT_JOINT_WORLD_NODE_B,
};

} // namespace

void JoltProjectSettings::register_settings() {
GLOBAL_DEF(PropertyInfo(Variant::INT, "physics/jolt_physics_3d/simulation/velocity_steps", PROPERTY_HINT_RANGE, U"2,16,or_greater"), 10);
GLOBAL_DEF(PropertyInfo(Variant::INT, "physics/jolt_physics_3d/simulation/position_steps", PROPERTY_HINT_RANGE, U"1,16,or_greater"), 2);
Expand Down Expand Up @@ -80,149 +71,53 @@ void JoltProjectSettings::register_settings() {
GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "physics/jolt_physics_3d/limits/max_bodies", PROPERTY_HINT_RANGE, U"1,10240,or_greater"), 10240);
GLOBAL_DEF(PropertyInfo(Variant::INT, "physics/jolt_physics_3d/limits/max_body_pairs", PROPERTY_HINT_RANGE, U"8,65536,or_greater"), 65536);
GLOBAL_DEF(PropertyInfo(Variant::INT, "physics/jolt_physics_3d/limits/max_contact_constraints", PROPERTY_HINT_RANGE, U"8,20480,or_greater"), 20480);
}

int JoltProjectSettings::get_simulation_velocity_steps() {
return GLOBAL_GET("physics/jolt_physics_3d/simulation/velocity_steps");
}

int JoltProjectSettings::get_simulation_position_steps() {
return GLOBAL_GET("physics/jolt_physics_3d/simulation/position_steps");
}

bool JoltProjectSettings::use_enhanced_internal_edge_removal_for_bodies() {
return GLOBAL_GET("physics/jolt_physics_3d/simulation/use_enhanced_internal_edge_removal");
}

bool JoltProjectSettings::areas_detect_static_bodies() {
return GLOBAL_GET("physics/jolt_physics_3d/simulation/areas_detect_static_bodies");
}

bool JoltProjectSettings::should_generate_all_kinematic_contacts() {
return GLOBAL_GET("physics/jolt_physics_3d/simulation/generate_all_kinematic_contacts");
}

float JoltProjectSettings::get_penetration_slop() {
return GLOBAL_GET("physics/jolt_physics_3d/simulation/penetration_slop");
}

float JoltProjectSettings::get_speculative_contact_distance() {
return GLOBAL_GET("physics/jolt_physics_3d/simulation/speculative_contact_distance");
}

float JoltProjectSettings::get_baumgarte_stabilization_factor() {
return GLOBAL_GET("physics/jolt_physics_3d/simulation/baumgarte_stabilization_factor");
}

float JoltProjectSettings::get_soft_body_point_radius() {
return GLOBAL_GET("physics/jolt_physics_3d/simulation/soft_body_point_radius");
}

float JoltProjectSettings::get_bounce_velocity_threshold() {
static const float value = GLOBAL_GET("physics/jolt_physics_3d/simulation/bounce_velocity_threshold");
return value;
}

bool JoltProjectSettings::is_sleep_allowed() {
return GLOBAL_GET("physics/jolt_physics_3d/simulation/allow_sleep");
}

float JoltProjectSettings::get_sleep_velocity_threshold() {
return GLOBAL_GET("physics/jolt_physics_3d/simulation/sleep_velocity_threshold");
}

float JoltProjectSettings::get_sleep_time_threshold() {
return GLOBAL_GET("physics/jolt_physics_3d/simulation/sleep_time_threshold");
}

float JoltProjectSettings::get_ccd_movement_threshold() {
return GLOBAL_GET("physics/jolt_physics_3d/simulation/continuous_cd_movement_threshold");
}

float JoltProjectSettings::get_ccd_max_penetration() {
return GLOBAL_GET("physics/jolt_physics_3d/simulation/continuous_cd_max_penetration");
}

bool JoltProjectSettings::is_body_pair_contact_cache_enabled() {
return GLOBAL_GET("physics/jolt_physics_3d/simulation/body_pair_contact_cache_enabled");
}

float JoltProjectSettings::get_body_pair_cache_distance_sq() {
const float value = GLOBAL_GET("physics/jolt_physics_3d/simulation/body_pair_contact_cache_distance_threshold");
return value * value;
}

float JoltProjectSettings::get_body_pair_cache_angle_cos_div2() {
return Math::cos((float)GLOBAL_GET("physics/jolt_physics_3d/simulation/body_pair_contact_cache_angle_threshold") / 2.0f);
}

bool JoltProjectSettings::use_enhanced_internal_edge_removal_for_queries() {
static const bool value = GLOBAL_GET("physics/jolt_physics_3d/queries/use_enhanced_internal_edge_removal");
return value;
}

bool JoltProjectSettings::enable_ray_cast_face_index() {
static const bool value = GLOBAL_GET("physics/jolt_physics_3d/queries/enable_ray_cast_face_index");
return value;
}

bool JoltProjectSettings::use_enhanced_internal_edge_removal_for_motion_queries() {
static const bool value = GLOBAL_GET("physics/jolt_physics_3d/motion_queries/use_enhanced_internal_edge_removal");
return value;
}

int JoltProjectSettings::get_motion_query_recovery_iterations() {
static const int value = GLOBAL_GET("physics/jolt_physics_3d/motion_queries/recovery_iterations");
return value;
}

float JoltProjectSettings::get_motion_query_recovery_amount() {
static const float value = GLOBAL_GET("physics/jolt_physics_3d/motion_queries/recovery_amount");
return value;
}

float JoltProjectSettings::get_collision_margin_fraction() {
static const float value = GLOBAL_GET("physics/jolt_physics_3d/collisions/collision_margin_fraction");
return value;
}

float JoltProjectSettings::get_active_edge_threshold() {
static const float value = Math::cos((float)GLOBAL_GET("physics/jolt_physics_3d/collisions/active_edge_threshold"));
return value;
}

bool JoltProjectSettings::use_joint_world_node_a() {
return (int)GLOBAL_GET("physics/jolt_physics_3d/joints/world_node") == JOLT_JOINT_WORLD_NODE_A;
}

int JoltProjectSettings::get_temp_memory_mib() {
return GLOBAL_GET("physics/jolt_physics_3d/limits/temporary_memory_buffer_size");
}

int64_t JoltProjectSettings::get_temp_memory_b() {
return get_temp_memory_mib() * 1024 * 1024;
}

float JoltProjectSettings::get_world_boundary_shape_size() {
return GLOBAL_GET("physics/jolt_physics_3d/limits/world_boundary_shape_size");
}

float JoltProjectSettings::get_max_linear_velocity() {
return GLOBAL_GET("physics/jolt_physics_3d/limits/max_linear_velocity");
}

float JoltProjectSettings::get_max_angular_velocity() {
return GLOBAL_GET("physics/jolt_physics_3d/limits/max_angular_velocity");
}

int JoltProjectSettings::get_max_bodies() {
return GLOBAL_GET("physics/jolt_physics_3d/limits/max_bodies");
}

int JoltProjectSettings::get_max_pairs() {
return GLOBAL_GET("physics/jolt_physics_3d/limits/max_body_pairs");
}

int JoltProjectSettings::get_max_contact_constraints() {
return GLOBAL_GET("physics/jolt_physics_3d/limits/max_contact_constraints");
read_settings();

ProjectSettings::get_singleton()->connect("settings_changed", callable_mp_static(JoltProjectSettings::read_settings));
}

void JoltProjectSettings::read_settings() {
simulation_velocity_steps = GLOBAL_GET("physics/jolt_physics_3d/simulation/velocity_steps");
simulation_position_steps = GLOBAL_GET("physics/jolt_physics_3d/simulation/position_steps");
use_enhanced_internal_edge_removal_for_bodies = GLOBAL_GET("physics/jolt_physics_3d/simulation/use_enhanced_internal_edge_removal");
areas_detect_static_bodies = GLOBAL_GET("physics/jolt_physics_3d/simulation/areas_detect_static_bodies");
generate_all_kinematic_contacts = GLOBAL_GET("physics/jolt_physics_3d/simulation/generate_all_kinematic_contacts");
penetration_slop = GLOBAL_GET("physics/jolt_physics_3d/simulation/penetration_slop");
speculative_contact_distance = GLOBAL_GET("physics/jolt_physics_3d/simulation/speculative_contact_distance");
baumgarte_stabilization_factor = GLOBAL_GET("physics/jolt_physics_3d/simulation/baumgarte_stabilization_factor");
soft_body_point_radius = GLOBAL_GET("physics/jolt_physics_3d/simulation/soft_body_point_radius");
bounce_velocity_threshold = GLOBAL_GET("physics/jolt_physics_3d/simulation/bounce_velocity_threshold");
sleep_allowed = GLOBAL_GET("physics/jolt_physics_3d/simulation/allow_sleep");
sleep_velocity_threshold = GLOBAL_GET("physics/jolt_physics_3d/simulation/sleep_velocity_threshold");
sleep_time_threshold = GLOBAL_GET("physics/jolt_physics_3d/simulation/sleep_time_threshold");
ccd_movement_threshold = GLOBAL_GET("physics/jolt_physics_3d/simulation/continuous_cd_movement_threshold");
ccd_max_penetration = GLOBAL_GET("physics/jolt_physics_3d/simulation/continuous_cd_max_penetration");
body_pair_contact_cache_enabled = GLOBAL_GET("physics/jolt_physics_3d/simulation/body_pair_contact_cache_enabled");
float body_pair_cache_distance = GLOBAL_GET("physics/jolt_physics_3d/simulation/body_pair_contact_cache_distance_threshold");
body_pair_cache_distance_sq = body_pair_cache_distance * body_pair_cache_distance;
float body_pair_cache_angle = GLOBAL_GET("physics/jolt_physics_3d/simulation/body_pair_contact_cache_angle_threshold");
body_pair_cache_angle_cos_div2 = Math::cos(body_pair_cache_angle / 2.0f);

use_enhanced_internal_edge_removal_for_queries = GLOBAL_GET("physics/jolt_physics_3d/queries/use_enhanced_internal_edge_removal");
enable_ray_cast_face_index = GLOBAL_GET("physics/jolt_physics_3d/queries/enable_ray_cast_face_index");

use_enhanced_internal_edge_removal_for_motion_queries = GLOBAL_GET("physics/jolt_physics_3d/motion_queries/use_enhanced_internal_edge_removal");
motion_query_recovery_iterations = GLOBAL_GET("physics/jolt_physics_3d/motion_queries/recovery_iterations");
motion_query_recovery_amount = GLOBAL_GET("physics/jolt_physics_3d/motion_queries/recovery_amount");

collision_margin_fraction = GLOBAL_GET("physics/jolt_physics_3d/collisions/collision_margin_fraction");
float active_edge_threshold = GLOBAL_GET("physics/jolt_physics_3d/collisions/active_edge_threshold");
active_edge_threshold_cos = Math::cos(active_edge_threshold);

joint_world_node = (JoltJointWorldNode)(int)GLOBAL_GET("physics/jolt_physics_3d/joints/world_node");

temp_memory_mib = GLOBAL_GET("physics/jolt_physics_3d/limits/temporary_memory_buffer_size");
temp_memory_b = temp_memory_mib * 1024 * 1024;
world_boundary_shape_size = GLOBAL_GET("physics/jolt_physics_3d/limits/world_boundary_shape_size");
max_linear_velocity = GLOBAL_GET("physics/jolt_physics_3d/limits/max_linear_velocity");
max_angular_velocity = GLOBAL_GET("physics/jolt_physics_3d/limits/max_angular_velocity");
max_bodies = GLOBAL_GET("physics/jolt_physics_3d/limits/max_bodies");
max_body_pairs = GLOBAL_GET("physics/jolt_physics_3d/limits/max_body_pairs");
max_contact_constraints = GLOBAL_GET("physics/jolt_physics_3d/limits/max_contact_constraints");
}
Loading