2
\$\begingroup\$

I have piece of code that using a tile set, i can draw an area of what is going to be a small town/city. The code seed_cities, make an X number of cities randomly on the map, whereas draw_city just paints the city over the screen.

My problem is that from time to time, 2 or more citites overlap in the map.

func seed_cities(num_cities):
    var initial_point 
    var final_point 
    for i in range(0, num_cities):
        initial_point = Vector2(int (rand_range(0, screen_size.x/10)), int (rand_range(0, screen_size.y/10)) )
        range_x = randi() % 10 + 5  # 5 being minium longitude
        range_y = randi() % 10 + 5  # 5 being minium longitude
        final_point = Vector2(range_x, range_y)
        var city_position = {
                initial_point: initial_point,
                final_point: final_point
        }
        cities.append(city_position)
        # TODO check if there is another city before
        draw_city(final_point,final_point)

now, i tried to use the function line_intersects_line_2d , but the appears an error saying that line_intersects_line_2d is not declared on the current class, so i have another method of checking if the cities overlap. i was thinking of using that or creating an area2D with code to check if there is a collision.

func draw_city(initial_point, final_point):
    for x in range(0,final_point.x):
         $Objects.set_cell(initial_point.x + x , initial_point.y,4)
         $Objects.set_cell(initial_point.x + x, initial_point.y + final_point.y,4)
    for y in range(0,final_point.y+1):
         $Objects.set_cell(initial_point.x, initial_point.y + y,4)
         $Objects.set_cell(initial_point.x + final_point.x, initial_point.y +y,4)

Any ideas on what i need to import to use line_intersects_line_2d? What other way i can check if 2 cities overlap before saving it's position and draw them on the map?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Did you consider using Rect2.intersects? \$\endgroup\$
    – DMGregory
    Commented Jul 1, 2020 at 19:26
  • \$\begingroup\$ Actually didn't know that thing exist, thank you, will try that \$\endgroup\$
    – Progs
    Commented Jul 1, 2020 at 19:51
  • 1
    \$\begingroup\$ If it works for you, be sure to post your solution as an Answer below! \$\endgroup\$
    – DMGregory
    Commented Jul 1, 2020 at 19:54
  • \$\begingroup\$ @DMGregory, yes that did it! ty \$\endgroup\$
    – Progs
    Commented Jul 8, 2020 at 18:40

0

You must log in to answer this question.

Browse other questions tagged .