If KinematicBody2D goes forward, how to detect collision that happened on the backside?

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

What is a situation:
Having object1 as main character with KinematicBody2D .
Having circle shape collider on object1.

Before calling method move_and_collide(velocity) in object1’s script:
Suppose we have velocity: Vector2(10, 0) — here we are going to the left.
And suppose another object2 is coming from the top. What to do how to detect that object2?

Notes:

  • object2 is also KinematicBody2D
    i have choosen KinematicBody2D for object2, because i don’t need the physics to be applied to the object2, and that’s why i have moved him by code and not by physics.

  • i choose for main character KinematicBody2D in place RigidBody2D, because i don’t need the physics to be applied to the object1.

  • i want the collision to be detected by object1’s script, in his move_and_collide

Second question:
Did i choose wisely types of my objects?
Third question:
Should main character be RigidBody2D in order to detect collisions from all sides?

:bust_in_silhouette: Reply From: kidscancode

Kinematic bodies only detect collision when they move. They do not detect bodies hitting them.

If another body moves into it, that body must process the collision.

In object2’s script, notify the collider (object1) that it was hit.

Allright.
Two separate questions:

  1. What if i have n objects just like object2, should all of them carry collision logic? Are they going to slow whole game?

.2. Should i change my object1 and object2 to something else if for object1 and object2 is ok not to be effected by pychics?

Brazda | 2019-06-14 14:22

  1. Yes. If all of them are moving, they already have movement logic. Add a line that says something like:
if collision:
    collision.collider.react()

Where react() is whatever function on object1 you want it to execute when it’s hit. It’s the same amount of code, it’s just being called by the moving object, not the stationary one.

Why would you expect this to cause a performance issue?

  1. There are pros and cons to each body type. Which is the right choice for you depends on many factors. See here for some suggestions: Physics introduction — Godot Engine (latest) documentation in English

kidscancode | 2019-06-14 16:39

  1. I have expected that it is better that you have colliding logic on one object rather on n objects.

Brazda | 2019-06-14 16:53