Original Post
Just wondering what any experienced people think about this implementation of a crude point based particle system and how I could improve it (I know my math is really terrible).
A single engine class can do about 9000 particles with 1300 FPS (vsync off, game loop fixed at 100 frames per second) on my machine (but i have really high end hardware).
Anyways, is there a better random function I can use that won't require jars or libs or DLL's that's cross platform??
here's the code:
A single engine class can do about 9000 particles with 1300 FPS (vsync off, game loop fixed at 100 frames per second) on my machine (but i have really high end hardware).
Anyways, is there a better random function I can use that won't require jars or libs or DLL's that's cross platform??
here's the code:
package joeysturgis.simple2d.tileengine;
import static org.lwjgl.opengl.GL11.*;
public class ParticleEngine {
int particlecount=0;
particle[] particles;
private double x = 0;
private double y = 0;
private double r = 0;
private double g = 0;
private double b = 0;
private double a = 0;
private double applygravity = 0;
private boolean emitter = false;
private boolean randomcolor = false;
private double speed = 0;
private int tornado = 0;
private boolean pause = false;
private class particle {
Timer timer = null;
private ParticleEngine daddy = null;
private double x = 0.0D;
private double y = 0.0D;
private double speed = 1.001;
private double r = 1.0D;
private double g = 0;
private double b = 0;
private double a = 1;
private double lifespan = 1000;
private boolean dead = false;
public particle(ParticleEngine dad){
daddy = dad;
timer = new Timer();
timer.startTimer();
if(daddy.randomcolor){
r = (Math.random()*100) % 3;
g = (Math.random()*100) % 3;
b = (Math.random()*100) % 3;
a = (Math.random()*100);
}
else{
r = daddy.r + (Math.random()/10);
g = daddy.g + (Math.random()/10);
b = daddy.b + (Math.random()/10);
a = daddy.a + (Math.random()/10);
}
}
private void update() {
if(pause) {
timer.pauseTimer();
return;
}
timer.unpauseTimer();
if(Math.random()>.5){
x += 1*Math.random();
}
else{
x -= 1*Math.random();
}
if(Math.random()>.5){
y += 1*Math.random();
}
else{
y -= 1*Math.random();
}
if(timer.getTime() >= lifespan / (Math.random()*Math.random())){
kill();
return;
}
double tornadocalc = Math.random();
if(tornadocalc>.5) { x += daddy.tornado/100; }
else { x -= daddy.tornado/100; }
x *= (speed+daddy.speed);
y *= (speed+daddy.speed);
if(daddy.applygravity!=0){
y += 0.01*daddy.applygravity;
}
}
private void draw() {
glColor4d(r,g,b,a);
//glPointSize((float)(Math.random() *10));
glVertex2d(daddy.x+x, daddy.y+y);
}
private void move(double x, double y){
this.x=x;
this.y=y;
}
private void kill(){
this.timer = null;
dead=true;
}
}
public ParticleEngine(int size, double r, double g, double b, double a, boolean emit) {
if(size<=0) {
return;
}
particlecount = size;
this.r = r;
this.g = g;
this.b = b;
this.a = a;
emitter = emit;
generate();
}
public ParticleEngine(int size, double r, double g, double b, double a, boolean emit, boolean randomcolor) {
if(size<=0) {
return;
}
particlecount = size;
this.r = r;
this.g = g;
this.b = b;
this.a = a;
emitter = emit;
this.randomcolor=randomcolor;
generate();
}
private void generate() {
particles = new particle[particlecount];
for(int i = 0; i < particlecount; i++) {
particles = new particle(this);
}
}
public void update() {
for(int i = 0; i < particlecount; i++) {
if(particles != null){
if(particles.dead){
particles.kill();
particles = null;
}
else{
particles.update();
}
}
else{
particles = null;
if(emitter)
{
particles = new particle(this);
}
}
}
}
public void draw() {
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glPointSize(10.0f);
glBlendFunc(GL_SRC_ALPHA, GL_DST_COLOR);
glBegin(GL_POINTS);
for(int i = 0; i < particlecount; i++) {
if(particles != null){
particles.draw();
}
}
glEnd();
glDisable(GL_BLEND);
}
public void move(double x, double y){
this.x = x;
this.y = y;
}
public void reset(){
for(int i = 0; i < particlecount; i++){
if(particles != null) particles.move(this.x,this.y);
}
}
public void killme(){
for(int i = 0; i < particlecount; i++){
if(particles!=null) particles.kill();
particles = null;
}
}
public void gravity(double i){
applygravity +=i;
}
public void setspeed(double speed){
this.speed = speed;
}
public void tornado(int i){
this.tornado+=i;
if(tornado<0) tornado=0;
}
public void pause(){
if(pause) {
pause = false;
}
else {
pause = true;
}
}
}