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

update to latest rapier version, 0.18. Add spring joint, fix disable collision of joints, polygon issue and world boundary, wrong area callback #38

Merged
merged 14 commits into from
Mar 28, 2024
7 changes: 1 addition & 6 deletions .github/actions/build/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ inputs:
rust_env_flags:
required: false
default: ''
descriptio: RUSTFLAGS env var.
description: RUSTFLAGS env var.
arch:
default: ''
description: Target architecture.
Expand Down Expand Up @@ -49,11 +49,6 @@ runs:
echo v${{ steps.version.outputs.version }} > bin/addons/godot-rapier2d/VERSION.txt
- name: Setup python and scons
uses: ./.github/actions/deps
- name: Lint
shell: sh
run:
./scripts/clang-format.sh
./scripts/clang-tidy.sh
- name: Build Godot Cpp
shell: sh
env:
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
</div>

<p align="center">
<a href="https://github.com/appsinacup/godot-rapier2d/actions/workflows/runner.yml">
<img src="https://github.com/appsinacup/godot-rapier2d/actions/workflows/runner.yml/badge.svg?branch=main"
<a href="https://github.com/appsinacup/godot-rapier-2d/actions/workflows/runner.yml">
<img src="https://github.com/appsinacup/godot-rapier-2d/actions/workflows/runner.yml/badge.svg?branch=main"
alt="Godot Rapier2D Build"></a>
<a href="https://github.com/dimforge/rapier/releases/tag/v0.17.2" alt="Rapier2D Version">
<img src="https://img.shields.io/badge/Rapier2D-v0.17.2-%23478cbf?logoColor=white" /></a>
<a href="https://github.com/dimforge/rapier/releases/tag/v0.18.0" alt="Rapier2D Version">
<img src="https://img.shields.io/badge/Rapier2D-v0.18.0-%23478cbf?logoColor=white" /></a>
<a href="https://github.com/godotengine/godot-cpp" alt="Godot Version">
<img src="https://img.shields.io/badge/Godot-v4.2-%23478cbf?logo=godot-engine&logoColor=white" /></a>
<a href="https://github.com/appsinacup/godot-rapier2d/graphs/contributors" alt="Contributors">
<img src="https://img.shields.io/github/contributors/appsinacup/godot-rapier2d" /></a>
<a href="https://github.com/appsinacup/godot-rapier2d/pulse" alt="Activity">
<img src="https://img.shields.io/github/commit-activity/m/appsinacup/godot-rapier2d" /></a>
<a href="https://github.com/appsinacup/godot-rapier-2d/graphs/contributors" alt="Contributors">
<img src="https://img.shields.io/github/contributors/appsinacup/godot-rapier-2d" /></a>
<a href="https://github.com/appsinacup/godot-rapier-2d/pulse" alt="Activity">
<img src="https://img.shields.io/github/commit-activity/m/appsinacup/godot-rapier-2d" /></a>
<a href="https://discord.gg/56dMud8HYn">
<img src="https://img.shields.io/discord/1138836561102897172?logo=discord"
alt="Chat on Discord"></a>
Expand All @@ -40,8 +40,8 @@ A 2d [rapier](https://github.com/dimforge/rapier) physics server for [Godot Engi
# Limitations

- SeparationRay2D missing.
- DampedSpringJoint2D missing.
- Shape skew missing.
- Web exports not working [issues/23](https://github.com/appsinacup/godot-rapier-2d/issues/23)

# Supported Platforms

Expand All @@ -50,7 +50,7 @@ A 2d [rapier](https://github.com/dimforge/rapier) physics server for [Godot Engi
- Linux (x86_64)
- Android (x86_64, arm64)
- iOS (arm64)
- Web (wasm32)
- Web (wasm32) * Not working

# Installation

Expand Down
2 changes: 1 addition & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
scons arch=arm64
scons arch=arm64 target=template_debug
rm -rf Godot-Physics-Tests/addons
cp -rf bin/addons Godot-Physics-Tests/addons

9 changes: 5 additions & 4 deletions src/bodies/rapier_body_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ void RapierBody2D::_apply_mass_properties(bool force_update) {

void RapierBody2D::update_mass_properties(bool force_update) {
mass_properties_update_list.remove_from_list();

ERR_FAIL_COND(mode < PhysicsServer2D::BODY_MODE_RIGID);
if (mode < PhysicsServer2D::BODY_MODE_RIGID) {
return;
}

real_t total_area = 0;
for (int i = 0; i < get_shape_count(); i++) {
Expand All @@ -64,7 +65,7 @@ void RapierBody2D::update_mass_properties(bool force_update) {
const RapierShape2D *shape = get_shape(i);

real_t shape_area = shape->get_aabb().get_area();
if (shape_area == 0.0) {
if (shape_area == 0.0 || mass == 0.0) {
continue;
}

Expand All @@ -90,7 +91,7 @@ void RapierBody2D::update_mass_properties(bool force_update) {
const RapierShape2D *shape = get_shape(i);

real_t shape_area = shape->get_aabb().get_area();
if (shape_area == 0.0) {
if (shape_area == 0.0 || mass == 0.0) {
continue;
}

Expand Down
3 changes: 3 additions & 0 deletions src/bodies/rapier_body_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ class RapierBody2D : public RapierCollisionObject2D {
contacts.resize(p_size);
contact_count = 0;
}
_FORCE_INLINE_ void reset_contact_count() {
contact_count = 0;
}
_FORCE_INLINE_ int get_max_contacts_reported() const { return contacts.size(); }
_FORCE_INLINE_ bool can_report_contacts() const { return !contacts.is_empty(); }
_FORCE_INLINE_ void add_contact(const Vector2 &p_local_pos, const Vector2 &p_local_normal, real_t p_depth, int p_local_shape, const Vector2 &p_collider_pos, int p_collider_shape, ObjectID p_collider_instance_id, const RID &p_collider, const Vector2 &p_collider_velocity_at_pos, const Vector2 &p_impulse);
Expand Down
16 changes: 11 additions & 5 deletions src/joints/rapier_damped_spring_joint_2d.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "rapier_damped_spring_joint_2d.h"
#include "../spaces/rapier_space_2d.h"

void RapierDampedSpringJoint2D::set_param(PhysicsServer2D::DampedSpringParam p_param, real_t p_value) {
switch (p_param) {
Expand All @@ -12,6 +13,7 @@ void RapierDampedSpringJoint2D::set_param(PhysicsServer2D::DampedSpringParam p_p
stiffness = p_value;
} break;
}
rapier2d::joint_change_sprint_params(space_handle, handle, rest_length, damping, stiffness);
}

real_t RapierDampedSpringJoint2D::get_param(PhysicsServer2D::DampedSpringParam p_param) const {
Expand All @@ -37,10 +39,14 @@ RapierDampedSpringJoint2D::RapierDampedSpringJoint2D(const Vector2 &p_anchor_a,

rest_length = p_anchor_a.distance_to(p_anchor_b);

// TODO: create rapier joint when available
// See https://github.com/dimforge/rapier/issues/241
ERR_FAIL_MSG("Spring joints not supported for now");
rapier2d::Vector rapier_anchor_A = { anchor_A.x, anchor_A.y };
rapier2d::Vector rapier_anchor_B = { anchor_B.x, anchor_B.y };

//A->add_constraint(this, 0);
//B->add_constraint(this, 1);
ERR_FAIL_COND(!p_body_a->get_space());
ERR_FAIL_COND(p_body_a->get_space() != p_body_b->get_space());
space_handle = p_body_a->get_space()->get_handle();
ERR_FAIL_COND(!rapier2d::is_handle_valid(space_handle));

handle = rapier2d::joint_create_spring(space_handle, p_body_a->get_body_handle(), p_body_b->get_body_handle(), &rapier_anchor_A, &rapier_anchor_B, stiffness, damping, rest_length, is_disabled_collisions_between_bodies());
ERR_FAIL_COND(!rapier2d::is_handle_valid(handle));
}
2 changes: 1 addition & 1 deletion src/joints/rapier_groove_joint_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ RapierGrooveJoint2D::RapierGrooveJoint2D(const Vector2 &p_a_groove1, const Vecto
space_handle = p_body_a->get_space()->get_handle();
ERR_FAIL_COND(!rapier2d::is_handle_valid(space_handle));

handle = rapier2d::joint_create_prismatic(space_handle, p_body_a->get_body_handle(), p_body_b->get_body_handle(), &rapier_axis, &rapier_anchor_A, &rapier_anchor_B, &rapier_limits);
handle = rapier2d::joint_create_prismatic(space_handle, p_body_a->get_body_handle(), p_body_b->get_body_handle(), &rapier_axis, &rapier_anchor_A, &rapier_anchor_B, &rapier_limits, is_disabled_collisions_between_bodies());
ERR_FAIL_COND(!rapier2d::is_handle_valid(handle));

//A->add_constraint(this, 0);
Expand Down
8 changes: 8 additions & 0 deletions src/joints/rapier_joint_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ void RapierJoint2D::copy_settings_from(RapierJoint2D *p_joint) {
disable_collisions_between_bodies(p_joint->is_disabled_collisions_between_bodies());
}

void RapierJoint2D::disable_collisions_between_bodies(const bool p_disabled) {
disabled_collisions_between_bodies = p_disabled;
if (rapier2d::is_handle_valid(handle)) {
// Joint not yet created, when it will be created it will have disable collision flag set
rapier2d::joint_change_disable_collision(space_handle, handle, is_disabled_collisions_between_bodies());
}
}

RapierJoint2D::~RapierJoint2D() {
if (rapier2d::is_handle_valid(handle)) {
ERR_FAIL_COND(!rapier2d::is_handle_valid(space_handle));
Expand Down
2 changes: 1 addition & 1 deletion src/joints/rapier_joint_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RapierJoint2D {
_FORCE_INLINE_ void set_rid(const RID &p_rid) { rid = p_rid; }
_FORCE_INLINE_ RID get_rid() const { return rid; }

_FORCE_INLINE_ void disable_collisions_between_bodies(const bool p_disabled) { disabled_collisions_between_bodies = p_disabled; }
void disable_collisions_between_bodies(const bool p_disabled);
_FORCE_INLINE_ bool is_disabled_collisions_between_bodies() const { return disabled_collisions_between_bodies; }

_FORCE_INLINE_ void set_max_force(real_t p_force) { max_force = p_force; }
Expand Down
2 changes: 1 addition & 1 deletion src/joints/rapier_pin_joint_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ RapierPinJoint2D::RapierPinJoint2D(const Vector2 &p_pos, RapierBody2D *p_body_a,
ERR_FAIL_COND(p_body_a->get_space() != p_body_b->get_space());
space_handle = p_body_a->get_space()->get_handle();
ERR_FAIL_COND(!rapier2d::is_handle_valid(space_handle));
handle = rapier2d::joint_create_revolute(space_handle, p_body_a->get_body_handle(), p_body_b->get_body_handle(), &rapier_anchor_A, &rapier_anchor_B, angular_limit_lower, angular_limit_upper, angular_limit_enabled, motor_target_velocity, motor_enabled);
handle = rapier2d::joint_create_revolute(space_handle, p_body_a->get_body_handle(), p_body_b->get_body_handle(), &rapier_anchor_A, &rapier_anchor_B, angular_limit_lower, angular_limit_upper, angular_limit_enabled, motor_target_velocity, motor_enabled, is_disabled_collisions_between_bodies());
ERR_FAIL_COND(!rapier2d::is_handle_valid(handle));
}
4 changes: 2 additions & 2 deletions src/rapier2d-wrapper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ parallel = ["rapier2d/parallel", "rapier2d-f64/parallel"]
# rapier2d = { features = [ "simd-stable"] }

# This changes the parry contacts to work even in some weird cases where in main branch it would return None
rapier2d = {git = "https://github.com/appsinacup/rapier", branch = "possible-fix-for-contacts"}
rapier2d-f64 = {git = "https://github.com/appsinacup/rapier", branch = "possible-fix-for-contacts"}
rapier2d = {git = "https://github.com/appsinacup/rapier", branch = "master"}
rapier2d-f64 = {git = "https://github.com/appsinacup/rapier", branch = "master"}
lazy_static = "1.4.0"

[profile.release]
Expand Down
Loading