Is there a way to make this run any faster?
in
Programming Questions
•
2 years ago
Hi all. So I've been working in processing for a few weeks now, and decided I'd make an evolution simulator, that spawns creatures with random properties. Eventually, I'll add reproduction, but as of now the creatures just sort of float around the screen, and follow the mouse somewhat. It interacts with sound from the line in, but I commented those parts out so that it can be viewed on canvas, it shouldn't really change the question, just thought I would point it out. As of now, the framerate is far less than ideal for me, and it slowly lowers as more creatures are spawned. Am I making some fundamental mistake, or am I just running too many functions per frame?
I've isolated the section that's causing the slowdown. Basically, this runs every frame and draws each creature in the creature array depending on its properties. I can't really think of a more streamlined way to do this. Could the push and popmatrix's be slowing it down particularly?
- if(ellipses[n] == i) {
- if(i == 0) {
- }
- else if (i == 1) {
- pushMatrix();
- translate(creatureX[n], creatureY[n]);
- ellipse(creatureSize[n], creatureSize[n], creatureSize[n], creatureSize[n]);
- rotate(radians(180));
- ellipse(creatureSize[n], creatureSize[n], creatureSize[n], creatureSize[n]);
- popMatrix();
- }
- else if(i == 2) {
- pushMatrix();
- translate(creatureX[n], creatureY[n]);
- ellipse(creatureSize[n], creatureSize[n], creatureSize[n], creatureSize[n]);
- rotate(radians(180));
- ellipse(creatureSize[n], creatureSize[n], creatureSize[n], creatureSize[n]);
- rotate(radians(270));
- ellipse(creatureSize[n], creatureSize[n], creatureSize[n], creatureSize[n]);
- popMatrix();
- }
- else if(i == 3) {
- pushMatrix();
- translate(creatureX[n], creatureY[n]);
- ellipse(creatureSize[n], creatureSize[n], creatureSize[n], creatureSize[n]);
- rotate(radians(90));
- ellipse(creatureSize[n], creatureSize[n], creatureSize[n], creatureSize[n]);
- rotate(radians(180));
- ellipse(creatureSize[n], creatureSize[n], creatureSize[n], creatureSize[n]);
- rotate(radians(270));
- ellipse(creatureSize[n], creatureSize[n], creatureSize[n], creatureSize[n]);
- popMatrix();
- }
- }
- if(hair[n] == i) {
- if(i == 0) {
- }
- else if (i == 1) {
- pushMatrix();
- translate(creatureX[n], creatureY[n]);
- for(int j = 0; j <= 360; j+=70) {
- rotate(j);
- stroke(colorAttribute[n], random(255));
- line(0,0, creatureSize[n] + random(10), creatureSize[n] + random(10));
- }
- popMatrix();
- }
- else if(i == 2) {
- pushMatrix();
- translate(creatureX[n], creatureY[n]);
- for(int j = 0; j <= 360; j+=30) {
- rotate(j);
- stroke(colorAttribute[n], random(255));
- line(0,0, creatureSize[n] + random(10), creatureSize[n] + random(10));
- }
- popMatrix();
- }
- else if(i == 3) {
- pushMatrix();
- translate(creatureX[n], creatureY[n]);
- for(int j = 0; j <= 360; j+=1) {
- rotate(j);
- stroke(colorAttribute[n], random(255));
- line(0,0, creatureSize[n] + random(10), creatureSize[n] + random(10));
- }
- popMatrix();
- }
- }
- }
1