From 9259b4adc449efa8eb5b7faaf1e23c3a3c0d5133 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Wed, 7 Jul 2021 19:39:26 +0200 Subject: [PATCH] Add a method to set the number of physics solver iterations in 3D This is only for GodotPhysics, and adds a 3D counterpart to the 2D method that was recently added. --- doc/classes/Physics2DServer.xml | 2 +- doc/classes/PhysicsServer.xml | 10 ++++++++++ modules/bullet/bullet_physics_server.cpp | 4 ++++ modules/bullet/bullet_physics_server.h | 2 ++ servers/physics/physics_server_sw.cpp | 4 ++++ servers/physics/physics_server_sw.h | 2 ++ servers/physics_2d_server.h | 2 +- servers/physics_server.cpp | 2 ++ servers/physics_server.h | 2 ++ 9 files changed, 28 insertions(+), 2 deletions(-) diff --git a/doc/classes/Physics2DServer.xml b/doc/classes/Physics2DServer.xml index d9e5d114211e..70a1cbe8b495 100644 --- a/doc/classes/Physics2DServer.xml +++ b/doc/classes/Physics2DServer.xml @@ -1009,7 +1009,7 @@ - Sets the amount of iterations for calculating velocities of colliding bodies. The greater the amount, the more accurate the collisions, but with a performance loss. + Sets the amount of iterations for calculating velocities of colliding bodies. The greater the amount of iterations, the more accurate the collisions will be. However, a greater amount of iterations requires more CPU power, which can decrease performance. The default value is [code]8[/code]. diff --git a/doc/classes/PhysicsServer.xml b/doc/classes/PhysicsServer.xml index 411ca2d62515..dccafe2a1bae 100644 --- a/doc/classes/PhysicsServer.xml +++ b/doc/classes/PhysicsServer.xml @@ -1155,6 +1155,16 @@ Activates or deactivates the 3D physics engine. + + + + + + + Sets the amount of iterations for calculating velocities of colliding bodies. The greater the amount of iterations, the more accurate the collisions will be. However, a greater amount of iterations requires more CPU power, which can decrease performance. The default value is [code]8[/code]. + [b]Note:[/b] Only has an effect when using the GodotPhysics engine, not the default Bullet physics engine. + + diff --git a/modules/bullet/bullet_physics_server.cpp b/modules/bullet/bullet_physics_server.cpp index 0796c28a3435..cdc03fc405be 100644 --- a/modules/bullet/bullet_physics_server.cpp +++ b/modules/bullet/bullet_physics_server.cpp @@ -1546,6 +1546,10 @@ void BulletPhysicsServer::finish() { BulletPhysicsDirectBodyState::destroySingleton(); } +void BulletPhysicsServer::set_collision_iterations(int p_iterations) { + WARN_PRINT("Changing the number of 3D physics collision iterations is only supported when using GodotPhysics, not Bullet."); +} + int BulletPhysicsServer::get_process_info(ProcessInfo p_info) { return 0; } diff --git a/modules/bullet/bullet_physics_server.h b/modules/bullet/bullet_physics_server.h index 7f1e461d0771..78016e62cb16 100644 --- a/modules/bullet/bullet_physics_server.h +++ b/modules/bullet/bullet_physics_server.h @@ -398,6 +398,8 @@ class BulletPhysicsServer : public PhysicsServer { virtual bool is_flushing_queries() const { return false; } + virtual void set_collision_iterations(int p_iterations); + virtual int get_process_info(ProcessInfo p_info); CollisionObjectBullet *get_collisin_object(RID p_object) const; diff --git a/servers/physics/physics_server_sw.cpp b/servers/physics/physics_server_sw.cpp index fd32000f4c53..690d5bf72e76 100644 --- a/servers/physics/physics_server_sw.cpp +++ b/servers/physics/physics_server_sw.cpp @@ -1274,6 +1274,10 @@ void PhysicsServerSW::set_active(bool p_active) { active = p_active; }; +void PhysicsServerSW::set_collision_iterations(int p_iterations) { + iterations = p_iterations; +}; + void PhysicsServerSW::init() { last_step = 0.001; iterations = 8; // 8? diff --git a/servers/physics/physics_server_sw.h b/servers/physics/physics_server_sw.h index 98fc348e8cd4..fdead0500100 100644 --- a/servers/physics/physics_server_sw.h +++ b/servers/physics/physics_server_sw.h @@ -363,6 +363,8 @@ class PhysicsServerSW : public PhysicsServer { virtual void flush_queries(); virtual void finish(); + virtual void set_collision_iterations(int p_iterations); + virtual bool is_flushing_queries() const { return flushing_queries; } int get_process_info(ProcessInfo p_info); diff --git a/servers/physics_2d_server.h b/servers/physics_2d_server.h index 8a2fd56a9e9d..6794b432a74f 100644 --- a/servers/physics_2d_server.h +++ b/servers/physics_2d_server.h @@ -555,7 +555,7 @@ class Physics2DServer : public Object { virtual bool is_flushing_queries() const = 0; - virtual void set_collision_iterations(int iterations) = 0; + virtual void set_collision_iterations(int p_iterations) = 0; enum ProcessInfo { diff --git a/servers/physics_server.cpp b/servers/physics_server.cpp index 695389e370d3..57193d15dc1a 100644 --- a/servers/physics_server.cpp +++ b/servers/physics_server.cpp @@ -671,6 +671,8 @@ void PhysicsServer::_bind_methods() { ClassDB::bind_method(D_METHOD("set_active", "active"), &PhysicsServer::set_active); + ClassDB::bind_method(D_METHOD("set_collision_iterations", "iterations"), &PhysicsServer::set_collision_iterations); + ClassDB::bind_method(D_METHOD("get_process_info", "process_info"), &PhysicsServer::get_process_info); BIND_ENUM_CONSTANT(SHAPE_PLANE); diff --git a/servers/physics_server.h b/servers/physics_server.h index 7d276f7c1ed8..c7641bf38c6a 100644 --- a/servers/physics_server.h +++ b/servers/physics_server.h @@ -726,6 +726,8 @@ class PhysicsServer : public Object { virtual bool is_flushing_queries() const = 0; + virtual void set_collision_iterations(int p_iterations) = 0; + enum ProcessInfo { INFO_ACTIVE_OBJECTS,