How can I create a RTS like building Mode with collision detection?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By NVA_Lee22
:warning: Old Version Published before Godot 3 was released.

What i want is that in buildMode the collision of the Building do not affect other shapes. Only be a visible Sprite to show where the Building can placed. If the building is placed then it needs the collision back again to prevent players to run through it.

My nodes:


Node2D [Main]

TileMap [Map]

KinematicBody2D [Player]

Sprite [Image]
Camera2D
CollisionPolygon2D [Collision]

Node2D [Buidlings]

KinematicBody2D [Building01]

Sprite [Image]
CollisionPolygon2D [Collision]


Szenario (short to explain):

# Variables
var allowPlace = false 
var buildMode = false

# on BuildMode Start:
buildMode = true
Building01.get_node("collision").set_trigger(true)

# on Fixed Process:
if buildMode:
    Building01.move_to(get_global_mouse_pos())
    var b = Buidling01.get_node("Collision")
    # How to do that here in a right way to check overlapping or collision ?
    if (b.is_colliding() || b.get_overlapping_bodies().size() > 0):
        allowPlace : false
    else:
        allowPlace: true

# on Mouseclick
if allowPlace:
    buildMode = false
    Building01.get_node("Collision").set_trigger(false)
else:
    print("OMG! You cant place a building on a Player or a collision map tile!")

Well I tried to use an Area2D instead of a KinematicBody2D for the Building01. There i can check if the Building01 is over another Shape but then i still have no collission when it is placed. That means my Player can run through the Building (although is_trigger equals false when placed).

If i use a KinematicBody2D then i cant really check if the Building is acutally over a Player or StaticBody2D with collision (while buildMode) but here i have the collision what prevents the player to run through the building.

I also tried to use a KinematicBody2D AND the Area2D in the [Building01] Node. But then they detect itselft as overlapping shape what will always return the wrong value for allowPlace.

I´m really confused how to fix that problem. Can anyone help me here ?

:bust_in_silhouette: Reply From: NVA_Lee22

I got a solution which works fine for me.

Create the following scene as your Building:

Node2D

  • StaticBody2D [Housecollisison]
    – CollisionPolygon2D [Option Trigger set to true]
  • Area2D [Check overlapping]
    – CollisionPolygon2D

Then after you instance your Buildingsscene you have to attach that building to the mousepointer until you click LMB or RMB.

While the building ist following your cursor you have to check if the Area2D from the building have overlapping bodies on it. That will always return true because of its own StaticBody2D in it. So you have to get a list of these overlapping bodies by calling

area2D_node.get_overlapping_bodies()

then you have to get alle the childs of your building scene and remove the Area2D from the array (array.erase()) then check if the overlapping bodies == the Childs of your building (Area2D erased)
If it return true then you have no other collisison and can place the building by deactivate the building from following the cursor and set the “Trigger” option in the staticBody2D → CollisionPolygon2D to false.

Greetings