0
\$\begingroup\$

Hey I have this chunking algorithm I am writing and I cant for the life of me figure out how to place the chunks so they are aligned with each other as if they were in a constant grid. The issue resides in the fact that I have a player object that never moves, instead when it "moves" the code moves all the chunks around the player. When a new chunk is moved underneath the player, the chunk loading algorithm is called and places all the nearby chunks. However I cant figure out what algorithm I need to figure out where they are all placed. They always pop up offset to the grid. How do I correct the offset so that they are aligned?

from bge import logic, events

co = logic.getCurrentController()
cameraObj = co.owner
scene = logic.getCurrentScene()
xNeighbors = [-1,0,1,-1,0,1,-1,0,1]
yNeighbors = [-1,-1,-1,0,0,0,1,1,1]
    
def keyHit(key_code): 
    
    status = logic.keyboard.events[key_code]
    
    return status == logic.KX_INPUT_ACTIVE
    
    
def loadChunks(curr):
    chunksToLoad = []
    chunkNeighborsX = []
    chunkNeighborsY = []
    chunksToRemove = []
    currX = curr % 4
    currY = int(curr/4)
    loopIndex = -1
    
    for x in xNeighbors:
        loopIndex += 1
        index = (currX + xNeighbors[loopIndex]) + 4 * (yNeighbors[loopIndex] + currY)+1
        checkX = currX + xNeighbors[loopIndex]
        checkY = currY + yNeighbors[loopIndex]
        if (checkX > 4):
            continue
        if (checkX < 0):
            continue
        if (checkY > 4):
            continue
        if (checkY < 0):
            continue
        chunksToLoad.append(index)
        chunkNeighborsX.append(xNeighbors[loopIndex])
        chunkNeighborsY.append(yNeighbors[loopIndex])
        
    # check scene for the empty object associated with that blend file, if it
    # exists, then there is no need to load the blend file and it can be skipped 
    # when loading the blend files
    

    
    for object in scene.objects:
        libToFree = 0
        loopIndex = 0
        for x in chunksToLoad:
            if (object.name == 'chunk0' + str(x)):
                #blend file that needs to be opened has already been opened
                chunksToLoad.remove(x)
                chunkNeighborsX.pop(loopIndex)
                chunkNeighborsY.pop(loopIndex)
                libToFree = 1
            loopIndex += 1
        if (libToFree == 0):
            if ('chunk0' in object.name):
                chunksToRemove.append(object.name)
                
    loopIndex = 0
    print(chunksToLoad)
    print(chunkNeighborsX)
    print(chunkNeighborsY)
    for x in chunksToLoad:
        try:
            logic.LibLoad('//chunk0' + str(x) + '.blend','Scene')
        except:
            continue
        
        chunkContents=scene.objects['chunk0'+str(x)]
        print(cameraObj['x'])
        print(cameraObj['y'])
        xOffset = 16*(int(cameraObj['x']/16)+1)
        yOffset = 16*(int(cameraObj['y']/16)+1)
        chunkContents.localPosition[0] += (chunkNeighborsX[loopIndex]*16 - 2*cameraObj['x'])
        chunkContents.localPosition[1] += (chunkNeighborsY[loopIndex]*16 - 2*cameraObj['y'])
        loopIndex += 1
        
    for x in chunksToRemove:
        print(x)
        logic.LibFree(logic.expandPath('//' + x + '.blend'))
    
def main():
    
    chunkSize = 16
    chunkChange = 0
    
    if (cameraObj['initState'] == 0):
        loadChunks(0)
        cameraObj['initState'] = 1
    
    if (keyHit(events.AKEY)):
        cameraObj['y'] = cameraObj['y'] + 0.1
        for object in scene.objects:
            if (object.name != 'Camera'):
                object.localPosition[1] = object.localPosition[1] - 0.1
    
    if (keyHit(events.WKEY)):
        cameraObj['x'] = cameraObj['x'] + 0.1
        for object in scene.objects:
            if (object.name != 'Camera'):
                object.localPosition[0] = object.localPosition[0] - 0.1
    
    y = cameraObj['y']
    x = cameraObj['x']
    currChunk = cameraObj['currChunk']
    checkChunk = int((y)/16) * 4 + int((x)/16) + 1
    
    if (currChunk != checkChunk):
        cameraObj['currChunk'] = checkChunk
        loadChunks(checkChunk - 1)

        
main()



\$\endgroup\$

You must log in to answer this question.

Browse other questions tagged .