Skip to content

Collision not working at all.  #26

@xenonmusic

Description

@xenonmusic

@RedHenDev Below is my entire main.py I honestly have no idea why the collisions aren't detecting, i've triple checked my code with the code in your videos. Any help would be very much appreciated!

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from numpy import floor
from perlin_noise import PerlinNoise
from mesh_terrain import MeshTerrain

app = Ursina()

window.color = color.rgb(127, 167, 219)
window.exit_button.visible = False
window.fullscreen=True
indra = Sky()
indra.color = window.color

terrain = MeshTerrain()

player = FirstPersonController()
player.y = 20
player.gravity = 0.0
player.cursor.visible=False

pX = player.x
pZ = player.z

def input(key):
    terrain.input(key)

count = 0
def update():
    global count, pX, pZ
    
    # generate terrain at current swirl pos
    
    terrain.genTerrain()
    
    count+=1
    if count >= 1:

        count=0
        
        # highlight terrain block selected at crosshair
        terrain.update(player.position,camera)
    
    # change subset pos based on player pos
    if abs(player.x-pX)>4 or abs(player.z-pZ)>4:
        pX=player.x
        pZ=player.z
        terrain.swirlEngine.reset(pX,pZ)
    
    blockFound=False
    step = 2
    height = 1.86
    x = str(floor(player.x+0.5))
    z = str(floor(player.z+0.5))
    y = floor(player.y+0.5)
    for i in range(-step,step):
        if terrain.td.get("x"+x+"y"+str(y+i)+"z"+z)=="t":
            target = y+i+height
            blockFound=True
            break

    if blockFound==True:
        # step up or down
        player.y = lerp(player.y, target, 6 * time.dt)
    else:
        player.y -= 9.8 * time.dt
        # gravity fall 

terrain.genTerrain()

app.run()

###############################################################################################
### Below this is my whole mesh terrain module, not sure why it didnt separate in markdown ###
###############################################################################################

from ursina import *
from perlin import Perlin
from random import random
from swirl_engine import SwirlEngine
from mining_system import *

class MeshTerrain:
    def __init__(this):
    
        this.block = load_model('block.obj')
        this.collider = 'box'
        this.textureAtlas = 'texture_atlas_3.png'
        this.numVertices = len(this.block.vertices)
        
        this.subsets = []
        this.numSubsets = 512
        this.subWidth = 8
        this.swirlEngine = SwirlEngine(this.subWidth)
        this.currentSubset = 0
        
        # terrain dictionary
        this.td = {}
        
        # vertex dictionary (mining)
        this.vd = {}
        
        
        this.perlin = Perlin()
        
        for i in range(0,this.numSubsets):
            e = Entity( model=Mesh(),
                        texture=this.textureAtlas)
            e.texture_scale*=64/e.texture.width
            this.subsets.append(e)
            
    # highlight selected block at crosshair
    def update(this,pos,cam):
        highlight(pos,cam,this.td)
                
    def input(this,key):
        if key=='left mouse up' and bte.visible==True:
            epi = mine(this.td,this.vd,this.subsets)
            if epi != None:
                this.genWalls(epi[0],epi[1])
                this.subsets[epi[1]].model.generate()
            
    # oh.
    def genWalls(this,epi,subset):
        if epi==None: return
        wp =    [   Vec3(0,1,0),
                    Vec3(0,-1,0),
                    Vec3(-1,0,0),
                    Vec3(1,0,0),
                    Vec3(0,0,-1),
                    Vec3(0,0,1),]
        for i in range(0,5):
            np = epi + wp[i]
            if this.td.get( 'x'+str(floor(np.x))+
                            'y'+str(floor(np.y))+
                            'z'+str(floor(np.z)))==None:
                this.genBlock(np.x,np.y,np.z,subset,gap=False)
        
    def genBlock(this,x,y,z,subset=-1,gap=True):
        if subset ==-1: subset=this.currentSubset
        # Extend the vertices of model
        model = this.subsets[subset].model
        
        model.vertices.extend([ Vec3(x,y,z) + v for v in
                                this.block.vertices])
        # record terrain in dict
        this.td["x"+str(floor(x))+
                "y"+str(floor(y))+
                "z"+str(floor(z))] = "t"
        # record gap above pos to correct
        if gap==True:
            key =  ("x"+str(floor(x))+
                    "y"+str(floor(y+1))+
                    "z"+str(floor(z)))
            if this.td.get(key)==None:
                this.td[key] = "g"
            
        # record subset index and first vertex of current block
        vob = (subset, len(model.vertices)-37)
        this.vd["x"+str(floor(x))+
                "y"+str(floor(y))+
                "z"+str(floor(z))] = vob
        
        # pick random color tint for block texture
        c = random()-0.776
        model.colors.extend((Vec4(1-c,1-c,1-c,1),)*
                               this.numVertices)
        
        # texture atlas coordinate for grass
        uu = 8
        uv = 7
        if y > 2:
            uu = 8
            uv = 6
        model.uvs.extend([Vec2(uu,uv) + u for u in this.block.uvs])
        
    def genTerrain(this):
        # get current pos as chunks load
        x = floor(this.swirlEngine.pos.x)
        z = floor(this.swirlEngine.pos.y)
        
        d = int(this.subWidth*0.5)
        
        for k in range(-d,d):
            for j in range(-d,d):
            
                y = floor(this.perlin.getHeight(x+k,z+j))
                if this.td.get( "x"+str(floor(x+k))+
                                "y"+str(floor(y))+
                                "z"+str(floor(z+j)))==None:
                    this.genBlock(x+k,y,z+j)
                
        this.subsets[this.currentSubset].model.generate()
        # current subset
        if this.currentSubset<this.numSubsets-1:
            this.currentSubset+=1
        else: this.currentSubset=0
        this.swirlEngine.move()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions