0 votes

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 getoverlappingareas() 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!

in Engine by (12 points)

1 Answer

+1 vote

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.

by (2,940 points)

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.

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 )

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.

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 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 webmaster@godotengine.org with your username.