We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
JMyron as interface (Read 207 times)
JMyron as interface
Mar 1st, 2009, 7:18pm
 
Hi there

I am trying to get the globBoxes in JMyron to collide with other regularly-drawn rectangles in Processing in order to move rectangles.

At the moment, I simply have a moving rectangle which I am trying to perform a collision test, but it just passes through the glob-detection rectangles. When it hits another regularly-drawn rectangle, however, it repels back.

Any tips? Here is my code:

import JMyron.*;

JMyron m;//a camera object

// Global variables for the moving rectangle
float rectm_x;
float rectm_y;
float rectm_dir = 1;
float rectm_width = 10;
float rectm_height = 10;
float dy = 0;  // Direction

// Global variables for the hitting rectangle
int rect2_width = 5;
int rect2_height = 20;

int dist_wall = 15;

//global variables for glob boxes
float glob_x;
float glob_y;
float glob_width;
float glob_height;



void setup(){
 size(320,240);
 m = new JMyron();//make a new instance of the object
 m.start(width,height);//start a capture at 320x240
 m.trackColor(255,255,255,200);//R, G, B, and range of similarity
 m.minDensity(40); //minimum pixels in the glob required to result in a box
 println("Myron " + m.version());
 noFill();
 
 //from pong:
 rectMode(CENTER_RADIUS);
 noStroke();
 rectm_y = height/2;
 rectm_x = 1;
}

void draw(){
 m.update();//update the camera view
 drawCamera();//draw the camera to the screen
 int[][] b = m.globBoxes();//get the center points

 //moving square:
 rectm_x += rectm_dir * 2;
 rectm_y += dy;
 if(rectm_x < -rectm_width) {
   rectm_x = width+rectm_width;
   rectm_y = random(0, height);
   dy = 0;
 }

 float rect2_y = constrain(mouseY, rect2_height, height-rect2_height);
 
 //attempt at a boolean that will detect collision
String  s = "glob_x > rectm_x+rectm_width || glob_x+glob_width < rectm_x || glob_y > rectm_y+rectm_height || glob_y+glob_height < rectm_y)";
boolean rectangle_collision  = boolean(s);
if (rectangle_collision) {
 println("The boolean is true");
} else {
 println("The boolean is false");
}

 //test to see if rectm touches glob
 float gy = glob_x+glob_width+rectm_width;
 if(rectm_x == gy  
    && rectm_y > glob_y - glob_height - rectm_height  
    && rectm_y < glob_y + glob_height + rectm_height) {
   rectm_dir *= -1;
   println("woo");
 }

 // Test to see if the rectm is touching the rect2
 float py = dist_wall+rect2_width+rectm_width;
 if(rectm_x == py  
    && rectm_y > rect2_y - rect2_height - rectm_height  
    && rectm_y < rect2_y + rect2_height + rectm_height) {
   rectm_dir *= -1;
   if(mouseY != pmouseY) {
dy = (mouseY-pmouseY)/2.0;
if(dy >  5) { dy =  5; }
if(dy < -5) { dy = -5; }
   }
 }  
 
 // If rectm hits rect2 or back wall, reverse direction
 if((rectm_x > 320-rectm_width) && rectm_dir == 1) {
   rectm_dir *= -1;
 }
 
 // If the rectm is touching top or bottom edge, reverse direction
 if(rectm_y > height-rectm_height) {
   dy = dy * -1;
 }
 if(rectm_y < rectm_height) {
   dy = dy * -1;
 }

 // Draw the moving rectangle
 fill(255);
 rect(rectm_x, rectm_y, rectm_width, rectm_height);
 
 // Draw the still rectangle/hitter
 fill(153);
 rect(dist_wall, rect2_y, rect2_width, rect2_height);
 
 //draw the boxes
 stroke(255,0,0);
 for(int i=0;i<b.length;i++){
   rect( b[i][0] , b[i][1] , 20 , 20 );
   float glob_x=b[i][0];
   float glob_y=b[i][1];  
   float glob_width=20;
   float glob_height=20;

 }
 
 

}

void drawCamera(){
 int[] img = m.image(); //get the normal image of the camera
 loadPixels();
 for(int i=0;i<width*height;i++){ //loop through all the pixels
   pixels[i] = img[i]; //draw each pixel to the screen
 }
 updatePixels();

}

void mousePressed(){
 m.settings();//click the window to get the settings
}

public void stop(){
 m.stop();//stop the object
 super.stop();
}

Page Index Toggle Pages: 1