Getting a static L system to grow in real time
in
Programming Questions
•
1 year ago
hi there would any one know how i can make this l system grow in real time?
import toxi.geom.*;
import peasy.*;
PeasyCam cam;
branch arm;
ArrayList <branch> allArms;
void setup (){
size (600,600, P3D);
smooth();
allArms = new ArrayList <branch>();
cam = new PeasyCam(this, 100);
Vec3D v = new Vec3D (0,0,0);
Vec3D inivel = new Vec3D (100,0,0);
arm = new branch(v, inivel, 6,"A");
allArms. add(arm);
}
void draw() {
background(255);
fill(227,53,53);
stroke(80);
sphere(100);
for (branch b: allArms){
b.run();
}
}
class branch {
Vec3D loc;
Vec3D oriLoc;
Vec3D vel;
int generations;
String type;
branch(Vec3D _loc, Vec3D _vel, int _generations, String _type){
loc =_loc;
oriLoc = loc.copy();
vel = _vel;
generations = _generations;
type= _type;
updateDir();
updateLoc();
spawn();
}
void run (){
Display();
}
void spawn(){
if (generations > 0) {
if (type == "A") {
Vec3D v = loc. copy();
Vec3D inivel = vel. copy();
branch newArm = new branch(v, inivel,generations -1,"A");
allArms. add(newArm);
Vec3D v2 = loc. copy();
Vec3D inivel2 = vel. copy();
branch newArm2 = new branch(v2, inivel2,generations -1,"B");
allArms. add(newArm2);
Vec3D v3 = loc. copy();
Vec3D inivel3 = vel. copy();
branch newArm3 = new branch(v3, inivel3,generations -1,"C");
allArms. add(newArm3);
Vec3D v4 = loc. copy();
Vec3D inivel4 = vel. copy();
branch newArm4 = new branch(v4, inivel4,generations -1,"D");
allArms. add(newArm4);
}
if (type == "B") {
Vec3D v3 = loc. copy();
Vec3D inivel3 = vel. copy();
branch newArm3 = new branch(v3, inivel3,generations -1,"B");
allArms. add(newArm3);
Vec3D v4 = loc. copy();
Vec3D inivel4 = vel. copy();
branch newArm4 = new branch(v4, inivel4,generations -1,"C");
allArms. add(newArm4);
Vec3D v5 = loc. copy();
Vec3D inivel5 = vel. copy();
branch newArm5 = new branch(v5, inivel5,generations -1,"D");
allArms. add(newArm5);
Vec3D v6 = loc. copy();
Vec3D inivel6 = vel. copy();
branch newArm6 = new branch(v6, inivel6,generations -1,"A");
allArms. add(newArm6);
}
if (type == "C") {
Vec3D v3 = loc. copy();
Vec3D inivel3 = vel. copy();
branch newArm3 = new branch(v3, inivel3,generations -1,"C");
allArms. add(newArm3);
Vec3D v4 = loc. copy();
Vec3D inivel4 = vel. copy();
branch newArm4 = new branch(v4, inivel4,generations -1,"D");
allArms. add(newArm4);
Vec3D v5 = loc. copy();
Vec3D inivel5 = vel. copy();
branch newArm5 = new branch(v5, inivel5,generations -1,"A");
allArms. add(newArm5);
Vec3D v6 = loc. copy();
Vec3D inivel6 = vel. copy();
branch newArm6 = new branch(v6, inivel6,generations -1,"B");
allArms. add(newArm6);
}
if (type == "D") {
Vec3D v3 = loc. copy();
Vec3D inivel3 = vel. copy();
branch newArm3 = new branch(v3, inivel3,generations -1,"D");
allArms. add(newArm3);
Vec3D v4 = loc. copy();
Vec3D inivel4 = vel. copy();
branch newArm4 = new branch(v4, inivel4,generations -1,"A");
allArms. add(newArm4);
Vec3D v5 = loc. copy();
Vec3D inivel5 = vel. copy();
branch newArm5 = new branch(v5, inivel5,generations -1,"B");
allArms. add(newArm5);
Vec3D v6 = loc. copy();
Vec3D inivel6 = vel. copy();
branch newArm6 = new branch(v6, inivel6,generations -1,"C");
allArms. add(newArm6);
}
}
}
void updateLoc(){
loc. addSelf(vel);
}
void updateDir(){
if (type =="A"){
float angle1 = radians(30);
float angle2 = radians(30);
float angle3 = radians(30);
vel. rotateX (angle1);
vel. rotateY (angle2);
vel. rotateZ (angle3);
}
if (type =="B"){
float angle1 = radians(-30);
float angle2 = radians(-30);
float angle3 = radians(-30);
vel. rotateX (angle1);
vel. rotateY (angle2);
vel. rotateZ (angle3);
}
if (type =="C"){
float angle1 = radians(0);
float angle2 = radians(0);
float angle3 = radians(0);
vel. rotateX (angle1);
vel. rotateY (angle2);
vel. rotateZ (angle3);
}
if (type =="D"){
float angle1 = radians(0);
float angle2 = radians(0);
float angle3 = radians(0);
vel. rotateX (angle1);
vel. rotateY (angle2);
vel. rotateZ (angle3);
}
}
void Display(){
stroke(0,0,0);
strokeWeight(0);
point (loc.x, loc.y, loc.z);
stroke(0);
strokeWeight(1);
line (loc.x, loc.y, loc.z, oriLoc.x, oriLoc.y, oriLoc.z);
line (loc.x, loc.y, loc.z, oriLoc.x, oriLoc.y, oriLoc.z);
}
}
1