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

Fix signal exit bug after collider removed #303

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .github/workflows/module_builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ jobs:
with:
name: static-rapier-${{ matrix.dimensions }}-${{ matrix.precision }}-${{ matrix.features }}-module
path: |
module
module${{ matrix.dimensions }}
if-no-files-found: error
9 changes: 0 additions & 9 deletions module/register_types.h

This file was deleted.

6 changes: 3 additions & 3 deletions module/SCsub → module2d/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ def get_sufix():

# Libraries
if env["platform"] == "macos" or env["platform"] == "ios":
base_path = "#modules/rapier/bin/addons/my_library/bin/libmy_library{}.framework".format(get_sufix())
base_path = "#modules/rapier_2d/bin/addons/rapier_2d/bin/librapier_2d{}.framework".format(get_sufix())
base_file = "libgodot_riscv{}".format(get_sufix())
else:
base_path = "#modules/rapier/bin/addons/my_library/bin"
base_path = "#modules/rapier_2d/bin/addons/rapier_2d/bin"
base_file = "libgodot_riscv{}".format(get_sufix())

env.Append(LIBS=[base_file])
env.Append(LIBPATH=[base_path])

# Godot-cpp
base_path = "#modules/rapier/ext/godot-cpp/bin"
base_path = "#modules/rapier_2d/ext/godot-cpp/bin"
base_file = "libgodot-cpp{}".format(get_sufix())

env.Append(LIBS=[base_file])
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion module/register_types.cpp → module2d/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void initialize_sandbox_module(ModuleInitializationLevel p_level) {
Ref<GDExtensionStaticLibraryLoader> loader;
loader.instantiate();
loader->set_entry_funcptr((void*)&gdext_rust_init);
GDExtensionManager::get_singleton()->load_extension_with_loader("rapier", loader);
GDExtensionManager::get_singleton()->load_extension_with_loader("rapier_2d", loader);
}

