An optimized way to detect overlapping nodes

Godot Version 4.5

4.5

an optimized way to detect overlapping sprites

I’m adding trees to my procedurally generated road game, and my code currently generates them and then detects if each one is on the road using a 2D area

However, when creating many trees, the game lags significantly. Do you know how to fix this?

Improving your algorithm to place trees away from the roads would be best, but if you are stuck on Area2Ds then using direct physics before instantiation would help

var state := get_world_2d().direct_space_state
var shape_rid := PhysicsServer2D.sphere_shape_create()
PhysicsServer2D.shape_set_data(shape_rid, 10) # radius

var shape_query := PhysicsShapeQueryParameters2D.new()
shape_query.shape_rid = shape_rid

var results: Array[Dictionary] = state.intersect_shape(shape_query)

1 Like