Get collision point with 2 Area2Ds

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

Is it there a way to get the collision point between two Area2D nodes?

:bust_in_silhouette: Reply From: kidscancode

2 areas can overlap in any number of ways, so there isn’t going to be a single collision point, but a whole overlapping area.

Explain a bit about what you’re trying to do and maybe someone can give you a pointer in the right direction.

I’m having a similar problem.
The goal is simple: I need to trigger a function when KinematicBody2D is fully inside an Area2D.
My initial idea was to somehow get the point of collision and then continuosly calculate the offset based on KinematicBody’s size to trigger the function at some point, but I do understand that getting the collision point isn’t as simple.

Is there a better solution to this?

Gadzhi Kharkharov | 2018-08-02 14:24

That’s how I was actually able to solve this: get shapes of both nodes on collision, draw two Rect2 and then continuously check if one encloses the other one. While this works, it definitely looks like super hacky solution that wouldn’t work with anything else other than rectangular collision shapes.

Would love to know if there’s a better solution to this.

extends Area2D

var overlapping = false
var player = null

func _physics_process(delta):
	if overlapping:
		var self_rect = Rect2(self.global_position - self.shape_owner_get_shape(0,0).extents, self.shape_owner_get_shape(0,0).extents*2)
		var player_rect = Rect2(player.global_position - player.shape_owner_get_shape(0,0).extents, player.shape_owner_get_shape(0,0).extents*2)
		print(self_rect.encloses(player_rect)) # this finally works

func _on_Area2D_body_entered(body):
	player = body
	overlapping = true

Gadzhi Kharkharov | 2018-08-03 02:05

Made the script a little bit easier to read…

extends Area2D
var overlapping_body = null

func _physics_process(delta):
	if overlapping_body:
		check_if_inside(overlapping_body)

func check_if_inside(body):
	var self_shape = self.get_node("CollisionShape2D").shape
	var body_shape = body.get_node("CollisionShape2D").shape
	var self_rect = Rect2(self.global_position - self_shape.extents, self_shape.extents * 2)
	var body_rect = Rect2(body.global_position - body_shape.extents, body_shape.extents * 2)
	print(self_rect.encloses(body_rect))
	
func _on_Area2D_body_entered(body):
	overlapping_body = body

func _on_Area2D_body_exited(body):
	overlapping_body = null

Gadzhi Kharkharov | 2018-08-03 12:57