This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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)
Godot version 3.5.1
in Engine by (63 points)
edited by

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?

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

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.

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.

Not the answer to your question but...

query.transform = $grenade_area.global_transform

...should probably be...

query.global_transform = $grenade_area.global_transform

1 Answer

0 votes

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.

by (63 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.