Adjusting Rigidbody2D collisionShape properties at runtime breaks signals.
Description
With RB2D and collisionshape2D created in the editor, adjusting the properties of the collisionshape2D shape with code breaks signalling for TileMap collisions. Signals like body_entered, body_shape_entered no longer trigger from the RigidBody2D for TileMap, TileMapLayer or other RigidBody2D collisions, although the physics still works. It does however still trigger for CharacterBody2D's.
In this particular instance, the collision shape is a CircleShape2D added to the RB2D in the editor and the radius is set on _ready(). Even if the radius is set to the same radius as it already is the behaviour is evident.
Creating the collision shape and then setting the property at runtime avoids this issue. Not touching the properties with code avoids this issue. Using godot built-in physics avoids this issue.
To Reproduce New scene Node2D. Add TileMapLayer, configure configure physics, add a short horizontal line of physics enabled tiles. Add RigidBody2D and CollisionShape2D Circle Shape. Set Solver > Contact Monitor to ON and Max reported contacts to 1
On RigidBody2D add script;
extends RigidBody2D
@onready var collision_shape_2d: CollisionShape2D = $CollisionShape2D
func _ready() -> void:
var circle_shape = collision_shape_2d.shape as CircleShape2D
circle_shape.radius = circle_shape.radius
connect("body_shape_entered", Callable(_on_body_shape_entered))
func _on_body_shape_entered(_body_rid: RID, body: Node, _body_shape_index: int, _local_shape_index: int):
print(self.name, " hit ", body.name)
Add another RigidBody2D, but don't create or assign a collisionShape. Add script to the second RB2D;
extends RigidBody2D
func _ready() -> void:
var newcollider = CollisionShape2D.new()
var circleshape = CircleShape2D.new()
circleshape.radius = 5
newcollider.shape = circleshape
add_child(newcollider)
connect("body_shape_entered", Callable(_on_body_shape_entered))
func _on_body_shape_entered(_body_rid: RID, body: Node, _body_shape_index: int, _local_shape_index: int):
print(self.name, " hit ", body.name)
Place the two RigidBody's above the tilemap and enable gravity for both.
Expected behavior
RigidBody2D should trigger body_entered and body_shape_entered signals for all collisions with collisionshapes including tilemaps and tilemaplayers even if the properties of a collision shape originally added in the editor are adjusted during runtime.
Environment:
- OS: Win 11
- Version: latest
- Godot Version: 4.3 stable
- Type: 2d
Example project(zip) Uploading rapier_RB2D_repro.zip…
Please attach a zip with project where issue occurs. Remove the addons folder so that the size is less than 25 mb.
Got this issue as well, I worked around it by using PhysicsServer2D:
var shape_rid := PhysicsServer2D.circle_shape_create()
PhysicsServer2D.shape_set_data(shape_rid, new_size)
PhysicsServer2D.body_add_shape(get_rid(), shape_rid)
Is there any progress on this? Seems like a fairly critical bug and the fix from @geowarin is not working for me. Strangely, this issue is also remedied for me when Debug: Visible Collision Shapes is turned on from the editor. Anyone found any fixes or have any insight into what's going on?
I'm using Godot v4.4 & Rapier2D v0.8.8
Not much progress from my side, no sorry.
@Ughuuu Ah okay, thanks for the response. Sorry, wish I knew enough Rust to help out!
@charliertm can you give this a go on latest main? I believe it was fixed in https://github.com/appsinacup/godot-rapier-physics/pull/413 but I can't download your attached project for some reason :)
Reopen if still happens.