Retrieving the collision point of two area2d objects

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

I have two area2d objects. I’m interested in determining where they collide.
area_enter_shape doesn’t appear to return any collider objects.

Is there a way of retrieving this value without using ray intersect?

You’ll have to define the problem a bit more than that. Areas don’t collide, they’re allowed to overlap, so they can report what enters them. However, when shapes overlap, you’re going to have more than one point of intersection.

avencherus | 2017-02-26 22:16

The phrasing is unfortunate, but the question is valid still. Just as intersect_shape returns collision data, one would assume that internally area_enter and area_enter_shape use this data as well.

Daniel Mircea | 2017-02-26 22:40

Which intersect_shape-function are you referring to? There isn’t one in GDscript.

Warlaan | 2017-02-27 09:31

intersect_shape returns the shapes overlapping.

Area nodes have the get_overlapping_areas and get_overlapping_bodies that work just like intersect_shape.

Is that what you are looking for?

eons | 2017-02-27 14:29

You’re right, intersect_shape isn’t good either. The provided collider object in the Dictionary response is actually the other area. Get get_overlapping_bodies returns PhysicsBody2D objects, which I have none of …

After doing some research on polygon intersection I came to the conclusion that these methods don’t provide intersection points, also evidenced here http://docs.godotengine.org/en/stable/tutorials/vector_math.html#some-examples-of-planes

Daniel Mircea | 2017-02-27 18:23

Maybe this Shape2D method Shape2D — Godot Engine (latest) documentation in English

With that, you need to test shape against shape with the transforms of the nodes, but may be better to do it only after a positive overlap.

eons | 2017-02-27 20:02

eons, thank you so much! That worked great.

Daniel Mircea | 2017-02-27 22:01

:bust_in_silhouette: Reply From: Daniel Mircea

As eons suggested, I solved this by using the collide_and_get_contacts method.

For anyone coming from google or any other source, here’s how I’ve handled the collision between a CollisionPolygon2D and a Shape:

Note: by CollisionPolygon2D, I’m referring to a custom shape I’ve created by hand.

var polygon_collision = get_node("polygon-collision")
var capsule_collision = get_node("capsule-collision")

# A concave polygon is more expensive 
# and is also instanced differently
var polygon_shape = ConvexPolygonShape2D.new()
polygon_shape.set_points(polygon_collision.get_polygon())

polygon_shape.collide_and_get_contacts(
    polygon_collision.get_global_transform(),
    capsule_collision.get_shape(), 
    capsule_collision.get_global_transform()
)