0 Replies - 497 Views - Last Post: 15 December 2018 - 10:40 AM Rate Topic: -----

#1 Rick Bowen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-December 18

Updating Particles In Python3 Using Pyglet

Posted 15 December 2018 - 10:40 AM

Hello. I am having some problems updating particles' positions in my 3D python project. I have created a particle class with an update function but whenever I run the update function the particles' positions don't seem to update even though the camera's position does when I run it's update function. I'm a big noob so it might be right there without me noticing but if you know the solution, please answer! It would be much appreciated.

Here's the Particle class v

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()


Here's how I load them in inside the Window class v

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()


And just in case you want to see it, here's the Camera class v

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


I hope you might be able to help me. Thank you in advance to anyone who can!

Is This A Good Question/Topic? 0
  • +

Page 1 of 1