void uninitialize_sandbox_module(ModuleInitializationLevel p_level) {
Expand Down
9 changes: 9 additions & 0 deletions module2d/register_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef RAPIER_2D_REGISTER_TYPES_H
#define RAPIER_2D_REGISTER_TYPES_H

#include "modules/register_module_types.h"

void initialize_rapier_2d_module(ModuleInitializationLevel p_level);
void uninitialize_rapier_2d_module(ModuleInitializationLevel p_level);

#endif // RAPIER_2D_REGISTER_TYPES_H
39 changes: 39 additions & 0 deletions module3d/SCsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python
from misc.utility.scons_hints import *

Import("env")
Import("env_modules")

def get_sufix():
suffix = ".{}.{}".format(env["platform"], env["target"])
if env.dev_build:
suffix += ".dev"
if env["precision"] == "double":
suffix += ".double"
suffix += "." + env["arch"]
if not env["threads"]:
suffix += ".nothreads"
return suffix.replace("editor", "template_release")

# Libraries
if env["platform"] == "macos" or env["platform"] == "ios":
base_path = "#modules/rapier_3d/bin/addons/rapier_3d/bin/librapier_3d{}.framework".format(get_sufix())
base_file = "libgodot_riscv{}".format(get_sufix())
else:
base_path = "#modules/rapier_3d/bin/addons/rapier_3d/bin"
base_file = "libgodot_riscv{}".format(get_sufix())

env.Append(LIBS=[base_file])
env.Append(LIBPATH=[base_path])

# Godot-cpp
base_path = "#modules/rapier_3d/ext/godot-cpp/bin"
base_file = "libgodot-cpp{}".format(get_sufix())

env.Append(LIBS=[base_file])
env.Append(LIBPATH=[base_path])

# Sources
env_gdextension = env_modules.Clone()

env_gdextension.add_source_files(env.modules_sources, "*.cpp")
6 changes: 6 additions & 0 deletions module3d/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
41 changes: 41 additions & 0 deletions module3d/gdextension_static_library_loader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "gdextension_static_library_loader.h"

#include "core/config/project_settings.h"
#include "core/io/dir_access.h"
#include "core/version.h"
#include "core/extension/gdextension.h"

Error GDExtensionStaticLibraryLoader::open_library(const String &p_path) {
library_path = p_path;
return OK;
}

Error GDExtensionStaticLibraryLoader::initialize(GDExtensionInterfaceGetProcAddress p_get_proc_address, const Ref<GDExtension> &p_extension, GDExtensionInitialization *r_initialization) {

GDExtensionInitializationFunction initialization_function = (GDExtensionInitializationFunction)entry_funcptr;
if (initialization_function == nullptr) {
}
GDExtensionBool ret = initialization_function(p_get_proc_address, p_extension.ptr(), r_initialization);

if (ret) {
return OK;
} else {
ERR_PRINT("GDExtension initialization function '" + library_path + "' returned an error.");
return FAILED;
}
}

void GDExtensionStaticLibraryLoader::close_library() {
}

bool GDExtensionStaticLibraryLoader::is_library_open() const {
return true;
}

bool GDExtensionStaticLibraryLoader::has_library_changed() const {
return false;
}

bool GDExtensionStaticLibraryLoader::library_exists() const {
return true;
}
32 changes: 32 additions & 0 deletions module3d/gdextension_static_library_loader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef GDEXTENSION_STATIC_LIBRARY_LOADER_H
#define GDEXTENSION_STATIC_LIBRARY_LOADER_H

#include <functional>

#include "core/extension/gdextension_loader.h"
#include "core/io/config_file.h"
#include "core/os/shared_object.h"

class GDExtensionStaticLibraryLoader : public GDExtensionLoader {
friend class GDExtensionManager;
friend class GDExtension;

private:
String resource_path;
void *entry_funcptr = nullptr;
String library_path;
Vector<SharedObject> library_dependencies;

HashMap<String, String> class_icon_paths;

public:
void set_entry_funcptr(void *p_entry_funcptr) { entry_funcptr = p_entry_funcptr; }
virtual Error open_library(const String &p_path) override;
virtual Error initialize(GDExtensionInterfaceGetProcAddress p_get_proc_address, const Ref<GDExtension> &p_extension, GDExtensionInitialization *r_initialization) override;
virtual void close_library() override;
virtual bool is_library_open() const override;
virtual bool has_library_changed() const override;
virtual bool library_exists() const override;
};

#endif // GDEXTENSION_STATIC_LIBRARY_LOADER_H
28 changes: 28 additions & 0 deletions module3d/register_types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "register_types.h"
#include "core/object/object.h"
#include "core/extension/gdextension_interface.h"
#include "core/extension/gdextension_manager.h"
#include "./gdextension_static_library_loader.h"
#include "core/object/ref_counted.h"

extern "C" {
GDExtensionBool gdext_rust_init(
GDExtensionInterfaceGetProcAddress p_get_proc_address,
GDExtensionClassLibraryPtr p_library,
GDExtensionInitialization *r_initialization
);
}

void initialize_sandbox_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SERVERS) {
return;
}

Ref<GDExtensionStaticLibraryLoader> loader;
loader.instantiate();
loader->set_entry_funcptr((void*)&gdext_rust_init);
GDExtensionManager::get_singleton()->load_extension_with_loader("rapier_3d", loader);
}

void uninitialize_sandbox_module(ModuleInitializationLevel p_level) {
}
9 changes: 9 additions & 0 deletions module3d/register_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef RAPIER_3D_REGISTER_TYPES_H
#define RAPIER_3D_REGISTER_TYPES_H

#include "modules/register_module_types.h"

void initialize_rapier_3d_module(ModuleInitializationLevel p_level);
void uninitialize_rapier_3d_module(ModuleInitializationLevel p_level);

#endif // RAPIER_3D_REGISTER_TYPES_H
10 changes: 9 additions & 1 deletion src/bodies/rapier_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,11 +836,19 @@ impl RapierBody {
);
}

