Checking areas when spawn new scenes

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

Hello everyone,

I have been using Godot for several days now and in general I am very impress about how easy is to set up and create new things. Thank you for all the work!

I am trying to spawn new scenes (basically Area2D actors) randomly and I don’t want them to overlap either with each other or with another 2D areas already spawn. So for this I was trying to check the collions via get_overlapping_areas() but I notice that is not taking effect until the next tick. Is there a way of forcing the physics to be calculated at that point or to check the collisions in another way?

Thank you very much!

:bust_in_silhouette: Reply From: klaas

I think you must use PhysicsShapeQuerys then.

https://docs.godotengine.org/en/stable/classes/class_physics2ddirectspacestate.html#class-physics2ddirectspacestate-method-intersect-shape

you have to get the space from the PhysicsServer2D and test intersection with a Physics2DShapeQueryParameters object.

Thank you for your answer. I have tried with that:

var query = Physics2DShapeQueryParameters.new()
query.set_transform(shipColl.transform)
query.set_shape(shipColl)
query.collide_with_areas = true

var space_state = get_world_2d().get_direct_space_state()
var result = space_state.get_rest_info(query) 

where shipColl is a CollisionShape2D but the result variable is always empty.

Akkon | 2020-07-24 16:56

shipColl is not transformed through any parent?

query.set_transform(shipColl.global_transform)

are you shure that you got the right space?

var space_rid = Physics2DServer.body_get_space( shipColl.get_rid() )
var space_state = Physics2DServer.space_get_direct_state( space_rid )

klaas | 2020-07-24 18:14

Thanks for your answer.

In principle the transform should not matter becasue for this test I have put everything in the origin.

I have use this:

var space_rid = Physics2DServer.body_get_space( shipColl.get_rid() )
var space_state = Physics2DServer.space_get_direct_state( space_rid )

but the space_state that I get is null. I am trying to read the documentation of this but I don’t fully understand what should I get here or if I need to change something in my Area2D.

I have checked in the process function the return of overlaping_areas() and there I do get what I expect.

Akkon | 2020-07-25 09:28