+1 vote

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 moveandslide to detect the collision, otherwise if it don't move it don't detect collision. pls help! :C

in Engine by (82 points)

1 Answer

+2 votes
Best answer

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.

by (20,895 points)
selected by

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

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().

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

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.

true, thank you!

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?

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.

Makes sense to me but moveandcollide() 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.

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.