Entry 6169

Explode.py

   

Submitted by anonymous on Aug. 17, 2010 at 5 p.m.
Language: Python. Code size: 3.1 KB.

from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from math import *
from numpy import *
import random
import sys

NUM_PARTICLES = 500
NUM_DEBRIS = 500
fuel = 0
angle = 0.0

explodeList = 0

class ParticleData:
	def __init__ (self):
		self.position = [0 for x in range (3)]
		self.speed = [0 for x in range (3)]
		self.color = [0 for x in range (3)]

class DebrisData:
	def __init__ (self):
		self.position = [0 for x in range (3)]
		self.speed = [0 for x in range (3)]
		self.orientation = [0 for x in range (3)]
		self.color = [0 for x in range (3)]
		self.scale = [0 for x in range (3)]

particles = [ParticleData () for x in range (NUM_PARTICLES)]
debris = [DebrisData () for x in range (NUM_DEBRIS)]



def newSpeed ():
	x = 2.0 * random.random () - 1.0 
	y = 2.0 * random.random () - 1.0
	z = 2.0 * random.random () - 1.0
	
	return [x, y, z]

def newExplosion ():
	for i in range (NUM_PARTICLES):
		particles[i].position = [0.0, 0.0, 0.0]
		particles[i].color = [1.0, 1.0, 0.5]
		particles[i].speed = newSpeed ()

	for i in range (NUM_DEBRIS):
		debris[i].position = [0.0, 0.0, 0.0]
		debris[i].orientation = [0.0, 0.0, 0.0]
		debris[i].color = [0.5, 0.0, 0.0]

		debris[i].scale = [(2.0 * random.random () - 1.0) for x in range (3)]
		debris[i].speed = newSpeed ()
		debris[i].orientationSpeed = newSpeed ()

	global fuel
	fuel = 1000

def InitExplosion ():
	global explodeList
	newSpeed ()
	newExplosion ()

def StartExplode ():
	glPushMatrix ()
	glLoadIdentity ()

	glTranslatef (0.0, 0.0, -10.0)
	glRotatef (angle, 0.0, 1.0, 0.0)

	if (fuel > 0):
		glDisable (GL_LIGHTING)
		glPushMatrix ()
		glDisable (GL_DEPTH_TEST)
		glBegin (GL_POINTS)
		for i in range (NUM_PARTICLES):
			glColor3fv (particles[i].color)
			glVertex3fv (particles[i].position)
		glEnd ()
		glPopMatrix ()
		glEnable (GL_LIGHTING)
		glEnable (GL_LIGHT0)
		glEnable (GL_DEPTH_TEST)
		glNormal3f (0.0, 0.0, 1.0)

		for i in range (NUM_DEBRIS):
			glColor3fv (debris[i].color)
			glPushMatrix ()
			glTranslatef (debris[i].position[0], debris[i].position[1], 
					debris[i].position[2])
			glRotatef (debris[i].orientation[0], 1.0, 0.0, 0.0)
			glRotatef (debris[i].orientation[1], 0.0, 1.0, 0.0)
			glRotatef (debris[i].orientation[2], 0.0, 1.0, 0.0)

			glScalef (debris[i].scale[0], debris[i].scale[1], 
					debris[i].scale[2])
			glBegin (GL_TRIANGLES)
			glVertex3f (0.0, 0.5, 0.0)
			glVertex3f (-0.25, 0.0, 0.0)
			glVertex3f (0.25, 0.0, 0.0)
			glEnd ()

			glPopMatrix ()

	glPopMatrix ()


def SetExplosionParameter (bActive = False):
	if (not bActive):
		return

	global fuel
	if (fuel > 0):
		for i in range (NUM_PARTICLES):
			particles[i].position = add (particles[i].position, 
					multiply (particles[i].speed ,0.2))

			particles[i].color = subtract (particles[i].color,
					[(1.0 / 500.0) for x in range (3)])

			for j in range (3):
				if (particles[i].color[x] < 0):
					particles[i].color[x] = 0

		for i in range (NUM_DEBRIS):
			debris[i].position = add (multiply (debris[i].speed , 0.1),
					debris[i].position)
			debris[i].orientation = add (multiply (debris[i].orientationSpeed,
						10), debris[i].orientation)

		fuel -= 1
	global angle 
	angle += 0.1

This snippet took 0.03 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).