Collision detection in a platform game using kinematic bodies

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

Hello everyone!

I’m in the process of creating a platformer with kinematic bodies as both the player and the enemies. I’ve been looking at the examples in the demos, but I’ve having trouble finding an appropriate collision detection example I can wrap my head around. The question is this… What is the simplest path to detecting a collision between two kinematic bodies? For example, I’ve got a punch animation and I want to detect if a kinematic body is present inside the area of the punch, so I can then take action on it. Can someone give me a super simple example that would print the string “success!” when this occurs? Or even better, something that prints the name the kinematic body.

Or am I grossly misunderstanding how collisions are supposed to work?

Here is code I’m using in a child to try and detect collisions, but no matter what it overlaps with, the printed result is always

extends Area2D

export var done = false

func _ready():
	set_fixed_process(true)

	get_node("Timer").start()

	print(get_overlapping_bodies())
	print(get_overlapping_areas())


func _on_Timer_timeout():
	queue_free()

Could REALLY use some help on this guys, I’ve been stuck on this for days.

Your question is about kinematic bodies or areas? both things are completely different.

eons | 2018-01-26 00:38

Honestly, I don’t care how I get there. It could be kinematic bodies, areas, or rigidbody. The moral of the story is, when I stick out my fist, I want to detect what it touches. The end goal is if I punch a wall, I want to get pushed back. If I punch an enemy, I want to be able to register the hit. I’ve managed to get basic collision detection working with area and get_overlapping_bodies, but I’m looking for something a little more descriptive than it simply telling me if I’m touching a body. I want to be able to do if conditions on the result of the collision. I’ve yet to find something that lets me do that, which seems silly to me as it is the basis for most physical interactions in video games.

behelit | 2018-01-26 00:44

:bust_in_silhouette: Reply From: gtkampos

GDquest melee atack with swords:

I already see where this will be problematic in my game. You see, in my case, there is no separate animation for the punch, it is the same as animation used to walk around, jump, etc. I simply want to make a collision box appear in front of the character when a punch occurs, detect if it touches something, then disappear. I don’t understand why it has to be this big complicated thing with code spread out among multiple objects.

LOL… 7 minutes into the video “alright, there is absolutely no collision code yet…”. You would think for one of the most fundamental concepts in game programming, Godot would have some really simple examples. Apparently not. I figured out collision in the 3d world of Second Life in 20% of the time it’s taking me to do the same in Godot.

Also, I’m using 2.1.4 and I don’t think this example will work for me anyway.

behelit | 2018-01-25 20:01

You could make an animation that will have two keyframe. One will enable the collision in the exact frame you want, and other keyframe will disable. It is very simple indeed.

I am planning to make a tutorial on youtube of a platform game that I am making. It have an enemy that uses that. Stay tunned on the forums.

gtkampos | 2018-02-08 14:39

:bust_in_silhouette: Reply From: behelit

Okay, I figured it out. In order to use area to detect things, I simply placed a area2d collision box in front of the character. From there, I linked the signal back into the main kinematic character script. When I throw a punch, it calls the area enter function. From there, I used get_node to retrieve the actual collision information. It seems like there should be an easier way of doing this, but that was the simplest method I found in 4 days of searching and experimentation.