pub fn set_max_contacts_reported(&mut self, size: i32) {
pub fn set_max_contacts_reported(
&mut self,
size: i32,
physics_engine: &mut PhysicsEngine,
physics_spaces: &mut PhysicsSpaces,
physics_ids: &PhysicsIds,
) {
self.state
.contacts
.resize(size as usize, Contact::default());
self.state.contact_count = 0;
// update all contact forces
self.recreate_shapes(physics_engine, physics_spaces, physics_ids);
}

pub fn reset_contact_count(&mut self) {
Expand Down
41 changes: 37 additions & 4 deletions src/rapier_wrapper/physics_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ pub struct PhysicsObjects {
pub collider_set: ColliderSet,
pub rigid_body_set: RigidBodySet,

pub removed_rigid_bodies_user_data: HashMap<RigidBodyHandle, UserData>,
pub removed_colliders_user_data: HashMap<ColliderHandle, UserData>,

pub handle: WorldHandle,
}
pub struct PhysicsWorld {
Expand All @@ -104,6 +107,9 @@ impl PhysicsWorld {
rigid_body_set: RigidBodySet::new(),
collider_set: ColliderSet::new(),

removed_rigid_bodies_user_data: HashMap::new(),
removed_colliders_user_data: HashMap::new(),

handle: WorldHandle::default(),
},
physics_pipeline,
Expand Down Expand Up @@ -293,6 +299,9 @@ impl PhysicsWorld {
}
}
}
// remove all the removed colliders and rigidbodies user data
self.physics_objects.removed_rigid_bodies_user_data.clear();
self.physics_objects.removed_colliders_user_data.clear();
}

pub fn insert_collider(
Expand All @@ -313,19 +322,31 @@ impl PhysicsWorld {
}

pub fn remove_collider(&mut self, collider_handle: ColliderHandle) {
self.physics_objects.collider_set.remove(
if let Some(collider) = self.physics_objects.collider_set.remove(
collider_handle,
&mut self.physics_objects.island_manager,
&mut self.physics_objects.rigid_body_set,
false,
);
) {
self.physics_objects
.removed_colliders_user_data
.insert(collider_handle, UserData::new(collider.user_data));
}
}

pub fn get_collider_user_data(&self, collider_handle: ColliderHandle) -> UserData {
let collider = self.physics_objects.collider_set.get(collider_handle);
if let Some(collider) = collider {
return UserData::new(collider.user_data);
}
// removed collider
if let Some(user_data) = self
.physics_objects
.removed_colliders_user_data
.get(&collider_handle)
{
return *user_data;
}
UserData::invalid_user_data()
}

Expand All @@ -339,21 +360,33 @@ impl PhysicsWorld {

pub fn remove_rigid_body(&mut self, body_handle: RigidBodyHandle) {
let rigid_body_handle = body_handle;
self.physics_objects.rigid_body_set.remove(
if let Some(rigid_body) = self.physics_objects.rigid_body_set.remove(
rigid_body_handle,
&mut self.physics_objects.island_manager,
&mut self.physics_objects.collider_set,
&mut self.physics_objects.impulse_joint_set,
&mut self.physics_objects.multibody_joint_set,
true,
);
) {
self.physics_objects
.removed_rigid_bodies_user_data
.insert(rigid_body_handle, UserData::new(rigid_body.user_data));
}
}

pub fn get_rigid_body_user_data(&self, rigid_body_handle: RigidBodyHandle) -> UserData {
let rigid_body = self.physics_objects.rigid_body_set.get(rigid_body_handle);
if let Some(rigid_body) = rigid_body {
return UserData::new(rigid_body.user_data);
}
// removed rigidbody
if let Some(user_data) = self
.physics_objects
.removed_rigid_bodies_user_data
.get(&rigid_body_handle)
{
return *user_data;
}
UserData::invalid_user_data()
}

Expand Down
7 changes: 6 additions & 1 deletion src/servers/rapier_physics_server_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,12 @@ impl RapierPhysicsServerImpl {
let physics_data = physics_data();
if let Some(body) = physics_data.collision_objects.get_mut(&body) {
if let Some(body) = body.get_mut_body() {
body.set_max_contacts_reported(amount);
body.set_max_contacts_reported(
amount,
&mut physics_data.physics_engine,
&mut physics_data.spaces,
&physics_data.ids,
);
}
}
}
Expand Down
Loading