pokepetter / ursina

A game engine powered by python and panda3d.
https://pokepetter.github.io/ursina/
MIT License
2.13k stars 321 forks source link

May bad terrain generation... #635

Closed DrFHMNBU closed 7 months ago

DrFHMNBU commented 7 months ago

I see a deterministic discrepance with the terrain generation respect to my camera. Im only seeing it or this is a bug? My code is here:

from ursina import *
import math

app = Ursina()

window.borderless = False
window.exit_button.visible = False
window.fps_counter.enabled = False
window.entity_counter.enabled = False
window.collider_counter.enabled = False

cubes = {}

def g(x,y,z):
    cubes[str(x)+" "+str(z)+" "+'0'] = Entity(model='cube', texture='dirt', position=(x, y, z), scale=(1,3,1))
    cubes[str(x)+" "+str(z)+" "+'1'] = Entity(parent='cube', model='cube', texture='grass_side', position=(x, y + 1, z), scale=(1.01,1,1.01))
    cubes[str(x)+" "+str(z)+" "+'2'] = Entity(parent='cube', model='cube', texture='grass', position=(x, y + 1, z), scale=(1,1.01,1))

def getSurface(i,k):
    return int((sin(i/5)+cos(k/5))*5)

for i in range(-10,10):
    for k in range(-10,10):
        surface=getSurface(i,k)
        g(i,surface,k)

walkspeed=4
rotspeed=90
mouse_sensitivity = Vec2(20, 20)

window.color = color.rgb(0,127,255)

def calcular_distancia(x1,y1,z1,x2,y2,z2):
    return ((x1-x2)**2+(y1-y2)**2+(z1-z2)**2)**0.5

def regenerar():
    for i in range(-10,10):
        for k in range(-10,10):
            surface=getSurface(i+camera.x,k+camera.z)

            cubes[str(i)+" "+str(k)+" "+'0'].x=i+camera.x
            cubes[str(i)+" "+str(k)+" "+'0'].y=surface
            cubes[str(i)+" "+str(k)+" "+'0'].z=k+camera.z

            cubes[str(i)+" "+str(k)+" "+'1'].x=i+camera.x
            cubes[str(i)+" "+str(k)+" "+'1'].y=surface+1
            cubes[str(i)+" "+str(k)+" "+'1'].z=k+camera.z

            cubes[str(i)+" "+str(k)+" "+'2'].x=i+camera.x
            cubes[str(i)+" "+str(k)+" "+'2'].y=surface+1
            cubes[str(i)+" "+str(k)+" "+'2'].z=k+camera.z

prev_position_x=camera.x
prev_position_y=camera.y
prev_position_z=camera.z

def update():
    global prev_position_x, prev_position_y, prev_position_z
    # if held_keys['space'] and mouse.locked == True and mouse.visible == False:
        # camera.position += (0, time.dt*walkspeed, 0)
    # if held_keys['x'] and mouse.locked == True and mouse.visible == False:
        # camera.position += (0,-time.dt*walkspeed, 0) 
    if held_keys['w'] and mouse.locked == True and mouse.visible == False:
        camera.position += (math.sin(math.radians(camera.rotation_y)) * time.dt * walkspeed, 0, math.cos(math.radians(camera.rotation_y)) * time.dt * walkspeed)
    if held_keys['s'] and mouse.locked == True and mouse.visible == False:
        camera.position -= (math.sin(math.radians(camera.rotation_y)) * time.dt * walkspeed, 0, math.cos(math.radians(camera.rotation_y)) * time.dt * walkspeed)
    if held_keys['a'] and mouse.locked == True and mouse.visible == False:
        camera.position -= (math.cos(math.radians(camera.rotation_y)) * time.dt * walkspeed, 0, -math.sin(math.radians(camera.rotation_y)) * time.dt * walkspeed)
    if held_keys['d'] and mouse.locked == True and mouse.visible == False:
        camera.position += (math.cos(math.radians(camera.rotation_y)) * time.dt * walkspeed, 0, -math.sin(math.radians(camera.rotation_y)) * time.dt * walkspeed)
    if mouse.left:
        mouse.locked = True
        mouse.visible = False
    if held_keys['escape']:
        mouse.locked = False
        mouse.visible = True
    if mouse.locked == True and mouse.visible == False:
        camera.rotation_y += mouse.velocity[0] * mouse_sensitivity[1]
        camera.rotation_x -= mouse.velocity[1] * mouse_sensitivity[0]
        camera.rotation_x = clamp(camera.rotation_x, -90, 90)
    if getSurface(camera.x,camera.z) + 6 < camera.y - 0.1:
        camera.y-=0.1
    else:
        if getSurface(camera.x,camera.z) + 6 > camera.y + 0.1:
            camera.y+=0.1
    if calcular_distancia(prev_position_x,0,prev_position_z,camera.x,0,camera.z)>1:
        regenerar()
        prev_position_x=camera.x
        prev_position_y=camera.y
        prev_position_z=camera.z
app.run()

