I’ve played a bit with particles and wrote my own particle system.
I had it working and changed somethings and now it won’t work anymore.
I’ve searched a long time for the wrong code, but i didn’t find anything.
Here’s my code:
ParticleEmitter.java
package com.iceengine.particles;
/**
*
* @author penguin
*/
import org.lwjgl.util.vector.Vector3f;
import org.lwjgl.util.Color;
import java.util.Random;
import org.lwjgl.opengl.GL11;
import com.iceengine.j3d.objects.IObject;
public class ParticleEmitter implements IObject{
private Vector3f position;
private Vector3f Rotation;
private int particleLifetime;
private Color myColor;
private Particle[] Particles;
private float size;
private Vector3f moveTo;
private boolean GenerateLifetime;
private boolean repeat;
public void drawObject(Vector3f theTranslation, Vector3f rotation)
{
GL11.glTranslatef(theTranslation.x, theTranslation.y, theTranslation.z);
GL11.glRotatef(rotation.x + Rotation.x, 1.0f, 0.0f, 0.0f);
GL11.glRotatef(rotation.y + Rotation.y, 0.0f, 1.0f, 0.0f);
GL11.glRotatef(rotation.z + Rotation.z, 0.0f, 0.0f, 1.0f);
Update();
drawParticles();
}
public void rotateX(float angle)
{
Rotation.x = angle;
}
public void rotateY(float angle)
{
Rotation.y = angle;
}
public void rotateZ(float angle)
{
Rotation.z = angle;
}
public ParticleEmitter(Vector3f Position,Vector3f MoveTo, Color color, int lifetime, int particles, float Size, boolean generateLifetime, boolean Repeat)
{
repeat = Repeat;
GenerateLifetime = generateLifetime;
moveTo = MoveTo;
size = Size;
position = Position;
myColor = color;
particleLifetime = lifetime;
Rotation = new Vector3f(0,0,0);
Particles = new Particle[particles];
Random generator = new Random();
for(int i = 0; i < particles; i++)
{
Particles[i] = createParticle();
}
}
public void Update()
{
for(int i = 0; i < Particles.length; i++)
{
Particles[i].Update();
if(!Particles[i].isParticleAlive())
{
if(repeat)
Particles[i] = createParticle();
}
}
}
private Particle createParticle()
{
float V = (float)(Math.random() * 25);
float angle = (float)(Math.random() * 360);
Vector3f velocity = new Vector3f();
velocity.x = (float)Math.sin(angle) * V;// * moveTo.x;
velocity.y = (float)Math.cos(angle) * V;// * moveTo.y;
velocity.z = (((float)Math.random() * 10) - 5) / 10 * V;// * moveTo.z;//generator.nextFloat() % 10)-5) / 10 * V;
//System.out.println("NEW " + " " + position.x + " " + position.y + " " + position.z + " " + angle + " " + V + " " + velocity.x + " " + velocity.y + " " + velocity.z);
int life = 0;
if(GenerateLifetime)
{
life = (int)(Math.random() * particleLifetime);
} else {
life = particleLifetime;
}
return new Particle(size,life, new Vector3f(position.x, position.y, position.z),velocity, myColor);
}
public void drawParticles()
{
for(int i = 0; i < Particles.length; i++)
{
//System.out.println("DRAW " + i + " - " + Particles[i].position.x + " - " + Particles[i].position.y + " - " + Particles[i].position.z + " - " + Particles[i].velocity.x + " - " + Particles[i].velocity.y + " - " + Particles[i].velocity.z + " - " + (System.currentTimeMillis() - Particles[i].startTime));
Particles[i].drawParticle();
}
}
public boolean isWorkDone()
{
boolean workDone = false;
for(int i = 0; i < Particles.length; i++)
{
if(!Particles[i].isParticleAlive())
workDone = true;
}
return workDone;
}
}
Particle.java
package com.iceengine.particles;
/**
*
* @author penguin
*/
import org.lwjgl.util.vector.Vector3f;
import org.lwjgl.util.Color;
import org.lwjgl.opengl.GL11;
public class Particle {
public Vector3f position;
private int lifetime;
public Vector3f velocity;
private Color myColor;
public long startTime;
private boolean alive;
private float size;
public Particle(float Size,int Lifetime, Vector3f Position, Vector3f Velocity, Color color)
{
size = Size;
position = Position;
lifetime = Lifetime;
myColor = color;
velocity = Velocity;
startTime = System.currentTimeMillis();
alive = true;
}
public void Update()
{
// Update Particle
if(alive)
{
if((System.currentTimeMillis() - startTime) <= lifetime)
{
position.x += velocity.x / 250;
position.y += velocity.y / 250;
position.z += velocity.z / 250;
velocity.x *= 0.975f;
velocity.y *= 0.975f;
velocity.z *= 0.975f;
} else {
alive = false;
}
}
}
public void drawParticle()
{
/*
GL11.glBegin(GL11.GL_TRIANGLE_STRIP);
GL11.glColor3f(myColor.getRed(), myColor.getGreen(), myColor.getBlue());
position.z = - 2.0f;
GL11.glVertex3f(position.x+size, position.y+size, position.z);
GL11.glVertex3f(position.x-size, position.y+size, position.z);
GL11.glVertex3f(position.x+size, position.y-size, position.z);
GL11.glVertex3f(position.x-size, position.y-size, position.z);
GL11.glEnd();
*/
if (alive)
{
GL11.glBegin(GL11.GL_POINT);
GL11.glColor3f(myColor.getRed(), myColor.getGreen(), myColor.getBlue());
GL11.glVertex3f(position.x, position.y, position.z);
System.out.println("drawParticle() - " + position);
GL11.glEnd();
}
}
public boolean isParticleAlive()
{
return alive;
}
}
Here’s how i use the code:
ParticleEmitter theEmitter = new ParticleEmitter(new Vector3f(0,0,0), new Vector3f(1,1,0), new org.lwjgl.util.Color(org.lwjgl.util.Color.WHITE), 4000, 100, 1.0f, true, true);
while(!IceEngine.isEngineCloseRequested()) // Same as Display.isCloseRequested();
{
org.lwjgl.opengl.GL11.glClear(org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT | org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT);
org.lwjgl.opengl.GL11.glLoadIdentity();
theEmitter.Update();
theEmitter.drawParticles();
IceEngine.update(); // Same as Display.update();
}
I hope someone can help me