detecting collision in kinematicbody2d?

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

hello. i want to detect collision in kinematicbody2d, i want it to detect area2ds and static/kinematic/rigidbody2d. i realize there’s a way to do that, but body must move_and_slide to detect the collision, otherwise if it don’t move it don’t detect collision. pls help! :C

:bust_in_silhouette: Reply From: kidscancode

A KinematicBody2D cannot detect an Area2D because areas do not provide collision. However, the area can detect when the body enters it using the area’s body_entered signal.

thank you, but what about, other, bodies? kinematic and rigid body?

ProXFox | 2019-01-25 03:08

A KinematicBody2D will collide with other kinematic bodies and with rigid bodies. If you’re using move_and_slide() you can obtain this information with get_slide_collision().

kidscancode | 2019-01-25 03:27

sorry, but if something collides with kinematic body 2d while it won’t moving with move_and_slide() it won’t detect the collision.

ProXFox | 2019-01-25 05:11

No, it won’t because collision is detected upon movement. However, the other body that moves will detect the collision, so you can detect it from that side.

kidscancode | 2019-01-25 06:27

true, thank you!

ProXFox | 2019-01-25 06:44

But from an OOP standpoint that doesnt make any sense, doesn’t it? I want to detect when another object bumps into another object and the only way i could find is to detect collision in both objects. Isn’t there any way to check if something bumped into an object, and not only if the object bumped into something?

Nesessary | 2019-01-29 15:33

A collision can only occur when there’s movement. Therefore, the moving body is the one that causes the collision, so it makes sense that it’s the one to detect it and take action. If needed, that action can be to notify and/or call a method on the body that it hit.

kidscancode | 2019-01-29 16:19

Makes sense to me but move_and_collide() only recognizes collisions in the direction of movement. Imagine in Super Mario, no matter if a Goomba walks into you or you walk into a Goomba who might be moving in the same direction, you want Mario to loose the level or shrink down. Idealy you would code the interaction or event for very different enemy in the enemys script but that doesn’t seem to work in Godot because the enemy object wouldn’t recognize collision from behind. So that means i would have to code the collision event for the player and the enemy and that makes no sense to me.

Nesessary | 2019-01-29 17:39

In that situation, when Mario moves, you trigger his “hit” method if he moves into a Goomba (and probably trigger the Goomba’s “bounce” method). If Mario is standing still and the Goomba moves into him, the Goomba triggers Mario’s “hit” method, and its own “bounce” method. No matter the situation, the two colliding bodies are going to affect each other. It’s the moving one that initiates the collision event, no matter how you code it.

Otherwise, what you’re suggesting would require that every body be continually scanning for collisions, even when not moving, which would be much less efficient. I’m not really sure how else to describe it, other than to suggest you experiment, look at examples, and see for yourself.

kidscancode | 2019-01-29 20:36