mardi 28 juillet 2015

Randomly generated rectangles do not fully complete separation, yet the program moves on

I am working on a random generation system for my game. I wanted to create a given number of randomly placed rooms that do not intersect. I've mocked up some code to do this for me, but there is one problem: it tells the program that it is finished separating long before it actually is. Here is some pseudo-code for you wonderful people:

generate all things required to draw properly on canvas

set how many rooms are to be placed
grab possible room sizes

new 'rooms' array
for numRoomsToPlace
    select random location on the canvas, clamped towards the center
    create a room of random size at location and push to rooms array

continueLoop = true
loopTimeout = 20
while continueLoop = true
    noIntersection = true
    loop through all active rooms (variable q)
        loop through all active rooms so that we have a pair (variable j)
            get room 1 (represented as q)
            get room 2 (represented as j)
            check if rooms intersect on both axes
            if rooms intersect
                noIntersection = false
                if it would be shorter to move along the x axis
                    shift both rooms half the distance required in opposite directions
                else, it would be shorter to move along the y axis
                    shift both rooms half the distance required in opposite directions
    loopTimeout--;
    if noIntersection || loopTimeout<=0
        continueLoop = false

draw all the things

So now that is finished, here is a working demo in javascript: http://ift.tt/1LVWjFt

I cannot for the life of me figure out why the rooms are not fully separating. The while loop is NOT timing out. I put in detection and the program thinks that it is fully separating the rooms. I'm sure there is a simple solution and that I've just been looking at this problem for too long. The more brains the better, so that's why I'm here! Thank you guys for helping in advance, you are always awesome.




Aucun commentaire:

Enregistrer un commentaire