in this code I build a matrix (tile heights)
then I create all the tiles - I need to connect up all the tiles vertex to create a terrain.
I need to store a vertex displacement list for each tile so when it is spawned, or it’s physics is spawned, I can deform them.
I create a list right now for each tile (neighbor tiles) (PL=[])
I am having trouble understanding how to loop through the data I have and create this ‘vertex displacement list’
then the tiles will be removed,
LOD adds the tiles back + applies the ‘Vertex displacement list’ to the newly added mesh as needed.
The part I need help with, is what to do after
#Get all neighbors for each tile
for object in store:
x = object[4]
y = object[5]
print(x)
print(y)
PL = []
if x>=1:
T1 = own['Matrix'][x-1][y]
PL.append([T1,x-1,y])
if y<=98:
T5 = own['Matrix']
PL.append([T5,x-1,y+1])
if x<=98:
T2 = own['Matrix'][x+1][y]
PL.append([T2,x+1,y])
if y<=1:
T3 = own['Matrix'][x+1][y+1]
PL.append([T3,x+1,y+1])
elif y>=1:
T4 = own['Matrix'][x+1][y-1]
PL.append([T4,x+1,y-1])
if y>=1:
T6 = own['Matrix'][x][y-1]
PL.append([T6,x,y-1])
so T1 - T6 = neighbor tiles that may or may not exist.
[Neighbor,X_location in matrix,Y_location in matrix]
Height = own[‘Matrix’][X][Y]*.5
#Full script
import bgeimport random
import mathutils
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
if 'Matrix' not in own:
#Generate noise in a 100x100 array
own['Actor']=own.scene.objects['Actor']
random.seed('Blank')
own['Matrix'] = [[0]*100 for i in range(100)]
for x in range(100):
for y in range(100):
own['Matrix'][x][y]=random.random()
minx = own.worldPosition.x-50
miny = own.worldPosition.y-50
index=0
store =[]
#create game objects for each point and store position and other info in a list
for x in range(100):
X=minx+x
for y in range(100):
Y =miny+y
added = own.scene.addObject('Tile',own,0)
added.worldPosition = (X,Y,own['Matrix'][int(x)][int(y)]*.5)
index+=1
store.append([added, added.worldPosition.copy(),added['Type'],0,int(x),int(y)])
#Get all neighbors for each tile
for object in store:
x = object[4]
y = object[5]
print(x)
print(y)
PL = []
if x>=1:
T1 = own['Matrix'][x-1][y]
PL.append([T1,x-1,y])
if y<=98:
T5 = own['Matrix']
PL.append([T5,x-1,y+1])
if x<=98:
T2 = own['Matrix'][x+1][y]
PL.append([T2,x+1,y])
if y<=1:
T3 = own['Matrix'][x+1][y+1]
PL.append([T3,x+1,y+1])
elif y>=1:
T4 = own['Matrix'][x+1][y-1]
PL.append([T4,x+1,y-1])
if y>=1:
T6 = own['Matrix'][x][y-1]
PL.append([T6,x,y-1])
#BuildKDTree
size = len(store)
kd = mathutils.kdtree.KDTree(size)
for i in range(size):
kd.insert(store[i][1], i)
kd.balance()
own['KD']=kd
own['Store']=store
else:
#Use KDTRee for Object LOD and PhysicsLOD
SClose=[]
close =[]
kd = own['KD']
# Find points within a radius of the 3d cursor
co_find = own['Actor'].worldPosition
for (co, index, dist) in kd.find_range(co_find,5):
SClose.append(index)
if own['Store'][index][0].invalid:
strn=own['Store'][index][2]+"_Physics"
own['Store'][index][0]=own.scene.addObject(own['Store'][index][2],own,0)
added=own.scene.addObject(strn,own,0)
own['Store'][index][0]['Phys']=added
own['Store'][index][0].worldPosition=own['Store'][index][1]
own['Store'][index][0]['Phys'].worldPosition=own['Store'][index][1]
own['Store'][index][3]=1
own['added']+=1
elif own['Store'][index][3]==1:
if 'Phys' not in own['Store'][index][0]:
print(own['Store'][index][0].getPropertyNames())
strn=own['Store'][index][2]+"_Physics"
own['Store'][index][0]['Phys']=own.scene.addObject(strn,own,0)
own['Store'][index][0]['Phys'].worldPosition=own['Store'][index][1]
#own['added']+=1
for (co, index, dist) in kd.find_range(co_find,10):
if index not in SClose:
close.append(index)
if own['Store'][index][0].invalid:
own['Store'][index][0]=own.scene.addObject(own['Store'][index][2],own,0)
own['Store'][index][0].worldPosition=own['Store'][index][1]
own['Store'][index][3]=1
else:
if 'Phys' in own['Store'][index][0]:
own['Store'][index][0]['Phys'].endObject()
del own['Store'][index][0]['Phys']
print(index)
for (co, index, dist) in kd.find_range(co_find, 12):
if index not in close and index not in SClose:
if own['Store'][index][3]==1:
try:
own['Store'][index][0].endObject()
own['Store'][index][3]=0
except:
n=False
main()
system working so far-