performance with animated sprites
in
Programming Questions
•
2 years ago
hello everybody.
Do you have any idea how to optimize animated sprites in this sketch.
On a i5 MacBookPro i get 15-20 fps with 100 Sprites ( if i remove smooth();).
how to get 200 Sprites at the same framerate or more (using vector input ? pgraphics ? something completly different ?)
you can grab source and data
here
thanks for your help.
- //Original Code from :
- //http://www.openprocessing.org/visuals/?visualID=38104
- //////////////
- //////////////Variables//////////////
- //Sprites Maker Variables
- int framenumber = 45;//Total frame number in the source animation
- int SZ = 150; //Original sprite size (Have to be Square)
- PImage spriteSheet;// the animation on 1 picture
- PImage sprite[];//the animation in "framenumber" pictures
- int sheetresolutionX=9;//
- int sheetresolutionY=5;//
- //System Variables
- float speed=1; // sprites speed
- float speedvar=speed*.2; //sprites speed variation
- int amount=50;// sprite number
- float repuls = 5; // repulsion effect
- character ch[];
- PFont font;
- //////////////
- //////////////Setup//////////////
- //Main
- void setup() {
- size(640,320,JAVA2D);
- smooth();//Make Fps drop.
- frameRate(24);
- imageMode(CENTER);// center pivot
- font = createFont("Arial Bold",50);//import font
- //setup character objects
- ch = new character[amount];
- for(int i = 0; i < amount; i++)
- ch[i] = new character();
- //Import Sprite sequence
- spriteSheet = loadImage("Sheet.png"); //Sheet file name.
- //divide sheet into frames
- sprite = new PImage[45];
- int cnt = 0;
- for (int y = 0; y < sheetresolutionY; y++)
- for (int x = 0; x < sheetresolutionX; x++)
- sprite[cnt++] = spriteSheet.get(x*SZ, y*SZ, SZ, SZ);
- }
- //////////////
- //////////////Draw//////////////
- void draw() {
- background(16,103,143);
- for(int i = 0; i < amount; i++)
- ch[i].check();
- // Display Fps
- fill(200);
- text(int(frameRate)+"fps",width-40,height-10);
- //if (key == 's' || key == 'S') saveFrame("blobbysprite_##.png");//saveframe
- }
- //////////////
- //////////////CLASS//////////////
- class character {
- PVector pos, dir;
- int frame;
- float spd;
- character() {
- pos = new PVector(int(random(width)), int(random(height)));
- dir = new PVector(random(2), random(2));
- frame = int(random(framenumber));//random do an offet on each animation
- spd = random(speedvar) + speed;
- }
- void check() {
- //check other characters
- for (int i=0; i<amount; i++)
- if (ch[i] != this)
- if (pos.dist(ch[i].pos) < repuls) {
- PVector dir2 = PVector.sub(pos, ch[i].pos);
- dir2.normalize();
- dir2.mult(spd);
- dir2.div(8);
- dir.add(dir2);
- }
- //check mouse
- PVector m = new PVector(mouseX, mouseY);
- if (pos.dist(m) > repuls) {
- PVector dir2 = PVector.sub(m, pos);
- dir2.normalize();
- dir2.mult(spd);
- dir2.div(repuls);
- dir.add(dir2);
- }
- //draw character
- dir.normalize();
- dir.mult(spd*3);
- pos.add(dir);
- if (frame >= framenumber) frame = 0; //create the loop
- pushMatrix();
- translate(pos.x, pos.y);
- rotate(atan2(dir.y, dir.x) + HALF_PI);
- image(sprite[frame++], 0, 0);
- popMatrix();
- }
- }
1