Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Re : Adding background image to existing code

$
0
0
I tried what you suggested but i got the error " it looks like you'r e mixing active with static modes" and the "background(b);" gets highlighted at line 51 .

i did resize the image to 800x600


  1. import processing.serial.*;
  2. import processing.opengl.*;

  3. Serial serial;
  4. int serialPort = 1;   // << Set this to be the serial port of your Arduino - ie if you have 3 ports : COM1, COM2, COM3 
  5.                       // and your Arduino is on COM2 you should set this to '1' - since the array is 0 based
  6.               
  7. int sen = 3; // sensors
  8. int div = 3; // board sub divisions

  9. Normalize n[] = new Normalize[sen];
  10. MomentumAverage cama[] = new MomentumAverage[sen];
  11. MomentumAverage axyz[] = new MomentumAverage[sen];
  12. float[] nxyz = new float[sen];
  13. int[] ixyz = new int[sen];

  14. float w = 256; // board size
  15. boolean[] flip = {
  16.   false, true, false};

  17. int player = 0;
  18. boolean moves[][][][];

  19. PFont font;
  20. PImage b;
  21. void setup() {
  22.   size(800, 600, P3D);
  23.   frameRate(25);
  24.   b = loadImage("data/1.jpg");
  25.   font = loadFont("TrebuchetMS-Italic-20.vlw");
  26.   textFont(font);
  27.   textMode(SCREEN);
  28.   
  29.   println(Serial.list());
  30.   serial = new Serial(this, Serial.list()[serialPort], 115200);
  31.   
  32.   for(int i = 0; i < sen; i++) {
  33.     n[i] = new Normalize();
  34.     cama[i] = new MomentumAverage(.01);
  35.     axyz[i] = new MomentumAverage(.15);
  36.   }
  37.   
  38.   reset();
  39. }

  40. void draw() {
  41.   updateSerial();
  42.   drawBoard();
  43. }
  44. background(b);
  45. void updateSerial() {
  46.   String cur = serial.readStringUntil('\n');
  47.   if(cur != null) {
  48.     String[] parts = split(cur, " ");
  49.     if(parts.length == sen  ) {
  50.       float[] xyz = new float[sen];
  51.       for(int i = 0; i < sen; i++)
  52.         xyz[i] = float(parts[i]);
  53.   
  54.       if(mousePressed && mouseButton == LEFT)
  55.         for(int i = 0; i < sen; i++)
  56.           n[i].note(xyz[i]);
  57.   
  58.       nxyz = new float[sen];
  59.       for(int i = 0; i < sen; i++) {
  60.         float raw = n[i].choose(xyz[i]);
  61.         nxyz[i] = flip[i] ? 1 - raw : raw;
  62.         cama[i].note(nxyz[i]);
  63.         axyz[i].note(nxyz[i]);
  64.         ixyz[i] = getPosition(axyz[i].avg);
  65.       }
  66.     }
  67.   }
  68. }
  69. }
  70. float cutoff = .2;
  71. int getPosition(float x) {
  72.   if(div == 3) {
  73.     if(x < cutoff)
  74.       return 0;
  75.     if(x < 1 - cutoff)
  76.       return 1;
  77.     else
  78.       return 2;
  79.   } 
  80.   else {
  81.     return x == 1 ? div - 1 : (int) x * div;
  82.   }
  83. }

  84. void drawBoard() {
  85.   background(255);

  86.   float h = w / 2;
  87.   camera(
  88.     h + (cama[0].avg - cama[2].avg) * h,
  89.     h + (cama[1].avg - 1) * height / 2,
  90.     w * 2,
  91.     h, h, h,
  92.     0, 1, 0);

  93.   pushMatrix();
  94.   noStroke();
  95.   fill(0, 40);
  96.   translate(w/2, w/2, w/2);
  97.   rotateY(-HALF_PI/2);
  98.   box(w);
  99.   popMatrix();

  100.   float sw = w / div;
  101.   translate(h, sw / 2, 0);
  102.   rotateY(-HALF_PI/2);

  103.   pushMatrix();
  104.   float sd = sw * (div - 1);
  105.   translate(
  106.     axyz[0].avg * sd,
  107.     axyz[1].avg * sd,
  108.     axyz[2].avg * sd);
  109.   fill(255, 160, 0);
  110.   noStroke();
  111.   sphere(18);
  112.   popMatrix();

  113.   for(int z = 0; z < div; z++) {
  114.     for(int y = 0; y < div; y++) {
  115.       for(int x = 0; x < div; x++) {
  116.         pushMatrix();
  117.         translate(x * sw, y * sw, z * sw);

  118.         noStroke();
  119.         if(moves[0][x][y][z])
  120.           fill(255, 0, 0, 200);
  121.         else if(moves[1][x][y][z])
  122.           fill(0, 0, 255, 200);
  123.         else if(
  124.         x == ixyz[0] &&
  125.           y == ixyz[1] &&
  126.           z == ixyz[2])
  127.           if(player == 0)
  128.             fill(255, 0, 0, 200);
  129.           else
  130.             fill(0, 0, 255, 200);
  131.         else
  132.           fill(0, 100);
  133.         box(sw / 3);

  134.         popMatrix();
  135.       }
  136.     }
  137.   }
  138.   
  139.   fill(0);
  140.   if(mousePressed && mouseButton == LEFT)
  141.     msg("defining boundaries");
  142. }

  143. void keyPressed() {
  144.   if(key == TAB) {
  145.     moves[player][ixyz[0]][ixyz[1]][ixyz[2]] = true;
  146.     player = player == 0 ? 1 : 0;
  147.   }
  148. }

  149. void mousePressed() {
  150.   if(mouseButton == RIGHT)
  151.     reset();
  152. }

  153. void reset() {
  154.   moves = new boolean[2][div][div][div];
  155.   for(int i = 0; i < sen; i++) {
  156.     n[i].reset();
  157.     cama[i].reset();
  158.     axyz[i].reset();
  159.   }
  160. }

  161. void msg(String msg) {
  162.   text(msg, 10, height - 10);
  163. }
could you please help me out here ? 
I am new to these, we are using it for our semester project 

Viewing all articles
Browse latest Browse all 1768

Trending Articles