#Create Polygon Collider from the shared shape of two other colliders / areas

1 messages · Page 1 of 1 (latest)

dull atlas
#

As shown in the picture, I would like to get the polygon of the intersection area of two polygon colliders ("detection colliders") to use it as a third collider ("intersection area collider").

The player will only collide with the intersection area collider and I plan to move and enable / disable the two detection colliders at runtime, which would dynamically change the polygon of the intersection area collider / disable or enable it.
What would be a good approach for this?

To better test this I would like to display the collider's shapes at runtime. What is the best method for doing this?

Thanks in advance.

dull atlas
#

Thank you, that looks great. Is it a good idea to use Polygon Colliders even if that polygon will be a rectangle or is there a way to get the points of a shape from that shape and its transform as a PackedVector2Array? Possibly a function that takes a shape and a transfrom and returns a PackedVector2Array?

proud magnet
#

Or did I misunderstand you?

dull atlas
# proud magnet Or did I misunderstand you?

Like in my picture, I wanted one of the detection colliders to be a rectangle. So either I would set is as a polygon collider from the start or I would have to get the points of the rectangle as a PackedVector2Array to put that into the intersect_polygons function. The intersection area should be a polygon.

proud magnet
dull atlas
#

that is the idea. I just don't know how to do just that

proud magnet
#

Well, what exactly is Area1 and 2?

#

CollisionShape2D or something else?

#

I'm very confident that whatever it is, you can get the shape out

dull atlas
#

Yes it would either be CollisionShape2D or CollisionPolygon2D

#

for CollisionPolygon2D, getting the PackedVector2Array polygon is easy

#

for CollisionShape2D though, I don't know how one could do that

proud magnet
# dull atlas for CollisionShape2D though, I don't know how one could do that

I think going down the path of CollisionShape2D.shape could work.
I don't think that it returns a Shape2D (it's abstract), but one of these:

Inherited By: CapsuleShape2D, CircleShape2D, ConcavePolygonShape2D, ConvexPolygonShape2D, RectangleShape2D, SegmentShape2D, SeparationRayShape2D, WorldBoundaryShape2D

For the RectangleShape2D you can get the size, if you take the position too, you can get the corners. And make your own PackedVector2Array.

dull atlas
#

Thanks. Is that more computationally efficient than simply defining the rectangle as a polygon though?
I wonder whether the collision detection for a rectangle shape (CollisionShape2D) is any different from the collision detection of a polygon that is a rectangle (CollisionPolygon2D).

proud magnet
# dull atlas Thanks. Is that more computationally efficient than simply defining the rectangl...

https://docs.godotengine.org/en/stable/classes/class_shape2d.html#class-shape2d

Shape2D
Performance: Primitive shapes, especially CircleShape2D, are fast to check collisions against. ConvexPolygonShape2D is slower, and ConcavePolygonShape2D is the slowest.

So CollisionShape2d will be more expensive if you use polygons... but it saves math later. 🤷‍♂️

I would probably try to go for cheap(er) collision detection + some extra math later. Collision detection is somewhat expensive already, so keeping that cheap (and it's running at 30 or 60 times per second) makes sense.

The alternative being more expensive collisions but easier math later. But the collision detection is frequent and the math is only once...

#

Makes sense?

dull atlas
#

Yes it does. Thank you for your help. One last thing: how would you visualize colliders? Is there maybe a solid color option for an Area2D node or something like that?

proud magnet
#

works for RayCasts too

dull atlas
#

fantastic 👍

dull atlas
#

thanks again, I implemented it and everything works out well (the only thing I still need to do is factor in the transform of everything)