CharacterBody2D: Detect RigidBody2D collisions but don't React

Godot Version

v4.6.2.stable.official [71f334935]

Question

How can I get a RigidBody2D to detect collisions but be physically unaffected by the collision?

I have a CharacterBody that only calls move_and_collide(Vector2(0,0)) and RigidBodys that can bounce off of it. I would like to have the RigidBodies bounce off the CharacterBody as if it was a StaticBody

However: After the collision, the RigidBody looses some velocity, the CharacterBody gets nudged, and the CharacterBody’s move_and_collide does not return a collision.

Commenting out the move_and_collide makes the collision happen as expected, but I need to move the CharacterBody so that is not helpful.

If I am understanding correctly, I should not need a contact monitor for the CharacterBody so I do not believe that is the issue.

Minimum Code:

# RigidBody2D
extends RigidBody2D

func _ready() -> void:
	print("Rigid Body Velocity ", linear_velocity)

func _on_body_entered(body: Node) -> void:
    # Print Characters new position and this objects new velocity
	print("\nIn RigidBody After Colision:\nCharacter Pos: ", body.position, "\nRigidBody Velocity: ", linear_velocity)

#CharacterBody2D
extends CharacterBody2D

func  _ready() -> void:
	print("Character Position ", position)

func _physics_process(delta: float) -> void:	
	var before = position
	var collision = move_and_collide(Vector2(0,0))
	var after = position
	if collision != null: # collision is always null
		print(collision.get_collider_velocity())
		print(before, after)


Setup:

Create a Rigid and Character Body with Rectangle Shapes. Enable contact monitor for Rigid Body, set a velocity, and remove all damping. Create A PhysicsMaterial with 0 friction and 1 bounce.

# Resulting Output:
Rigid Body Velocity (80.0, 0.0)
Character Position (500.0, 300.0)

In RigidBody After Colision:
Character Pos: (501.0587, 300.0)
RigidBody Velocity: (-16.47706, 0.0)

As shown, the Character is moved and does not report a collision, while the RigidBody looses momentum

Background

I have a hollow circle controlled by the character that goes around “picking up” rigid bodies. When the rigid bodies are inside the circle, I do not want them nudging the circle around but I do want them to bounce around inside it. “Picking Up” happens when a RigidBody crosses the outside of the circle into the middle. I am not there yet, but I plan to do that logic with layers

You’ll need to disable the collision layer of your RigidBody2D in the CharacterBody2D CollisionObject2D.collision_mask property so the RigidBody2D can see the CharacterBody2D but the CharacterBody2D can’t see the RigidBody2D more info:

For example, you can set your RigidBody2D CollisionObject.collision_layer to 2

This way the RigidBody2D will collide with the CharacterBody2D but the CharacterBody2D won’t see or react to the collision.

You can also check this tutorial which explains it a bit better:

If you don’t want your RigidBody2D to lose velocity when bouncing off then you’ll need to set its RigidBody2D.linear_damp_mode to Replace and its RigidBody2D.linear_damp to 0.0. Do the same with the angular properties too.

Thank You for the response,

Removing the collision mask from the CharacterBody or setting the RigidBody layer to 2 does cause the physics to work as expected, but I want CharacterBody to be able to detect the collisions. This is especially a problem because even in its current state, move_and_collide is not returning any collisions.

And I should have specified, I do have both damp modes at 0 and set to Replace. The velocity loss on the RigidBody only happens when the CharacterBody runs move_and_collide.

I looked at the kidscancode article and tried move_and_slide. It was able to detect the collision, which is progress, but that has the same physical effect of nudging the CharacterBody and slowing the RigidBody

Can this be considered a bug? I don’t believe move_and_collide and move_and_slide should have this effect.

You and the article did give me an idea though. I took 4 CollisionPolygon2D shaped like a quarter circle with one way collision on. With this method, I won’t need to detect collisions to trap a particle, so the layer changes you suggested will work here.

Thank You