So... ¿Its bad generated or it generates good? Sorry my misunderstanding and thanks anyway. Apart of that, i decided to use this default meshes instead external models because I want to resize the terrain height to prevent holes into my terrain... so, I need that 3 models, anyway...

DrFHMNBU commented 7 months ago

I think I solved it:

from ursina import *
import math

app = Ursina()

window.borderless = False
window.exit_button.visible = False
window.fps_counter.enabled = False
window.entity_counter.enabled = False
window.collider_counter.enabled = False

cubes = {}

def g(x,y,z):
    cubes[str(x)+" "+str(z)+" "+'0'] = Entity(model='cube', texture='dirt', position=(x, y, z), scale=(1,3,1))
    cubes[str(x)+" "+str(z)+" "+'1'] = Entity(parent='cube', model='cube', texture='grass_side', position=(x, y + 1, z), scale=(1.01,1,1.01))
    cubes[str(x)+" "+str(z)+" "+'2'] = Entity(parent='cube', model='cube', texture='grass', position=(x, y + 1, z), scale=(1,1.01,1))

def getSurface(i,k):
    return int((sin(i/5)+cos(k/5))*5)

for i in range(-10,10):
    for k in range(-10,10):
        surface=getSurface(i,k)
        g(i,surface,k)

walkspeed=4
rotspeed=90
mouse_sensitivity = Vec2(20, 20)

window.color = color.rgb(0,127,255)

camera.x=0
camera.y=0
camera.z=0

def calcular_distancia(x1,y1,z1,x2,y2,z2):
    return ((x1-x2)**2+(y1-y2)**2+(z1-z2)**2)**0.5

def regenerar():
    for i in range(-10,10):
        for k in range(-10,10):
            surface=int(getSurface(int(i+camera.x),int(k+camera.z)))

            cubes[str(i)+" "+str(k)+" "+'0'].x=int(i+camera.x)
            cubes[str(i)+" "+str(k)+" "+'0'].y=int(surface)
            cubes[str(i)+" "+str(k)+" "+'0'].z=int(k+camera.z)

            cubes[str(i)+" "+str(k)+" "+'1'].x=int(i+camera.x)
            cubes[str(i)+" "+str(k)+" "+'1'].y=int(surface+1)
            cubes[str(i)+" "+str(k)+" "+'1'].z=int(k+camera.z)

            cubes[str(i)+" "+str(k)+" "+'2'].x=int(i+camera.x)
            cubes[str(i)+" "+str(k)+" "+'2'].y=int(surface+1)
            cubes[str(i)+" "+str(k)+" "+'2'].z=int(k+camera.z)

prev_position_x=camera.x
prev_position_y=camera.y
prev_position_z=camera.z

def update():
    global prev_position_x, prev_position_y, prev_position_z
    # if held_keys['space'] and mouse.locked == True and mouse.visible == False:
        # camera.position += (0, time.dt*walkspeed, 0)
    # if held_keys['x'] and mouse.locked == True and mouse.visible == False:
        # camera.position += (0,-time.dt*walkspeed, 0) 
    if held_keys['w'] and mouse.locked == True and mouse.visible == False:
        camera.position += (math.sin(math.radians(camera.rotation_y)) * time.dt * walkspeed, 0, math.cos(math.radians(camera.rotation_y)) * time.dt * walkspeed)
    if held_keys['s'] and mouse.locked == True and mouse.visible == False:
        camera.position -= (math.sin(math.radians(camera.rotation_y)) * time.dt * walkspeed, 0, math.cos(math.radians(camera.rotation_y)) * time.dt * walkspeed)
    if held_keys['a'] and mouse.locked == True and mouse.visible == False:
        camera.position -= (math.cos(math.radians(camera.rotation_y)) * time.dt * walkspeed, 0, -math.sin(math.radians(camera.rotation_y)) * time.dt * walkspeed)
    if held_keys['d'] and mouse.locked == True and mouse.visible == False:
        camera.position += (math.cos(math.radians(camera.rotation_y)) * time.dt * walkspeed, 0, -math.sin(math.radians(camera.rotation_y)) * time.dt * walkspeed)
    if mouse.left:
        mouse.locked = True
        mouse.visible = False
    if held_keys['escape']:
        mouse.locked = False
        mouse.visible = True
    if mouse.locked == True and mouse.visible == False:
        camera.rotation_y += mouse.velocity[0] * mouse_sensitivity[1]
        camera.rotation_x -= mouse.velocity[1] * mouse_sensitivity[0]
        camera.rotation_x = clamp(camera.rotation_x, -90, 90)
    if getSurface(camera.x,camera.z) + 6 < camera.y - 0.1:
        camera.y-=0.1
    else:
        if getSurface(camera.x,camera.z) + 6 > camera.y + 0.1:
            camera.y+=0.1
    if calcular_distancia(prev_position_x,0,prev_position_z,camera.x,0,camera.z)>1:
        regenerar()
        prev_position_x=camera.x
        prev_position_y=camera.y
        prev_position_z=camera.z
app.run()