使用Pyglet更新Python3中的粒子

时间:2018-12-15 05:39:03

标签: python 3d position pyglet particles

我在更新python 3D游戏项目中的粒子位置时遇到了一些麻烦。我已经创建了带有更新功能的粒子类,但是当我运行更新功能时,即使相机的位置确实没有变化,粒子的位置也似乎没有更新。

这里是一些代码(c表示计数,r是范围,t是纹理,可能有点明显,但以防万一):

class Particle:
    def get_tex(self, file):
        tex = pyglet.image.load(file).texture
        glTexParameterf(GL_TEXTURE_2D,  GL_TEXTURE_MIN_FILTER,GL_NEAREST)
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
        return pyglet.graphics.TextureGroup(tex)

    def __init__(self, x, y, z, c, r, t):
        self.x = x
        self.tex = self.get_tex(t)

        self.batch = pyglet.graphics.Batch()

        tex_coords = ('t2f', (1,0, 0,0, 0,1, 1,1, ))

        i = 0
        while i <= c:
            i += 1
            j = random.uniform((r*-0.5)-0.5,(r*0.5)-0.5)
            k = random.uniform((r*-0.5)-0.5,(r*0.5)-0.5)
            l = random.uniform((r*-0.5)-0.5,(r*0.5)-0.5)
            self.batch.add(4, GL_QUADS,self.tex,('v3f',(x+j, y+k, z+l, x+j+0.1, y+k, z+l+0.1, x+j+0.1, y+k+0.1, z+l+0.1, x+j, y+k+0.1, z+l,)), tex_coords)
            self.batch.add(4, GL_QUADS,self.tex,('v3f',(x+j, y+k, z+l+0.1, x+j+0.1, y+k, z+l, x+j+0.1, y+k+0.1, z+l, x+j, y+k+0.1, z+l+0.1,)), tex_coords)

    def update(self, dt):
        s = dt*10
        self.x -= s*0.1

    def draw(self):
        self.batch.draw()

那是粒子类^(同样很明显……)

这是window类中的一些代码片段,它是更新功能

class Window(pyglet.window.Window):
    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.set_minimum_size(925, 500)
        self.keys = key.KeyStateHandler()
        self.push_handlers(self.keys)
        pyglet.clock.schedule(self.update)
        # DEFINING THE CAMERA
        self.player = Player((0.5,1.5,1.5),(-30,0))

        # DEFINING THE DRAWINGS
        self.part0 = Particle(0, 2, 0, 100, 1, 'textures\\particle1.png')

    def update(self,dt):
        self.player.update(dt,self.keys)
        self.part0.update(dt)
        self.part0.draw()

    def on_draw(self):
        self.clear()
        self.set3d()
        self.push(self.player.pos,self.player.rot)
        # DRAWING THE MODELS
        self.part0.draw()

以防万一,您也想在这里看到相机功能

class Player:
    def __init__(self,pos=(0,0,0),rot=(0,0)):
        self.pos = list(pos)
        self.rot = list(rot)

    def mouse_motion(self,dx,dy):
        dx/=4; dy/=4; self.rot[0]+=dy; self.rot[1]-=dx
        if self.rot[0]>90: self.rot[0] = 90
        elif self.rot[0]<-90: self.rot[0] = -90

    def update(self,dt,keys):
        s = dt*10
        rotY = -self.rot[1]/180*math.pi
        dx,dz = s*math.sin(rotY),s*math.cos(rotY)
        if keys[key.W]: self.pos[0]+=dx*0.4; self.pos[2]-=dz*0.4
        if keys[key.S]: self.pos[0]-=dx*0.4; self.pos[2]+=dz*0.4
        if keys[key.A]: self.pos[0]-=dz*0.4; self.pos[2]-=dx*0.4
        if keys[key.D]: self.pos[0]+=dz*0.4; self.pos[2]+=dx*0.4
        if keys[key.SPACE]: self.pos[1]+=s*0.4
        if keys[key.LCTRL]: self.pos[1]-=s*0.4

