hi everybody,
could you give some help with this problem ?
you can grab the sketch and source files
here
The problem :
Is this sketch, i ve have some problem to update the sprites amount with control P5.
I don't think that the problem comes not from ControlP5 but more from my code.
I have no idea how to fix this now.
thanks for you time and help.
here i've disable everything related to Kinect, you need osx and Shiffman kinect library to play with).
Problem append with lines 76-78 and 193-195 when changing "amount" variable using Control P5.
- //////////////IMPORT//////////////
- //////////////Kinect//////////////
- /*import org.openkinect.*;
- import org.openkinect.processing.*;
- KinectTracker tracker;// kinect class
- Kinect kinect;// Kinect Library object*/
- //////////////Open Gl//////////////
- import processing.opengl.PGraphicsOpenGL;
- import javax.media.opengl.GL;
- PGraphicsOpenGL pgl;
- GL gl;
- //////////////Control P5//////////////
- import controlP5.*;
- ControlP5 controlP5;
- ColorPicker cp;
- //////////////VARIABLES//////////////
- //////////////Sprites Maker Variables //////////////
- //Sprite animation 1
- int framenumber1 = 45;//Total frame number in the source animation1
- int SZ1 = 200; //Original sprite size (Have to be Square)
- PImage spriteSheet1;// the animation on 1 picture
- PImage sprite1[];//the animation in "framenumber11" pictures
- int sheetresolutionX1=9;//row in sheet 1
- int sheetresolutionY1=5;//columms in sheet 1
- //Sprite animation 2
- int framenumber2 = 45;//Total frame number in the source animation1
- int SZ2 = 200; //Original sprite size (Have to be Square)
- PImage spriteSheet2;// the animation on 1 picture
- PImage sprite2[];//the animation in "framenumber11" pictures
- int sheetresolutionX2=9;//row in sheet 2
- int sheetresolutionY2=5;//columms in sheet 2
- //Sprite animation 3
- int framenumber3 = 45;//Total frame number in the source animation1
- int SZ3 = 200; //Original sprite size (Have to be Square)
- PImage spriteSheet3;// the animation on 1 picture
- PImage sprite3[];//the animation in "framenumber11" pictures
- int sheetresolutionX3=9;//row in sheet 3
- int sheetresolutionY3=5;//columms in sheet 3
- //////////////System Variables//////////////
- int fps=24; //Frames Second
- float speed=2; // sprites speed
- float speedvar=speed*.1; //sprites speed variation
- int amount=20;// sprite number
- float repuls = 3; // repulsion effect
- float spritescale =1 ;//scale multplier
- character ch[];//create frames caontainer
- //PVector v1;//Kinect mapping to screen
- //float tgy2=0;//Kinect mapping to screen
- boolean mode = true;//Redraw background ON/OFF
- //////////////
- //////////////SETUP//////////////
- void setup() {
- //////////////Window
- size(1280,720, OPENGL);//Setup window and render mode
- //////////////OGL Graphics
- pgl = (PGraphicsOpenGL) g;
- gl = pgl.gl;
- gl.setSwapInterval(1);
- //////////////Interface
- controlP5 = new ControlP5(this);//Call P5
- controlP5.setColorLabel(0xffdddddd); //Tweak P5 font color
- //cp = controlP5.addColorPicker("picker",0,640,255,20);//make color picker
- controlP5.addSlider("fps",1,60,0,686,100,20);//make fps slider
- controlP5.addSlider("amount",1,200,600,686,100,20);//make amount slider
- controlP5.addSlider("spritescale",0.1,1.2,300,686,100,20);//make sprite scale slider
- controlP5.addToggle("mode",true,0,617,40,40);//make mode toggle
- //////////////Setup character objects
- imageMode(CENTER);// center pivot
- ch = new character[amount];//
- for (int i = 0; i < amount; i++)
- ch[i] = new character();
- //////////////Make animation 1
- //Import Sprite sequence 1
- spriteSheet1 = loadImage("Sheet_high.png"); //Sheet file name.
- //divide sheet into frames for animation 1
- sprite1 = new PImage[framenumber1];
- int cnt = 0;
- for (int y1 = 0; y1 < sheetresolutionY1; y1++)
- for (int x1 = 0; x1 < sheetresolutionX1; x1++)
- sprite1[cnt++] = spriteSheet1.get(x1*SZ1, y1*SZ1, SZ1, SZ1);
- //////////////Make animation 2
- //Import Sprite sequence 2
- spriteSheet2 = loadImage("Sheet_high.png"); //Sheet file name.
- //divide sheet into frames for animation 1
- sprite2 = new PImage[framenumber2];
- int cnt2 = 0;
- for (int y2 = 0; y2 < sheetresolutionY2; y2++)
- for (int x2 = 0; x2 < sheetresolutionX2; x2++)
- sprite2[cnt2++] = spriteSheet2.get(x2*SZ2, y2*SZ2, SZ2, SZ2);
- //////////////Make animation 3
- //Import Sprite sequence 3
- spriteSheet3 = loadImage("Sheet_high.png"); //Sheet file name.
- //divide sheet into frames for animation 1
- sprite3 = new PImage[framenumber3];
- int cnt3 = 0;
- for (int y3 = 0; y3 < sheetresolutionY3; y3++)
- for (int x3 = 0; x3 < sheetresolutionX3; x3++)
- sprite3[cnt3++] = spriteSheet3.get(x3*SZ3, y3*SZ3, SZ3, SZ3);
- //Kinect
- /*kinect = new Kinect(this);
- tracker = new KinectTracker();*/
- }
- //////////////
- //////////////DRAW//////////////
- void draw() {
- // Run the tracking analysis
- //tracker.track();
- // Show the image
- //tracker.display();
- if(mode==true) {
- background(20,40,60);}//Draw or Not the background
- 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
- //Kinect target
- // Let's draw the raw location
- /*PVector v1 = tracker.getPos();
- v1.x=map(v1.x,0,640,0,width);
- v1.y=map(v1.y,0,350,0,height);
- fill(255, 255, 139);
- noStroke();
- ellipse(v1.x,v1.y,20,20);
- Display some info
- int t = tracker.getThreshold();
- fill(0);
- text("threshold: " + t + " " + "framerate: " + (int)frameRate + " " + "UP increase threshold, DOWN decrease threshold",10,500);*/
- }
- ///Kinect set Threshold
- /*void keyPressed() {
- int t = tracker.getThreshold();
- if (key == CODED) {
- if (keyCode == UP) {
- t+=5;
- tracker.setThreshold(t);
- }
- else if (keyCode == DOWN) {
- t-=5;
- tracker.setThreshold(t);
- }
- }
- }*/
- //////////////
- //////////////CLASS//////////////
- class character {
- PVector pos, dir;//characters position
- int frame1;
- int frame2;
- int frame3;
- float spd;
- float spriteselect;
- character() {
- pos = new PVector(int(random(100)), int(random(100)));//birth spread
- dir = new PVector(random(1), random(1));//birth rotation
- frame1 = int(random(framenumber1));//random do an offet on each animation for animation 1
- frame2 = int(random(framenumber1));//random do an offet on each animation for animation 2
- frame3 = int(random(framenumber1));//random do an offet on each animation for animation 3
- spd = random(speedvar) + speed; //speed and speed variation
- spriteselect=random(3);//switch between animations
- }
- 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 or Kinect
- //With Kinect
- /*PVector tg =tracker.getPos();
- tgx2=map(tg.x,0,640,0,screen.width);
- tgy2=map(tg.y,0,350,0,screen.height);
- PVector m = new PVector(tgx2, tgy2);*/
- //With Mouse
- PVector m = new PVector(mouseX, mouseY);//create mouse vector
- 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();
- frameRate(fps);
- dir.mult(spd*3);
- pos.add(dir);
- if (frame1 >= framenumber1) frame1 = 0; //create the loop for animation 1
- if (frame2 >= framenumber2) frame2 = 0; //create the loop for amimation 2
- if (frame3 >= framenumber3) frame3 = 0; //create the loop for animation 3
- pushMatrix();
- translate(pos.x, pos.y);
- rotate(atan2(dir.y, dir.x) + HALF_PI);
- scale(spritescale);
- if (spriteselect<=1){image(sprite1[frame1++], 0, 0);};
- if ((spriteselect>1) && (spriteselect<=2)){image(sprite2[frame2++], 0, 0);};
- if (spriteselect>2){image(sprite3[frame3++], 0, 0);};
- popMatrix();
- }
- }
- //////////////Save Frame
- void keyReleased() {
- if (key == 's' || key == 'S') saveFrame(timestamp()+"_##.png");
- }
- ////////////// timestamp for Save Frame
- String timestamp() {
- Calendar now = Calendar.getInstance();
- return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
- }
- //////////////Kinect stop.
- /*void stop() {
- tracker.quit();
- super.stop();*/
- //}*/
1