Skip to content

Commit

Permalink
Fix shape scale issue and extra contacts reported. Also update rigidb…
Browse files Browse the repository at this point in the history
…odies to use collision groups (#128)

- Fixes #127
- Fixes #126
- Fixes #129
- Fixes #130
- Fixes #102
  • Loading branch information
Ughuuu authored Jul 1, 2024
1 parent 394f313 commit 403a17d
Show file tree
Hide file tree
Showing 16 changed files with 282 additions and 286 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ publish = false
crate-type = ["cdylib"]

[features]
default = ["single-dim2", "serde-serialize"]
default = ["single-dim3", "serde-serialize"]
dim2 = []
dim3 = []
single = []
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ The reason it was rewritten is to do easier cross platform determinism part and
- Cross platform determinism.
- Double builds.
- Liquids Missing.
- No support for asymetric collisions (eg. object 1 hitting object 2 but object 2 not hitting object 1).
- No support for asymetric collisions (eg. object 1 hitting object 2 but object 2 not hitting object 1). More info here [Rapier Collision groups and solver groups](https://rapier.rs/docs/user_guides/rust/colliders/#collision-groups-and-solver-groups). This is the exact check rapier does: `(A.layer & B.mask) != 0 && (B.layer & A.mask) != 0`
- Friction works differently than it does in Godot. The current formula is: friction is multiplied by other friction, bounce is taken the max value.

# Platforms

Expand Down
101 changes: 46 additions & 55 deletions bin2d/test.tscn
Original file line number Diff line number Diff line change
@@ -1,63 +1,54 @@
[gd_scene load_steps=5 format=3 uid="uid://cn4jscixu2bmf"]
[gd_scene load_steps=4 format=3 uid="uid://cn4jscixu2bmf"]

[sub_resource type="GDScript" id="GDScript_gph3d"]
script/source = "extends Node2D
[sub_resource type="CircleShape2D" id="CircleShape2D_lc8oa"]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_irytb"]

func _ready() -> void:
var space := get_viewport().find_world_2d().direct_space_state as RapierDirectSpaceState2D
var space_json := FileAccess.open(\"user://space.json\", FileAccess.WRITE)
var shapes_json := FileAccess.open(\"user://shapes.json\", FileAccess.WRITE)
#space_json.store_string(space.export_json())
#shapes_json.store_string(RapierPhysicsServer2D.shapes_export_json())
"
[sub_resource type="CircleShape2D" id="CircleShape2D_ykjo6"]
[sub_resource type="CircleShape2D" id="CircleShape2D_6y2xx"]
[sub_resource type="GDScript" id="GDScript_swyq8"]
script/source = "extends ShapeCast2D


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
print(get_collision_count())
print(get_collision_point(0))
print(get_collision_point(1))
"
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_ila5n"]
normal = Vector2(-0.0425147, -0.999096)

[node name="SceneRayOnSwitch" type="Node2D"]

[node name="Node2D" type="Node2D" parent="."]
script = SubResource("GDScript_gph3d")
[node name="Node2D2" type="Node2D" parent="."]
[node name="RigidBody2D" type="StaticBody2D" parent="."]
position = Vector2(133, 63)
[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D"]
position = Vector2(65, 27)
shape = SubResource("CircleShape2D_ykjo6")
[node name="RigidBody2D2" type="StaticBody2D" parent="."]
position = Vector2(132, 67)
[node name="RigidBody2D2" type="RigidBody2D" parent="."]
position = Vector2(-54, -682)
rotation = -1.51672
scale = Vector2(4, 2.51)
mass = 100.0
max_contacts_reported = 1

[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D2"]
position = Vector2(65, 27)
shape = SubResource("CircleShape2D_ykjo6")
[node name="ShapeCast" type="ShapeCast2D" parent="."]
position = Vector2(123, 62)
shape = SubResource("CircleShape2D_6y2xx")
target_position = Vector2(205, 82)
script = SubResource("GDScript_swyq8")
[node name="Marker2D" type="Marker2D" parent="."]
position = Vector2(207, 92)
position = Vector2(-89, 3)
scale = Vector2(1.89, 1)
shape = SubResource("CircleShape2D_lc8oa")

[node name="CollisionShape2D2" type="CollisionShape2D" parent="RigidBody2D2"]
position = Vector2(-88.2483, 25.9664)
scale = Vector2(1.89, 1)
shape = SubResource("CircleShape2D_lc8oa")

[node name="CollisionShape2D3" type="CollisionShape2D" parent="RigidBody2D2"]
position = Vector2(-87.2888, 54.2119)
scale = Vector2(1.89, 1)
shape = SubResource("CircleShape2D_lc8oa")

[node name="RigidBody2D3" type="RigidBody2D" parent="."]
position = Vector2(-80, -513)
rotation = -1.51672
scale = Vector2(4, 2.51)
max_contacts_reported = 1

[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D3"]
position = Vector2(-89, 3)
scale = Vector2(1.89, 1)
shape = SubResource("RectangleShape2D_irytb")

[node name="StaticBody2D" type="StaticBody2D" parent="."]
position = Vector2(-1, 55)

[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"]
position = Vector2(-6, 61)
rotation = -0.060205
shape = SubResource("WorldBoundaryShape2D_ila5n")

[node name="Camera2D" type="Camera2D" parent="."]
zoom = Vector2(2, 2)
44 changes: 20 additions & 24 deletions src/bodies/rapier_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,26 +374,13 @@ impl RapierArea {
if let Some(space) = physics_spaces.get_mut(&space_rid) {
space.area_remove_from_area_update_list(*area_rid);
}
for (detected_body, _) in detected_bodies {
let mut area_override_settings = None;
if let Some(body) = physics_collision_objects.get(&detected_body) {
if let Some(body) = body.get_body() {
area_override_settings = Some(
body.get_area_override_settings(physics_spaces, physics_collision_objects),
);
}
}
if let Some(area_override_settings) = area_override_settings {
if let Some(body) = physics_collision_objects.get_mut(&detected_body) {
if let Some(body) = body.get_mut_body() {
body.apply_area_override(
area_override_settings,
physics_engine,
physics_spaces,
);
}
}
}
for (detected_body, _) in &detected_bodies {
RapierBody::apply_area_orverride_to_body(
detected_body,
physics_engine,
physics_spaces,
physics_collision_objects,
);
}
}

Expand Down Expand Up @@ -713,7 +700,7 @@ impl IRapierCollisionObject for RapierArea {
return;
}
self.base.set_space(p_space, physics_engine, physics_spaces);
self.recreate_shapes(physics_engine, physics_shapes);
self.recreate_shapes(physics_engine, physics_shapes, physics_spaces);
}

fn add_shape(
Expand Down Expand Up @@ -831,7 +818,7 @@ impl IRapierCollisionObject for RapierArea {
physics_engine: &mut PhysicsEngine,
physics_shapes: &mut PhysicsShapes,
) -> ColliderHandle {
if !self.base.is_space_valid() {
if !self.base.is_valid() {
return ColliderHandle::invalid();
}
let mat = self.init_material();
Expand All @@ -840,15 +827,24 @@ impl IRapierCollisionObject for RapierArea {
}

fn init_material(&self) -> Material {
default_material()
Material::new(
self.base.get_collision_layer(),
self.base.get_collision_mask(),
)
}

fn recreate_shapes(
&mut self,
physics_engine: &mut PhysicsEngine,
physics_shapes: &mut PhysicsShapes,
physics_spaces: &mut PhysicsSpaces,
) {
RapierCollisionObject::recreate_shapes(self, physics_engine, physics_shapes);
RapierCollisionObject::recreate_shapes(
self,
physics_engine,
physics_shapes,
physics_spaces,
);
}

fn shape_changed(
Expand Down
Loading

0 comments on commit 403a17d

Please sign in to comment.