Skip to content

Commit

Permalink
Merge pull request #95261 from rburing/2d_physics_module
Browse files Browse the repository at this point in the history
Move Godot Physics 2D into a module; add dummy 2D physics server
  • Loading branch information
akien-mga committed Sep 24, 2024
2 parents 6bea41d + 7c4c4b9 commit 39115be
Show file tree
Hide file tree
Showing 47 changed files with 497 additions and 29 deletions.
10 changes: 5 additions & 5 deletions COPYRIGHT.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ Copyright: 2011, Ole Kniemeyer, MAXON, www.maxon.net
2007-2014, Juan Linietsky, Ariel Manzur
License: Expat and Zlib

Files: ./modules/godot_physics_2d/godot_joints_2d.cpp
Comment: Chipmunk2D Joint Constraints
Copyright: 2007, Scott Lembcke
License: Expat

Files: ./modules/godot_physics_3d/gjk_epa.cpp
./modules/godot_physics_3d/joints/godot_generic_6dof_joint_3d.cpp
./modules/godot_physics_3d/joints/godot_generic_6dof_joint_3d.h
Expand Down Expand Up @@ -126,11 +131,6 @@ Copyright: 2001, Robert Penner
2007-2014, Juan Linietsky, Ariel Manzur
License: Expat

Files: ./servers/physics_2d/godot_joints_2d.cpp
Comment: Chipmunk2D Joint Constraints
Copyright: 2007, Scott Lembcke
License: Expat

Files: ./servers/rendering/renderer_rd/shaders/ss_effects_downsample.glsl
./servers/rendering/renderer_rd/shaders/ssao_blur.glsl
./servers/rendering/renderer_rd/shaders/ssao_importance_map.glsl
Expand Down
1 change: 1 addition & 0 deletions doc/classes/ProjectSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2215,6 +2215,7 @@
<member name="physics/2d/physics_engine" type="String" setter="" getter="" default="&quot;DEFAULT&quot;">
Sets which physics engine to use for 2D physics.
"DEFAULT" and "GodotPhysics2D" are the same, as there is currently no alternative 2D physics server implemented.
"Dummy" is a 2D physics server that does nothing and returns only dummy values, effectively disabling all 2D physics functionality.
</member>
<member name="physics/2d/run_on_separate_thread" type="bool" setter="" getter="" default="false">
If [code]true[/code], the 2D physics server runs on a separate thread, making better use of multi-core CPUs. If [code]false[/code], the 2D physics server runs on the main thread. Running the physics server on a separate thread can increase performance, but restricts API access to only physics process.
Expand Down
11 changes: 10 additions & 1 deletion main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
#include "servers/navigation_server_2d.h"
#include "servers/navigation_server_2d_dummy.h"
#include "servers/physics_server_2d.h"
#include "servers/physics_server_2d_dummy.h"

#ifndef _3D_DISABLED
#include "servers/physics_server_3d.h"
Expand Down Expand Up @@ -340,7 +341,15 @@ void initialize_physics() {
// Physics server not found, Use the default physics
physics_server_2d = PhysicsServer2DManager::get_singleton()->new_default_server();
}
ERR_FAIL_NULL(physics_server_2d);

// Fall back to dummy if no default server has been registered.
if (!physics_server_2d) {
WARN_PRINT(vformat("Falling back to dummy PhysicsServer2D; 2D physics functionality will be disabled. If this is intended, set the %s project setting to Dummy.", PhysicsServer2DManager::setting_property_name));
physics_server_2d = memnew(PhysicsServer2DDummy);
}

// Should be impossible, but make sure it's not null.
ERR_FAIL_NULL_MSG(physics_server_2d, "Failed to initialize PhysicsServer2D.");
physics_server_2d->init();
}

Expand Down
5 changes: 5 additions & 0 deletions modules/godot_physics_2d/SCsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python

Import('env')

env.add_source_files(env.modules_sources, "*.cpp")
6 changes: 6 additions & 0 deletions modules/godot_physics_2d/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def can_build(env, platform):
return True


