Pixel Detection Code

Cracking game idea this week. A maze.  Draw up a complex maze – then work your way through it.  Winner is best time.

Two ways to implement it.  First is to say create a bunch of objects.  Then use collision detection on those objects. Thats a lot of work.  Second way it to draw the matrix – then use pixel collision detection to determine if the player can move – a bit easier.  Here is an example app with a simple matrix.

 

maze

 

PImage maze;
boolean collision;
PrintWriter output;

int px,py,pw,ph;

void setup()

{

    size(200,200);
    
    output = createWriter("positions.txt"); 
    
    maze = loadImage("maze.png");
    image(maze, 0, 0); // Displays the image from point (0,0) 
    maze.loadPixels();
    
    for (int y = 0; y < maze.height-1; y++) { // Skip top and bottom edges
    for (int x = 0; x < maze.width-1; x++) { // Skip left and right edges
          // Calculate the adjacent pixel for this kernel point
          
          
          int pos =  y *maze.width + x;
          color c = maze.pixels[pos];
          output.println("Pixel: " + x + " y: " +  c);
          int alpha = (c >> 24) & 0xFF;
          int red   = (c >> 16) & 0xFF;
          int green = (c >> 8)  & 0xFF;
          int blue  =  c        & 0xFF;
          output.println("Color Map: " + alpha + " " + red + " " + green + " " + blue);
                   
          
        }
  }
  
  output.flush(); // Writes the remaining data to the file
  output.close(); // Finishes the file
  
  
}



void draw()
{

 background(255);
 fill(255,0,0);
 
 
  image(maze, 0, 0); // Displays the image from point (0,0) 
  maze.loadPixels();
  // Create an opaque image of the same size as the original
 //PImage edgeImg = createImage(maze.width, maze.height, RGB);
  // Loop through every pixel in the image.
  for (int y = 0; y < maze.height-1; y++) { // Skip top and bottom edges
    for (int x = 0; x < maze.width-1; x++) { // Skip left and right edges
          // Calculate the adjacent pixel for this kernel point
          
           int pos =  y *maze.width + x;
          color c = maze.pixels[pos];
          
          int alpha = (c >> 24) & 0xFF;
          int red   = (c >> 16) & 0xFF;
          int green = (c >> 8)  & 0xFF;
          int blue  =  c        & 0xFF;
          
          if ( alpha != 0 ) // is this a non transparent pixel ?
          {
              collision = insideBox(x, y, mouseX-4, mouseY-4,8,8);
              
              if ( collision ) 
              {
                fill(0,255,0);              
                
              }
            
          }
          
          
        }
  }
        
   rect(mouseX-4, mouseY-4, 8,8);
  
}


boolean insideBox(int x, int y, int p1x, int p1y, int w, int h)
// This code checks the cursor is inside the window... 
{
  // This code will return a false if the cursor is not in the button
  if (x > p1x  && x < p1x + w && y > p1y  &&  y  < p1y + h  ) {
       // the cursor is in the box
       return true;
  }
  else {
       // the cursor is not in the box
       return false;
  }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s