How can I randomly place several non-colliding rects?

前端 未结 7 1681
谎友^
谎友^ 2020-12-31 16:55

I\'m working on some 2D games with Pygame. I need to place several objects at the same time randomly without them intersecting. I have tried a few obvious

相关标签:
7条回答
  • 2020-12-31 17:50

    Did you try:

    Until there are enough objects:
        create new object
        if it doesn't collide with anything in the list:
            add it to the list
    

    No sense recreating the entire list, or taking out everything that's involved in a collision.

    Another idea is to "fix" collisions by either of the following approaches:

    1) Find the center of the region of intersection, and adjust the appropriate corner of each intersecting rect to that point, such that they now touch on corner/edge instead of intersecting.

    2) When a rectangle collides with something, randomly generate a sub-region of that rectangle and try that instead.

    0 讨论(0)
提交回复
热议问题