def configure(env):
pass
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
61 changes: 61 additions & 0 deletions modules/godot_physics_2d/register_types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**************************************************************************/
/* register_types.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "register_types.h"

#include "godot_physics_server_2d.h"
#include "servers/physics_server_2d.h"
#include "servers/physics_server_2d_wrap_mt.h"

static PhysicsServer2D *_createGodotPhysics2DCallback() {
#ifdef THREADS_ENABLED
bool using_threads = GLOBAL_GET("physics/2d/run_on_separate_thread");
#else
bool using_threads = false;
#endif

PhysicsServer2D *physics_server_2d = memnew(GodotPhysicsServer2D(using_threads));

return memnew(PhysicsServer2DWrapMT(physics_server_2d, using_threads));
}

void initialize_godot_physics_2d_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SERVERS) {
return;
}
PhysicsServer2DManager::get_singleton()->register_server("GodotPhysics2D", callable_mp_static(_createGodotPhysics2DCallback));
PhysicsServer2DManager::get_singleton()->set_default_server("GodotPhysics2D");
}

void uninitialize_godot_physics_2d_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SERVERS) {
return;
}
}
39 changes: 39 additions & 0 deletions modules/godot_physics_2d/register_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**************************************************************************/
/* register_types.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef GODOT_PHYSICS_2D_REGISTER_TYPES_H
#define GODOT_PHYSICS_2D_REGISTER_TYPES_H

#include "modules/register_module_types.h"

void initialize_godot_physics_2d_module(ModuleInitializationLevel p_level);
void uninitialize_godot_physics_2d_module(ModuleInitializationLevel p_level);

#endif // GODOT_PHYSICS_2D_REGISTER_TYPES_H
2 changes: 1 addition & 1 deletion scene/2d/physics/shape_cast_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "scene/2d/physics/collision_object_2d.h"
#include "scene/2d/physics/physics_body_2d.h"
#include "scene/resources/2d/circle_shape_2d.h"
#include "servers/physics_2d/godot_physics_server_2d.h"
#include "servers/physics_server_2d.h"

void ShapeCast2D::set_target_position(const Vector2 &p_point) {
target_position = p_point;
Expand Down
2 changes: 1 addition & 1 deletion scu_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ def generate_scu_files(max_includes_per_scu):
process_folder(["modules/openxr"], ["register_types"])
process_folder(["modules/openxr/action_map"])
process_folder(["modules/openxr/editor"])
process_folder(["modules/godot_physics_2d"])
process_folder(["modules/godot_physics_3d"])
process_folder(["modules/godot_physics_3d/joints"])

Expand Down Expand Up @@ -350,7 +351,6 @@ def generate_scu_files(max_includes_per_scu):
process_folder(["servers/rendering/renderer_rd/effects"])
process_folder(["servers/rendering/renderer_rd/environment"])
process_folder(["servers/rendering/renderer_rd/storage_rd"])
process_folder(["servers/physics_2d"])
process_folder(["servers/audio"])
process_folder(["servers/audio/effects"])

Expand Down
2 changes: 0 additions & 2 deletions servers/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ SConscript("navigation/SCsub")
SConscript("rendering/SCsub")
SConscript("text/SCsub")

SConscript("physics_2d/SCsub")

if not env["disable_3d"]:
env.add_source_files(env.servers_sources, "physics_server_3d.cpp")
env.add_source_files(env.servers_sources, "physics_server_3d_wrap_mt.cpp")
Expand Down
5 changes: 0 additions & 5 deletions servers/physics_2d/SCsub

This file was deleted.

4 changes: 3 additions & 1 deletion servers/physics_server_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,9 @@ String PhysicsServer2DManager::get_server_name(int p_id) {
}

PhysicsServer2D *PhysicsServer2DManager::new_default_server() {
ERR_FAIL_COND_V(default_server_id == -1, nullptr);
if (default_server_id == -1) {
return nullptr;
}
Variant ret;
Callable::CallError ce;
physics_2d_servers[default_server_id].create_callback.callp(nullptr, 0, ret, ce);
Expand Down
Loading

0 comments on commit 39115be

Please sign in to comment.