How can I detect collision between the intersection of two shapes? (2D)

Godot Version

v4.2.2.stable.arch_linux

Question

I’d want to ideally setup movement like that of move_and_slide, but be able to make it so you can only collide between the intersection of two colliders (area thats contained inside both colliders will be what collides with the player) named, let’s say A and B. I have found a post mentioning Prototyping levels with CSG — Godot Engine (stable) documentation in English, but that appears to be 3d only and another mentioning raycasts (which I still wouldn’t know how to use in a similar way to move_and_slide). I have thought of dynamically modifying the collision mask of objects if you are in within the area of A, but that would make it so you collide with parts of B that are outside of A whenever you enter A.

It sounds like you want to detect collision when the following criteria are fulfilled:

  • Collision happened inside the Collisionshape2D of objectA.
  • Collision happened inside the Collisionshape2D of objectB.

For that, you can first detect the collision itself. Afterwards, you can get the collision point and then ask the direct_space_state if objectA and objectB are colliding with the collision point.

You can use the Geometry2D to get the intersection of two polygon.

Then use it as a collision shape

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.