intersect_shape() return multiple duplicates of a same object (2D)

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

Is this by design or am I missing something? My example code

var query = Physics2DShapeQueryParameters.new()
query.set_collision_layer(2)
query.set_shape($grenade_area/CollisionShape2D.get_shape())
query.set_collide_with_areas(true)
query.transform = $grenade_area.global_transform
query.set_exclude([$grenade_area])

var space = get_world_2d().direct_space_state
var areas = space.intersect_shape(query, 32)

I’d expect each returned shape to be unique. Are you sure they’re really duplicates? intersect_shape() returns an array of dictionaries. If you print the rid property of each of the returned dictionaries, do you really see duplicates?

jgodfrey | 2023-01-01 21:05

Yes but there’s something odd in the dictionary. Here’s a result I got for print(areas) for code above:

[
{collider:Area2D:[Area2D:1889], collider_id:1889, metadata:Null, rid:[RID], shape:4},
{collider:Area2D:[Area2D:1889], collider_id:1889, metadata:Null, rid:[RID], shape:5},
{collider:Area2D:[Area2D:1889], collider_id:1889, metadata:Null, rid:[RID], shape:6},
{collider:Area2D:[Area2D:1889], collider_id:1889, metadata:Null, rid:[RID], shape:7},
{collider:Area2D:[Area2D:1889], collider_id:1889, metadata:Null, rid:[RID], shape:8},
{collider:Area2D:[Area2D:1889], collider_id:1889, metadata:Null, rid:[RID], shape:11},
{collider:Area2D:[Area2D:1889], collider_id:1889, metadata:Null, rid:[RID], shape:12}
...

The entries in dictionary look same except the shape key which value changes. I’m boggled.
Here are the rids, print(a.rid.get_id()) :

619
619
619
619
619
619
619
619
619
343
343
343
343
343
343
343
343

ville | 2023-01-01 21:20

I got it. I use Area2D and CollisionPolygon2D. Build mode for that CollisionPolygon2D was Solids. I changed it to Segments and now it works perfectly.

ville | 2023-01-01 21:30

Okay, the story continues. The segment constant for build mode will only include the polygon edge collisions. So the Area2D I’m checking against must touch the edges of the Area2Ds for the collision to happen in segment mode. It’s really not what I wanted.

ville | 2023-01-01 21:35

Not the answer to your question but…

query.transform = $grenade_area.global_transform

…should probably be…

query.global_transform = $grenade_area.global_transform

SQBX | 2023-01-02 04:12

:bust_in_silhouette: Reply From: ville

intersect_shape() returns shapes, not collisionbodies. CollisionPolygon2D can have and often has multiple collision shapes as this is how the engine is designed. The best solution is to use CollisionShape2D for simple objects and the collisionpolygon for something more complex.