Complex KinematicBody2D objects tree collision detection

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Notorious2016

Hello to everyone,

I have an object made of several KinematicBody2D parts, since some parts need separate rotations and/or animations (imagine a level boss to be intended).

I need simply to stop the object from moving (the entire tree of KinematicBody2D object parts) if one of them touch an obstacle. Currently, every object part has his move_and_collide() method called in the _physics_process() method, and what succeeds now is that while the colliding object part correctly stops moving when a collision occurs, the other parts still move, breaking the whole thing.

Basically, how I can synchronize every object parts that if just one part collides and stops all the remaining should also stop? The best way? Hoping to have explained decently the problem, I’m very glad for suggestions. Thanks!!

:bust_in_silhouette: Reply From: Notorious2016

I just solved the question in the following way, for now it seems without any contraindication:

Firstly, my set of KinematicBody2D parts is composed of this simple hiearchy:

LevelBoss
--------Part01
--------Part02
etc etc…

The _physics_process() function of LevelBoss I checked is called always for first by Godot, but to move the LevelBoss i must know if some of its parts (Part01, Part02, etc…) collides with obstacles.

So, in the LevelBoss _physics_process() I simply call for every part a CheckCollision() method, passing it the delta time, this is my simple CheckCollision():

func CheckCollision(delta):
if (test_move(global_transform, velocity * delta)):
	get_parent().StopMove()

here the StopMove() simply sets a flag in the parent (the LevelBoss) to inform he can’t move. But If no parts calls StopMove(), LevelBoss finally moves by simply calling in its _physics_process() :

move_and_collide(velocity * delta)

This was my natural way to handle an object composed of different KinematicBody2D parts, if you have suggestions to improve the code they are welcomed!