在此先感谢您能提供帮助的人!


1 个答案:

答案 0 :(得分:0)

已解决!我会将代码提供给任何想知道我如何解决它的人。

(我忘记在最初的问题中添加推矩阵,并意识到这是关键)

首先,推送矩阵:

def push_p(self,pos,rot): glPushMatrix(); glRotatef(-rot[0],1,0,0); glRotatef(-rot[1],0,1,0); glTranslatef(-self.player.pos[0] - pos[0],-self.player.pos[1] + pos[1],-self.player.pos[2] - pos[2],)

第二,粒子类:

class Particle:
    def get_tex(self, file):
        tex = pyglet.image.load(file).texture
        glTexParameterf(GL_TEXTURE_2D,  GL_TEXTURE_MIN_FILTER,GL_NEAREST)
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
        return pyglet.graphics.TextureGroup(tex)

    def __init__(self, pos, rot, c, r, t):
        self.pos = list(pos)
        self.rot = list(rot)
        self.tex = self.get_tex(t)

        self.x = self.pos[0]
        self.y = self.pos[1]
        self.z = self.pos[2]

        self.batch = pyglet.graphics.Batch()

        tex_coords = ('t2f', (1,0, 0,0, 0,1, 1,1, ))

        i = 0
        while i <= c:
            i += 1
            j = random.uniform((r*-0.5)-0.5,(r*0.5)-0.5)
            k = random.uniform((r*-0.5)-0.5,(r*0.5)-0.5)
            l = random.uniform((r*-0.5)-0.5,(r*0.5)-0.5)
            self.batch.add(4, GL_QUADS,self.tex,('v3f',(self.pos[0]+j, self.pos[1]+k, self.pos[2]+l, self.pos[0]+j+0.3, self.pos[1]+k, self.pos[2]+l+0.3, self.pos[0]+j+0.3, self.pos[1]+k+0.3, self.pos[2]+l+0.3, self.pos[0]+j, self.pos[1]+k+0.3, self.pos[2]+l,)), tex_coords))
            self.batch.add(4, GL_QUADS,self.tex,('v3f',(self.pos[0]+j, self.pos[1]+k, self.pos[2]+l+0.3, self.pos[0]+j+0.3, self.pos[1]+k, self.pos[2]+l, self.pos[0]+j+0.3, self.pos[1]+k+0.3, self.pos[2]+l, self.pos[0]+j, self.pos[1]+k+0.3, self.pos[2]+l+0.3,)), tex_coords))
            self.batch.add(4, GL_QUADS,self.tex,('v3f',(self.pos[0]+j, self.pos[1]+k+0.3, self.pos[2]+l, self.pos[0]+j+0.3, self.pos[1]+k+0.3, self.pos[2]+l+0.3, self.pos[0]+j+0.3, self.pos[1]+k, self.pos[2]+l+0.3, self.pos[0]+j, self.pos[1]+k, self.pos[2]+l,)), tex_coords))
            self.batch.add(4, GL_QUADS,self.tex,('v3f',(self.pos[0]+j, self.pos[1]+k+0.3, self.pos[2]+l+0.3, self.pos[0]+j+0.3, self.pos[1]+k+0.3, self.pos[2]+l, self.pos[0]+j+0.3, self.pos[1]+k, self.pos[2]+l, self.pos[0]+j, self.pos[1]+k, self.pos[2]+l+0.3,)), tex_coords))

    def update(self, dt):
        s = dt*10
        self.pos[1] -= s*0.2
        self.pos[0] += s*0.1
        if self.pos[1] <= self.y - 15: self.pos[1] += 15
        if self.pos[0] >= self.x + 15: self.pos[0] -= 15


    def draw(self):
        self.batch.draw()

最后,窗口中的图形:

    def on_draw(self):
        self.clear()
        self.set3d()

        self.push_p(self.part0.pos, self.player.rot)

        self.part0.draw()

        glPopMatrix()

欢迎任何遇到此问题的人。