Skip to content

Commit

Permalink
Add function to get number of active objects (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ughuuu authored Oct 30, 2023
1 parent 249280e commit 2d9a7dd
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/web_builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
rust_extra_flags: ${{ matrix.arch.rust_extra_flags }}
platform: web
precision: ${{ matrix.precision }}
features: ${{ matrix.features }},wasm-bindgen
features: ${{ matrix.features }}
rust_env_flags: "-C target-feature=+atomics,+bulk-memory"

- name: Upload ${{ matrix.arch.arch }} ${{ matrix.precision }} ${{ matrix.features }}
Expand Down
50 changes: 36 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,14 @@

A 2d [rapier](https://github.com/dimforge/rapier) physics server for [Godot Engine](https://github.com/godotengine/godot), implemented as a GDExtension.

## Supported Platforms
## Table of Contents

- Windows (x86_64, x86_32)
- macOS (x86-64 + arm64 Universal)
- Linux (x86_64)
- Android (x86_64, arm64)
- iOS (arm64) without signing

# Features

- Single and double float precision build
- SIMD (Single instruction, multiple data) build
- Cross-platform determinism (assuming the rest of your code is also deterministic)

[More on different rapier features](https://rapier.rs/docs/user_guides/rust/getting_started)
1. [Limitations](#limitations)
2. [Supported Platforms](#supported-platforms)
3. [Installation](#installation)
4. [Features](#features)
5. [Comparison](#comparison)
6. [License](#license)

# Limitations

Expand All @@ -47,6 +40,14 @@ A 2d [rapier](https://github.com/dimforge/rapier) physics server for [Godot Engi
- Shape skewing missing.
- Shape Cast Margin isn't supported.

# Supported Platforms

- Windows (x86_64, x86_32)
- macOS (x86-64 + arm64 Universal)
- Linux (x86_64)
- Android (x86_64, arm64)
- iOS (arm64) without signing

# Installation

- Automatic (Recommended): Download the plugin from the official [Godot Asset Store](https://godotengine.org/asset-library/asset/2267) using the `AssetLib` tab in Godot.
Expand All @@ -57,6 +58,27 @@ A 2d [rapier](https://github.com/dimforge/rapier) physics server for [Godot Engi

After installing, go to `Advanced Settings` -> `Physics` -> `2D`. Change `Physics Engine` to `Rapier2D`.

Video Tutorial:

[![Tutorial](https://img.youtube.com/vi/KgKWAZ49T9E/0.jpg)](https://www.youtube.com/watch?v=KgKWAZ49T9E)

# Features

- Single and double float precision build
- SIMD (Single instruction, multiple data) build
- Cross-platform determinism (assuming the rest of your code is also deterministic)

[More on different rapier features](https://rapier.rs/docs/user_guides/rust/getting_started)

# Comparison

Watch a comparison to Godot Physics 2D and [Box2D](https://github.com/appsinacup/godot-box-2d) physics plugin:

[![Comparison](https://img.youtube.com/vi/wgUiZ7E19eM/0.jpg)](https://www.youtube.com/watch?v=wgUiZ7E19eM)

Or read about it on [appsinacup.com/godot-physics-vs-box2d-vs-rapier2d](https://appsinacup.com/godot-physics-vs-box2d-vs-rapier2d/)


# Roadmap

- Fix all other issues from Limitations.
Expand Down
2 changes: 1 addition & 1 deletion src/rapier2d-wrapper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ double = []
enhanced-determinism = ["rapier2d/enhanced-determinism", "rapier2d-f64/enhanced-determinism"]
simd-stable = ["rapier2d/simd-stable", "rapier2d-f64/simd-stable"]
simd-nightly = ["rapier2d/simd-nightly", "rapier2d-f64/simd-nightly"]
wasm-bindgen = ["rapier2d/wasm-bindgen", "rapier2d-f64/wasm-bindgen"]
# wasm-bindgen = ["rapier2d/wasm-bindgen", "rapier2d-f64/wasm-bindgen"]
parallel = ["rapier2d/parallel", "rapier2d-f64/parallel"]

[dependencies]
Expand Down
2 changes: 2 additions & 0 deletions src/rapier2d-wrapper/includes/rapier2d_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ Handle world_create(const WorldSettings *settings);

void world_destroy(Handle world_handle);

size_t world_get_active_objects_count(Handle world_handle);

void world_set_active_body_callback(Handle world_handle, ActiveBodyCallback callback);

void world_set_body_collision_filter_callback(Handle world_handle,
Expand Down
7 changes: 7 additions & 0 deletions src/rapier2d-wrapper/src/physics_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,10 @@ pub extern "C" fn world_set_contact_point_callback(world_handle : Handle, callba
let physics_world = physics_engine.get_world(world_handle);
physics_world.contact_point_callback = callback;
}

#[no_mangle]
pub extern "C" fn world_get_active_objects_count(world_handle : Handle) -> usize {
let mut physics_engine = SINGLETON.lock().unwrap();
let physics_world = physics_engine.get_world(world_handle);
return physics_world.island_manager.active_dynamic_bodies().len();
}
16 changes: 6 additions & 10 deletions src/servers/rapier_physics_server_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1238,20 +1238,16 @@ void RapierPhysicsServer2D::_step(double p_step) {

++frame;

island_count = 0;
active_objects = 0;
collision_pairs = 0;
for (auto const &iterator : active_spaces) {
RapierSpace2D *space = iterator.value;
space->step(p_step);
island_count += space->get_island_count();
active_objects += space->get_active_objects();
collision_pairs += space->get_collision_pairs();
}

// island_count = 0;
// active_objects = 0;
// collision_pairs = 0;
// for (const RapierSpace2D *space : active_spaces) {
// stepper->step(space, p_step);
// island_count += space->get_island_count();
// active_objects += space->get_active_objects();
// collision_pairs += space->get_collision_pairs();
// }
}

void RapierPhysicsServer2D::_sync() {
Expand Down
1 change: 1 addition & 0 deletions src/spaces/rapier_space_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ void RapierSpace2D::step(real_t p_step) {
body_iterator = body_iterator->next();
body->on_update_active();
}
active_objects = rapier2d::world_get_active_objects_count(handle);
}

// Returns true to ignore the collider
Expand Down
1 change: 0 additions & 1 deletion src/spaces/rapier_space_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ class RapierSpace2D {
void set_island_count(int p_island_count) { island_count = p_island_count; }
int get_island_count() const { return island_count; }

void set_active_objects(int p_active_objects) { active_objects = p_active_objects; }
int get_active_objects() const { return active_objects; }

int get_collision_pairs() const { return collision_pairs; }
Expand Down

0 comments on commit 2d9a7dd

Please sign in to comment.