Differentiation between different colliders

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

How can I tell the difference between what node I am hitting? The script is attached to the moving object and I need it to differentiate between the floor or the player nodes. Everything has been set up with Body > Sprite, Collider. I have tried both KinematicBody2D and RigidBody2D:

var bodies = get_colliding_bodies()
print(bodies)
for body in bodies:
	if body.is_in_group("Player"):
		print("here")
		#queue_free()
"""
#get_collider()
if is_colliding():
	print(get_collider_shape())
	#print(get_node("/root/Player"))
	queue_free()
"""

For the code above it throws an error at get_colliding_bodies saying: condition !contant_moniter is true. returned Array()

:bust_in_silhouette: Reply From: eons

body_enter signal will tell you about each collision when they happen (you need contact monitor).

get_colliding_bodies gives you an array with the bodies colliding IF contact monitor is true (default false), the size of the array will be as large as the collision max count (default 0).

Kinematic bodies works different, these have a simple collision detection that works only when (after) using move or move_to during _fixed_process

Kinematic character and platformer demo have many examples.

I suppose I can look into even more demos. I am aware of the functions. I just need to put it all together. Mainly for the scope of this question, I need to detect whether or not it is a “Player” node or a “Floor” node on the script attached to the moving object.

jjphung | 2017-04-08 07:31

Once you have the collider (with rigid signal or kinematic function) or the array with colliders (rigid function) you can check in many ways.

I prefer to use groups (is_in_group) because is flexible.
If you have a good collision layer design, these can be used to distinguish the type of object too.
The node name or part of the name can be used too, also node class/parent class.

The best way may depend on your design.

eons | 2017-04-08 14:15

I finally got the Player node and have access to it. However the get_colliding_bodies always turn up empty:

var bodies = get_colliding_bodies()
   for body in bodies:
		if body.is_in_group("Player"):
			print("Game Over")

The tree structure is below. The Meteor node is instantiated as get_tree().get_root().add_child(newMeteor) at run time. Here is the Tree Structure.

jjphung | 2017-04-09 05:13

Solved it! Had to set set_max_contacts_reported() since it defaults at 0, explains why get_colliding_bodies array turned up empty! Then I used:

var bodies = get_colliding_bodies()
if player in get_colliding_bodies():
	print("Game Over")

jjphung | 2017-04-09 06:36