Collision detection on RigidBody2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Arek900704
:warning: Old Version Published before Godot 3 was released.

Hi, I have same problems with detection collision between RigidBody2D and KinematicBody2D. On Rigid I have set Contact Monitor as ON and Contacts Raported as 10.

The code

var bodies = get_colliding_bodies()
    print(bodies)

return null although in the same time code from KinematicBody2D

if (is_colliding()):
        print(get_collider())

return

[RigidBody2D:631]

What I have to do to have detection on RigidBody? Please help :slight_smile:

Can you explain the situation where that happens?

If you try to “move” the kinematic it will never “touch” the rigid because move prevents overlaps.

eons | 2016-11-27 15:24

Yes, I move KinematicBodies using “move”, When I use set_pos() on KinematicBody I catch collision on RigidBody but I loose Collision on KinematicBody :frowning:

Arek900704 | 2016-11-27 15:34

:bust_in_silhouette: Reply From: eons

Rigid bodies are meant to be moved by forces and controlled by the engine (unpredictables at some extent) while Kinematic bodies are meant to be like Static bodies that can have their position changed.

As an extra, in Godot kinematic bodies provide the move method to prevent overlaps and report some kind of contact (is a “I can’t move more” type of report) when using move.

That means using move, a kinematic body can’t push a rigid (or another kinematic)…


I was doing some experiments for a “push” kind of move, one option is to get the collider after move or move_to and work over that collider (group filtering the collider could help for pushable elements).

Another way is to “cheat the system”:

KinematicBody2D (layer 0, mask 1) (script with move)
  |-Shapes,sprites,whatever
  |-KinematicBody2D (no layer, mask 1) (not affected by move)
        |-Shape, a bit broad than parent's shape

That way, move on the main body will move (position) of the children, the children kinematic will touch all the rigidbodies on layer 1 (and overlap static/kinematic) while the parent won’t overlap anything on layer 1.

You can find more ways to do that too, like using rays or areas if you just want detections or working with Shape methods directly.