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

Migrate to latest gdext. Also fix body notification issue. #179

Merged
merged 12 commits into from
Aug 4, 2024
33 changes: 11 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dim3 = []
single = []
double = []
# TODO add feature for "glam/libm"
enhanced-determinism = ["rapier2d/enhanced-determinism", "rapier2d-f64/enhanced-determinism", "rapier3d/enhanced-determinism", "rapier3d-f64/enhanced-determinism", "godot/enchanced-determinism"]
enhanced-determinism = ["rapier2d/enhanced-determinism", "rapier2d-f64/enhanced-determinism", "rapier3d/enhanced-determinism", "rapier3d-f64/enhanced-determinism"]
serde-serialize = ["serde", "hashbrown/serde", "bincode", "serde_json", "typetag", "godot/serde", "rapier2d/serde-serialize", "rapier2d-f64/serde-serialize", "rapier3d/serde-serialize", "rapier3d-f64/serde-serialize" ]
simd-stable = ["rapier2d/simd-stable", "rapier2d-f64/simd-stable", "rapier3d/simd-stable", "rapier3d-f64/simd-stable"]
simd-nightly = ["rapier2d/simd-nightly", "rapier2d-f64/simd-nightly", "rapier3d/simd-nightly", "rapier3d-f64/simd-nightly"]
Expand All @@ -33,8 +33,7 @@ double-dim3 = ["double", "dim3", "rapier3d-f64", "salva3d"]
bincode = { version = "1", optional = true }
hashbrown = { version = "0.14" }

godot = { git = "https://github.com/ughuuu/gdext", branch = "make-stuff-serializable", features=["api-4-2"] }
#godot = { git = "https://github.com/godot-rust/gdext", branch = "master", features=["api-4-2"] }
godot = { git = "https://github.com/godot-rust/gdext", branch = "master", features=["api-4-2"] }

