Check if two shapes overlapping

Godot Version

4.2.1

Question

Hi there,

Is there a better method to detect if two cubes are overlapping besides using Area3D.overlaps_area()? For context, I’m making rooms within a hotel and I want to check if a newly spawned one overlaps with any other rooms before spawning it completely. The issue is that overlaps_area() needs to wait until the next frame to correctly check if an area overlaps another. This is making the method I made not return any collisions. I’d need something that allows detecting collisions between two shapes instantly.

I could probably just do some transformation math myself, but I’ll be honest I’m lazy and don’t want to deal with AABB and rotation math/vectors.

Anyway, is there another way to detect this that I’m missing?

Or does anyone know how to get the AABB of something that is not a mesh? For instance, a collisionshape or an area3d?

Hey, thanks for the method, it seems to be what I was looking for.

I was wondering how I should use it though? I tried to create a new instance for it, but since it’s an abstract class I can’t to call the methods it has. Is there some way to call it and it’s functions?

I was able to figure out how to get it working, for any one with the same issue in the future, here is my code:

extends Node3D

@onready var shape_to_check = $Area3D/CollisionShape3D
var shape_query = null
var direct_physics_state = null

func _ready():
	direct_physics_state = get_world_3d().direct_space_state # Get physics state from 3d world
	shape_query = PhysicsShapeQueryParameters3D.new() # create a new query
	shape_query.shape = shape_to_check.shape # set shape to collision-shape shape
	shape_query.collide_with_bodies = false # Disable body collides
	shape_query.collide_with_areas = true # Enable area collides
	shape_query.collision_mask = 4096 # Set detection mask to unique value -> only want to detect rooms
	shape_query.transform = shape_to_check.global_transform # Set transform to collisionshapes transform

func _process(delta):
	if shape_query != null:
		var intersected_areas = direct_physics_state.intersect_shape(shape_query) # Check if shape collides with anything
		if len(intersected_areas) > 1: # If there are more than 1 intersections, colliding with another area (seems to detect itself too)
			intersected_areas.pop_front() # Remove first index, it is always the original shape
			print("Colliding with objects!")
			print(intersected_areas)
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.