Skip to content

Commit

Permalink
Move Godot Physics 2D into a module; add dummy 2D physics server
Browse files Browse the repository at this point in the history
If the module is enabled (default), 2D physics works as it did before.

If the module is disabled and no other 2D physics server is registered
(via a module or GDExtension), then we fall back to a dummy
implementation which effectively disables 2D physics functionality (and
a warning is printed).
  • Loading branch information
rburing committed Aug 7, 2024
1 parent 33fe10c commit 1842267
Show file tree
Hide file tree
Showing 45 changed files with 481 additions and 28 deletions.
2 changes: 1 addition & 1 deletion COPYRIGHT.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Copyright: 2001, Robert Penner
2007-2014, Juan Linietsky, Ariel Manzur
License: Expat

Files: ./servers/physics_2d/godot_joints_2d.cpp
Files: ./modules/godot_physics_2d/godot_joints_2d.cpp
Comment: Chipmunk2D Joint Constraints
Copyright: 2007, Scott Lembcke
License: Expat
Expand Down
12 changes: 11 additions & 1 deletion main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@
// 2D
#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 @@ -330,7 +332,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("No PhysicsServer2D implementation was found; 2D physics functionality will be disabled.");
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;
}
}
38 changes: 38 additions & 0 deletions modules/godot_physics_2d/register_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**************************************************************************/
/* 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/csg"])
process_folder(["modules/gdscript"])
Expand All @@ -348,7 +349,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/physics_3d"])
process_folder(["servers/physics_3d/joints"])
process_folder(["servers/audio"])
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"]:
SConscript("physics_3d/SCsub")
env.add_source_files(env.servers_sources, "physics_server_3d.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 1842267

Please sign in to comment.