Collision Detection

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

I am having trouble detecting collision with a Kinematic Body2D and an Area2D. Here is the code for collisions:

for idx in range(get_slide_count()):
		var collision = get_slide_collision(idx)
		if collision.collider.is_in_group("meteor"):
			var playspace = playsp.instance()
			playspace.playing = false

any suggestions? here is the full code if you need it:

extends KinematicBody2D

var velocity = Vector2()
var speed = 10
var immune = false
const playsp = preload("res://playspace.tscn")

func _ready():
	$AnimationPlayer.play("start_animation")
	$Particles2D2.emitting = false
	$LaserReadyTimer.start()

func get_input():
	var up = Input.is_action_pressed("up")
	var down = Input.is_action_pressed("down")
	var laser = Input.is_action_just_pressed("laser")
	if laser:
		laser()
	if up:
		velocity.y -= speed
	if down:
		velocity.y += speed
	
func laser():
	if $Particles2D3.lifetime <= 0.99:
		return
	$Particles2D2.emitting = true
	$LaserTimer.start()
	$Particles2D3.lifetime = 0.1
	immune = true


func progress_bar():
	if $Particles2D3.lifetime >= 1:
		return
	$Particles2D3.lifetime += 0.001

func _process(delta):
	get_input()
	if velocity.y < 0:
		velocity.y += 1
	if velocity.y > 0:
		velocity.y -= 1
	$Particles2D.lifetime += 0.0002
	progress_bar()
	if position.y <= 0:
		position.y = 575
	if position.y >= 576:
		position.y = 0

func _physics_process(delta):
	move_and_slide(velocity)
	if velocity.y >= 10000:
		velocity.y = 10000
	for idx in range(get_slide_count()):
		var collision = get_slide_collision(idx)
		if collision.collider.is_in_group("meteor"):
			var playspace = playsp.instance()
			playspace.playing = false

func _on_LaserTimer_timeout():
	$Particles2D2.emitting = false
	immune = false
:bust_in_silhouette: Reply From: kidscancode

Kinematic bodies don’t collide with areas. Areas only provide overlap detection, not collision.

You can use the body_entered signal of the Area2D to detect when the KinematicBody2D enters its shape.

Kidscancode can you please elaborate on this a little more. How would collision then be checked?

MysteryGM | 2018-10-13 16:48

As I wrote above. Area2D has a body_entered signal that is emitted whenever a PhysicsBody node enters. You can use that signal to do whatever you want to happen when the body contacts the area.

For example, if your Area2D was a coin that you wanted to collect:

func _on_Coin_body_entered(body):
    queue_free()

See the documentation has many examples, for example in the “Dodge the Creeps” game:
http://docs.godotengine.org/en/latest/getting_started/step_by_step/your_first_game.html

kidscancode | 2018-10-13 16:52