rapier2d = { git = "https://github.com/ughuuu/rapier", branch = "custom-changes", optional = true }
rapier2d-f64 = { git = "https://github.com/ughuuu/rapier", branch = "custom-changes", optional = true}
Expand Down
1 change: 1 addition & 0 deletions scripts/build-mac-dev-2d.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
cargo fmt -- --config-path rustfmt.toml
cargo clippy --fix --allow-dirty
#cargo build --features="single-dim2,parallel,simd-stable,serde-serialize" --no-default-features
cargo build --features="single-dim2,parallel,simd-stable,serde-serialize" --no-default-features
rm bin2d/addons/godot-rapier2d/bin/libgodot_rapier.macos.framework/libgodot_rapier.macos.dylib
rm /Users/dragosdaian/Documents/Godot-Physics-Tests/addons/godot-rapier2d/bin/libgodot_rapier.macos.framework/libgodot_rapier.macos.dylib
Expand Down
2 changes: 2 additions & 0 deletions src/bodies/rapier_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::*;
derive(serde::Serialize, serde::Deserialize)
)]
struct MonitorInfo {
#[cfg_attr(feature = "serde-serialize", serde(skip, default = "invalid_rid"))]
pub rid: Rid,
pub instance_id: u64,
pub object_shape_index: u32,
Expand Down Expand Up @@ -58,6 +59,7 @@ pub struct RapierArea {
#[cfg_attr(feature = "serde-serialize", serde(skip))]
area_monitor_callback: Option<Callable>,
monitored_objects: HashMap<(ColliderHandle, ColliderHandle), MonitorInfo>,
#[cfg_attr(feature = "serde-serialize", serde(skip))]
detected_bodies: HashMap<Rid, u32>,
base: RapierCollisionObject,
}
Expand Down
17 changes: 15 additions & 2 deletions src/bodies/rapier_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct Contact {
pub collider_pos: Vector,
pub collider_shape: i32,
pub collider_instance_id: u64,
#[cfg_attr(feature = "serde-serialize", serde(skip, default = "invalid_rid"))]
pub collider: Rid,
pub local_velocity_at_pos: Vector,
pub collider_velocity_at_pos: Vector,
Expand Down Expand Up @@ -78,6 +79,7 @@ pub struct ForceIntegrationCallbackData {
#[derive(Clone, Copy)]
#[cfg_attr(feature = "serde-serialize", derive(serde::Serialize))]
pub struct RidWithPriority {
#[cfg_attr(feature = "serde-serialize", serde(skip, default = "invalid_rid"))]
pub rid: Rid,
pub priority: i32,
}
Expand Down Expand Up @@ -125,6 +127,7 @@ pub struct RapierBody {
using_area_gravity: bool,
using_area_linear_damping: bool,
using_area_angular_damping: bool,
#[cfg_attr(feature = "serde-serialize", serde(skip))]
exceptions: HashSet<Rid>,
ccd_enabled: bool,
omit_force_integration: bool,
Expand All @@ -133,6 +136,7 @@ pub struct RapierBody {
can_sleep: bool,
constant_force: Vector,
linear_velocity: Vector,
previous_linear_velocity: Vector,
impulse: Vector,
torque: Angle,
angular_velocity: Angle,
Expand Down Expand Up @@ -188,6 +192,7 @@ impl RapierBody {
can_sleep: true,
constant_force: Vector::default(),
linear_velocity: Vector::default(),
previous_linear_velocity: Vector::default(),
impulse: Vector::default(),
torque: ANGLE_ZERO,
angular_velocity: ANGLE_ZERO,
Expand Down Expand Up @@ -360,8 +365,8 @@ impl RapierBody {
);
// if we are a conveyer belt, we need to modify contacts
// also if any shape is one-way
let modify_contacts_enabled = self.get_static_angular_velocity() != ANGLE_ZERO
|| self.get_static_linear_velocity() != Vector::ZERO
let modify_contacts_enabled = self.base.mode == BodyMode::STATIC
|| self.base.mode == BodyMode::KINEMATIC
|| override_modify_contacts;
physics_engine.collider_set_modify_contacts_enabled(
space_handle,
Expand Down Expand Up @@ -1147,6 +1152,14 @@ impl RapierBody {
}
}

pub fn set_previous_linear_velocity(&mut self, p_velocity: Vector) {
self.previous_linear_velocity = p_velocity;
}

pub fn get_previous_linear_velocity(&self) -> Vector {
self.previous_linear_velocity
}

pub fn on_update_active(
&mut self,
space: &mut RapierSpace,
Expand Down
18 changes: 13 additions & 5 deletions src/bodies/rapier_collision_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rapier::dynamics::RigidBodyHandle;
use rapier::geometry::ColliderHandle;
use servers::rapier_physics_singleton::PhysicsShapes;
use servers::rapier_physics_singleton::PhysicsSpaces;
use servers::rapier_project_settings::RapierProjectSettings;

use super::rapier_area::RapierArea;
use super::rapier_body::RapierBody;
Expand Down Expand Up @@ -131,6 +132,7 @@ pub enum CollisionObjectType {
)]
pub struct CollisionObjectShape {
pub xform: Transform,
#[cfg_attr(feature = "serde-serialize", serde(skip, default = "invalid_rid"))]
pub shape: Rid,
pub disabled: bool,
pub one_way_collision: bool,
Expand All @@ -140,7 +142,7 @@ pub struct CollisionObjectShape {
impl Default for CollisionObjectShape {
fn default() -> Self {
Self {
xform: Transform::default(),
xform: Transform::IDENTITY,
shape: Rid::Invalid,
disabled: false,
one_way_collision: false,
Expand All @@ -153,11 +155,13 @@ impl Default for CollisionObjectShape {
#[cfg_attr(feature = "serde-serialize", derive(serde::Serialize))]
pub struct RapierCollisionObject {
collision_object_type: CollisionObjectType,
#[cfg_attr(feature = "serde-serialize", serde(skip, default = "invalid_rid"))]
rid: Rid,
instance_id: u64,
canvas_instance_id: u64,
pickable: bool,
pub(crate) shapes: Vec<CollisionObjectShape>,
#[cfg_attr(feature = "serde-serialize", serde(skip, default = "invalid_rid"))]
space: Rid,
transform: Transform,
inv_transform: Transform,
Expand Down Expand Up @@ -190,10 +194,12 @@ impl RapierCollisionObject {
.get_setting_with_override(SLEEP_THRESHOLD_ANGULAR.into())
.try_to()
.unwrap_or_default();
let activation_linear_threshold = project_settings
let length_unit = RapierProjectSettings::get_length_unit();
let mut activation_linear_threshold = project_settings
.get_setting_with_override(SLEEP_THRESHOLD_LINEAR.into())
.try_to()
.unwrap_or_default();
activation_linear_threshold /= length_unit;
let activation_time_until_sleep = project_settings
.get_setting_with_override(TIME_BEFORE_SLEEP.into())
.try_to()
Expand All @@ -206,8 +212,8 @@ impl RapierCollisionObject {
pickable: true,
shapes: Vec::new(),
space: Rid::Invalid,
transform: Transform::default(),
inv_transform: Transform::default(),
transform: Transform::IDENTITY,
inv_transform: Transform::IDENTITY,
collision_mask: 1,
collision_layer: 1,
is_debugging_contacts: false,
Expand Down Expand Up @@ -512,7 +518,7 @@ impl RapierCollisionObject {
if let Some(shape) = self.shapes.get(idx) {
return shape.xform;
}
Transform::default()
Transform::IDENTITY
}

pub fn set_transform(
Expand All @@ -521,6 +527,7 @@ impl RapierCollisionObject {
wake_up: bool,
physics_engine: &mut PhysicsEngine,
) {
let teleport = self.transform == Transform::IDENTITY;
self.transform = p_transform;
self.inv_transform = transform_inverse(&self.transform);
if !self.is_valid() {
Expand All @@ -534,6 +541,7 @@ impl RapierCollisionObject {
self.body_handle,
position,
rotation,
teleport,
wake_up,
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/bodies/rapier_direct_body_state_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl RapierDirectBodyStateImpl {
return body.get_base().get_transform();
}
}
Transform::default()
Transform::IDENTITY
}

pub(super) fn get_velocity_at_local_position(&self, local_position: Vector) -> Vector {
Expand Down
4 changes: 2 additions & 2 deletions src/joints/rapier_damped_spring_joint_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ impl RapierDampedSpringJoint2D {
pub fn new(
p_anchor_a: Vector,
p_anchor_b: Vector,
body_a: &Box<dyn IRapierCollisionObject>,
body_b: &Box<dyn IRapierCollisionObject>,
body_a: &dyn IRapierCollisionObject,
body_b: &dyn IRapierCollisionObject,
physics_engine: &mut PhysicsEngine,
) -> Self {
let invalid_joint = Self {
Expand Down
4 changes: 2 additions & 2 deletions src/joints/rapier_groove_joint_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ impl RapierGrooveJoint2D {
p_a_groove1: Vector,
p_a_groove2: Vector,
p_b_anchor: Vector,
body_a: &Box<dyn IRapierCollisionObject>,
body_b: &Box<dyn IRapierCollisionObject>,
body_a: &dyn IRapierCollisionObject,
body_b: &dyn IRapierCollisionObject,
physics_engine: &mut PhysicsEngine,
) -> Self {
let invalid_joint = Self {
Expand Down
2 changes: 2 additions & 0 deletions src/joints/rapier_joint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use super::rapier_revolute_joint::RapierRevoluteJoint;
#[cfg(feature = "dim3")]
use super::rapier_spherical_joint_3d::RapierSphericalJoint3D;
use crate::rapier_wrapper::prelude::*;
use crate::types::invalid_rid;
use crate::*;
#[cfg_attr(feature = "serde-serialize", typetag::serde(tag = "type"))]
pub trait IRapierJoint {
Expand All @@ -35,6 +36,7 @@ pub struct RapierJointBase {
max_force: f32,
handle: JointHandle,
space_handle: WorldHandle,
#[cfg_attr(feature = "serde-serialize", serde(skip, default = "invalid_rid"))]
space_rid: Rid,
disabled_collisions_between_bodies: bool,
}
Expand Down
4 changes: 2 additions & 2 deletions src/joints/rapier_revolute_joint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ impl RapierRevoluteJoint {
pub fn new(
anchor_a: Vector,
anchor_b: Vector,
body_a: &Box<dyn IRapierCollisionObject>,
body_b: &Box<dyn IRapierCollisionObject>,
body_a: &dyn IRapierCollisionObject,
body_b: &dyn IRapierCollisionObject,
physics_engine: &mut PhysicsEngine,
) -> Self {
let invalid_joint = Self {
Expand Down
4 changes: 3 additions & 1 deletion src/rapier_wrapper/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl PhysicsEngine {
}
}

#[allow(clippy::too_many_arguments)]
pub fn body_create(
&mut self,
world_handle: WorldHandle,
Expand Down Expand Up @@ -161,6 +162,7 @@ impl PhysicsEngine {
body_handle: RigidBodyHandle,
pixel_pos: Vector<Real>,
rot: Rotation<Real>,
teleport: bool,
wake_up: bool,
) {
if let Some(physics_world) = self.get_mut_world(world_handle)
Expand All @@ -173,7 +175,7 @@ impl PhysicsEngine {
body,
Translation::from(pixel_pos),
rot,
false,
teleport,
wake_up,
);
}
Expand Down
Loading
Loading