Lines are freaking out
in
Contributed Library Questions
•
2 years ago
Hello,
I am going for a kind of flying-through-space effect with random-colored lines, and I've basically got it, but if I set the "timer" variable too high, I get like an overload of lines and a bunch of random lines intersecting the origin are randomly drawn. The structure of the code is this; there is a class called "littlelight" which are the individual lines erupting from the origin. That class is called every frame--and every frame a new "littlelight" is generated by "ps" which is an instance of the ParticleSystem class. The ParticleSystem class removes "littlelight"'s from the ArrayList when their "timer" has run out. The "timer" is set to run to zero about when that "littlelight" is entirely off-screen; so, once a "littlelight" leaves the screen area, it dies. However, I really have no idea where this overload behavior is coming from.
This uses toxic libs btw. The code is set now to overload as I have described above. If you go down to where the "timer" variable is set, you will see I have commented code that will work.
This code is based on this example from the Processing website. http://processing.org/learning/topics/simpleparticlesystem.html
Thanks
import toxi.geom.*;
ParticleSystem ps;
void setup ()
{
size(1280, 720, P3D);
frameRate(30);
translate(width/2, height/2);
ps = new ParticleSystem(1, new Vec3D(0, 0, 0));
//smooth();
}
void draw() {
translate(width/2, height/2);
background(0);
ps.run();
ps.addlittlelight(0, 0);
noStroke();
translate(-width/2, -height/2);
}
class ParticleSystem {
//GLOBAL VARIABLES
ArrayList particles; // An arraylist for all the particles
Vec3D origin; // An origin point for where particles are born
//CONSTRUCTOR
ParticleSystem(int num, Vec3D v) {
particles = new ArrayList(); // Initialize the arraylist
origin = v; // Store the origin point
// Add "num" amount of particles to the arraylist
//for (int i = 0; i < num; i++) {
//particles.add(new littlelight(origin));
//}
}
//FUNCTIONS
void run() {
for (int i = particles.size() - 1; i>=0; i--) {
littlelight p = (littlelight) particles.get(i);
//p.run();
if (p.dead()) {
particles.remove(i);
}
else p.run();
}
}
/*
void addlittlelight() {
particles.add(new littlelight(origin));
}
*/
void addlittlelight(float x, float y) {
particles.add(new littlelight(new Vec3D(x, y, 0)));
}
/*
void addlittlelight(littlelight p) {
particles.add(p);
}
*/
boolean dead() {
if (particles.isEmpty()) {
return true;
}
else
{
return false;
}
}
}
class littlelight {
//GLOBAL VARIABLES
Vec3D loc;
Vec3D loc2;
Vec3D vel = new Vec3D(random(-1, 1), random(-1, 1), random(0.9, 0.99));
Vec3D acc;
float r;
float timer;
float colorr = random(0, 255);
float colorg = random(0, 255);
float colorb = random(0, 255);
float len = 100;
//CONSTRUCTOR
littlelight(Vec3D l) {
//vel = new Vec3D(random(-1, 1), random(-1, 1), random(0.9, 0.99));
acc = vel.copy();
acc.normalizeTo(1);
loc = l;
loc2 = vel.copy();
loc2.normalizeTo(len);
//loc2.normalizeTo(len);
timer = 100.0;
//try this
//timer = 30.0;
}
//FUNCTIONS
void run() {
update();
render();
}
void update() {
vel.addSelf(acc);
loc.addSelf(vel);
loc2.addSelf(vel);
timer -= 1.0;
}
void render() {
//noStroke();
if (timer > -1.0) {
strokeWeight(1);
stroke(colorr, colorg, colorb);
//Line3D boner = new Line3D(loc, loc2);
line(loc.x, loc.y, loc.z, loc2.x, loc2.y, loc2.z);
//strokeWeight(10);
//stroke(255, 0, 0);
//point(loc.x, loc.y, loc.z);
//stroke(0, 0, 255);
//point(loc2.x, loc2.y, loc2.z);
//ellipse(loc.x, loc.y, r * (1000 - timer), r * (1000 - timer));
//x = Line3D(loc, loc2);
}
}
boolean dead() {
if (timer <= 0.0) {
return true;
}
else {
return false;
}
}
}
I am going for a kind of flying-through-space effect with random-colored lines, and I've basically got it, but if I set the "timer" variable too high, I get like an overload of lines and a bunch of random lines intersecting the origin are randomly drawn. The structure of the code is this; there is a class called "littlelight" which are the individual lines erupting from the origin. That class is called every frame--and every frame a new "littlelight" is generated by "ps" which is an instance of the ParticleSystem class. The ParticleSystem class removes "littlelight"'s from the ArrayList when their "timer" has run out. The "timer" is set to run to zero about when that "littlelight" is entirely off-screen; so, once a "littlelight" leaves the screen area, it dies. However, I really have no idea where this overload behavior is coming from.
This uses toxic libs btw. The code is set now to overload as I have described above. If you go down to where the "timer" variable is set, you will see I have commented code that will work.
This code is based on this example from the Processing website. http://processing.org/learning/topics/simpleparticlesystem.html
Thanks
import toxi.geom.*;
ParticleSystem ps;
void setup ()
{
size(1280, 720, P3D);
frameRate(30);
translate(width/2, height/2);
ps = new ParticleSystem(1, new Vec3D(0, 0, 0));
//smooth();
}
void draw() {
translate(width/2, height/2);
background(0);
ps.run();
ps.addlittlelight(0, 0);
noStroke();
translate(-width/2, -height/2);
}
class ParticleSystem {
//GLOBAL VARIABLES
ArrayList particles; // An arraylist for all the particles
Vec3D origin; // An origin point for where particles are born
//CONSTRUCTOR
ParticleSystem(int num, Vec3D v) {
particles = new ArrayList(); // Initialize the arraylist
origin = v; // Store the origin point
// Add "num" amount of particles to the arraylist
//for (int i = 0; i < num; i++) {
//particles.add(new littlelight(origin));
//}
}
//FUNCTIONS
void run() {
for (int i = particles.size() - 1; i>=0; i--) {
littlelight p = (littlelight) particles.get(i);
//p.run();
if (p.dead()) {
particles.remove(i);
}
else p.run();
}
}
/*
void addlittlelight() {
particles.add(new littlelight(origin));
}
*/
void addlittlelight(float x, float y) {
particles.add(new littlelight(new Vec3D(x, y, 0)));
}
/*
void addlittlelight(littlelight p) {
particles.add(p);
}
*/
boolean dead() {
if (particles.isEmpty()) {
return true;
}
else
{
return false;
}
}
}
class littlelight {
//GLOBAL VARIABLES
Vec3D loc;
Vec3D loc2;
Vec3D vel = new Vec3D(random(-1, 1), random(-1, 1), random(0.9, 0.99));
Vec3D acc;
float r;
float timer;
float colorr = random(0, 255);
float colorg = random(0, 255);
float colorb = random(0, 255);
float len = 100;
//CONSTRUCTOR
littlelight(Vec3D l) {
//vel = new Vec3D(random(-1, 1), random(-1, 1), random(0.9, 0.99));
acc = vel.copy();
acc.normalizeTo(1);
loc = l;
loc2 = vel.copy();
loc2.normalizeTo(len);
//loc2.normalizeTo(len);
timer = 100.0;
//try this
//timer = 30.0;
}
//FUNCTIONS
void run() {
update();
render();
}
void update() {
vel.addSelf(acc);
loc.addSelf(vel);
loc2.addSelf(vel);
timer -= 1.0;
}
void render() {
//noStroke();
if (timer > -1.0) {
strokeWeight(1);
stroke(colorr, colorg, colorb);
//Line3D boner = new Line3D(loc, loc2);
line(loc.x, loc.y, loc.z, loc2.x, loc2.y, loc2.z);
//strokeWeight(10);
//stroke(255, 0, 0);
//point(loc.x, loc.y, loc.z);
//stroke(0, 0, 255);
//point(loc2.x, loc2.y, loc2.z);
//ellipse(loc.x, loc.y, r * (1000 - timer), r * (1000 - timer));
//x = Line3D(loc, loc2);
}
}
boolean dead() {
if (timer <= 0.0) {
return true;
}
else {
return false;
}
}
}
1