![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | indy2005 |
I am randomly placing nodes onto a target, and want to randomise the positions to the point where there is zero overlap. This happens at the start of the game.
I realised you cant do an overlaps test in the same frame you move things around, so I am trying to yield to the next frame. The issue is, my overlap_count is printing out zero, but I am getting overlaps. My collision shapes are correct, and I am setting the collision masks on the Area2D nodes correctly I think.
func randomise_card_positions():
var rng = RandomNumberGenerator.new()
rng.randomize()
var cards = get_tree().get_nodes_in_group("playing_cards")
var t_size = target.get_node("Sprite").texture.get_size()[0]
t_size = 800 # TODO manual override
for card in cards:
var random_x = rng.randi_range(-t_size/2.0,t_size/2.0)
var random_y = rng.randi_range(-t_size/2.0,t_size/2.0)
var random_angle = rng.randi_range(0,359)
card.rotation_degrees = random_angle
card.position = Vector2(random_x, random_y)
func remove_overlaps():
var cards = get_tree().get_nodes_in_group("playing_cards")
print("Number of cards: " + str(len(cards)))
var overlap_count = 100
while overlap_count > 0:
overlap_count = 0
var overlapping_areas
for card in cards:
overlapping_areas = card.get_overlapping_areas()
if overlapping_areas.empty() == false:
overlap_count += 1
print("Number of overlapping areas: " + str(overlap_count))
if overlap_count > 0:
randomise_card_positions()
yield(get_tree(), "idle_frame")
Output when this completes:
Output in the console:
Number of overlapping areas: 16
Number of overlapping areas: 15
Number of overlapping areas: 12
Number of overlapping areas: 3
Number of overlapping areas: 0
Note: I have also tried yielding to the “physics_frame” but this doesnt work either, the only way I can get it to work is by yielding to a timer of 0.1 seconds, and randomising one overlapping card at a time rather than all of them.