+1 vote

Hello is there a way to detect if an static body overlaps with other static bodies?
I want to check if a space is already occupied before i spawn the static body.

in Engine by (37 points)

1 Answer

0 votes

Add an Area2d as a child which will work on a separate 2d layer to detect the collisions

by (68 points)

But an area cant detect static bodys to my knowledge

I assume you have a node with a script that spawns the static bodies.

In that script, you will have to have a physicsprocess function which will check for the Area2D of each static body with getoverlappingareas() or getoverlappingbodies()

If the value is not empty then it overlaps with an area and you will have to move or remove it programmatically

I made a TemplateStaticBody as follows:

StaticBody2D (TemplateStaticBody)
| -> Sprite
     | -> Area2D
          | -> CollisionShape2D (For area2d detection)
| -> CollisionShape2D (For static body)

Then I made two inherited scenes one BlackPawn and one WhitePawn.
Created a Node2D scene (World) and added them inside overlapping each other. Then made a script to that World, and did the following:

func _ready():

This did not show any overlapping areas.
Then, did a physicsprocess call:

func _physics_process(delta):

Output was:


Which proves that there are overlapping bodies. I tried to yield one idle frame in ready() but it didn't produce the overlapping either, so it needs a minimum threshold of time to detect the overlapping areas that _physicsprocess does.

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.