slow performance -pc and android
in
Programming Questions
•
1 year ago
i've got this simple code working as i want, but the performance sucks getting only 30fps on pc and even less on android.
i presume the mouse position checks against the box_collection every frame slows this down.
i could spawn a "square" at he mouse position. but i want to bring a sqaure to life, rather than spawning.
- ArrayList Box_Collection;
- Square[] Square_Collection;
- int rows = 9;
- int cols = 9;
- float spacing = 1.0;
- void setup() {
- size(680, 680,P2D);
- background(0);
- noStroke();
- //smooth();
- int boxsizex = int((width/rows));
- int boxsizey = int(height/cols);
- //square background attempt
- int total = (rows)*(cols);
- Square_Collection = new Square [total];
- for (int j = 0; j < rows; j++)
- {
- for (int i =0; i < cols; i++)
- {
- PVector squarepos = new PVector((j*(boxsizex)), (i*(boxsizey)), 0);
- Square_Collection[(j*(cols))+i] = new Square(squarepos);
- }
- }
- }
- void draw() {
- background(0);
- println("frameRate = " + frameRate);
- int boxsizex = int((width/rows));
- int boxsizey = int(height/cols);
- for ( int i = 0; i < Square_Collection.length; i++ )
- {
- Square_Collection[i].display();
- }
- PVector mouse = new PVector(mouseX,mouseY,0);
- for (int j = 0; j < rows; j++)
- {
- for (int k =0; k < cols; k++)
- {
- PVector squarepos = new PVector((j*boxsizex), (k*boxsizey), 0);
- PVector offset = new PVector ((boxsizex/2), (boxsizey/2), 0);
- PVector totalPostion = PVector.add(squarepos,offset);
- int distance = int(totalPostion.dist(mouse));
- if (distance > 0 && distance < (boxsizex/2)) {
- Square_Collection[(j*(cols))+k].live();
- }
- }
- }
- }
- class Square {
- int sizex;
- int sizey;
- int life = 00;
- PVector loc = new PVector (0, 0, 0);
- int boxsizex = int((width/rows));
- int boxsizey = int(height/cols);
- int linelengh;
- ///////////////////////////////////////////////////////////////
- Square (PVector _loc) {
- loc = _loc;
- }
- ///////////////////////////////////////////////////////////////
- void run() {
- display();
- }
- ///////////////////////////////////////////////////////////////
- void live() {
- life = 80;
- linelengh = 60;
- }
- ///////////////////////////////////////////////////////////////
- void display() {
- pushMatrix();
- translate( loc.x,loc.y);
- fill(150*(life*0.02), (life*0.02),(life*0.5));
- strokeWeight(0.15);
- stroke((life*10), 25, 10, (life*10));
- rect(0, 0, boxsizex, boxsizey,6);
- strokeWeight(3);
- stroke((255), 25, 10, (linelengh*10));
- line(-linelengh, 0,(boxsizex+linelengh), 0);
- line(-linelengh,(boxsizey- 0),(boxsizex+linelengh), (boxsizey- 0));
- line(0, -linelengh,0, (boxsizey+linelengh));
- line((boxsizey-0), -linelengh,(boxsizey- 0), (boxsizey+linelengh));
- popMatrix();
- life -= 3;
- linelengh --;
- }
- }
1