I am having a little troubles updating particle positions in my python 3D Game project. I have created a particle class with an update function but when I run the update function the particles' positions don't seem to update even though the camera's position does.
Here's Some Code (c means count, r is range, and t is texture which is probably a bit obvious but just in case):
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()
That is the particle class ^ (again probably quite obvious...)
And this is some of the snippets in the window class and it's update function
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 the camera function as well here it is 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
Thank you in advance to anyone who can help!
SOLVED! I will put the code for anyone who wants to know how i fixed it.
(I forgot to add the push matrix in my initial question and realized it was the key)
first of all, the push matrix:
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],)
Secondly, the particle class:
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()
And finally, the drawing within the window:
def on_draw(self):
self.clear()
self.set3d()
self.push_p(self.part0.pos, self.player.rot)
self.part0.draw()
glPopMatrix()
You're welcome to anyone who comes across this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.