We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello guys!
I have a little colortracking game, which includes a webcam. My only problem now is that the video is not flipping as it should be. I really tried out all the code examples here in this forum but nothing worked! Sometimes it did flip the video correctly but then I wasn't able to track the color anymore... really don't know why.
So, first things first, here is my code:
import processing.video.*;
Capture webcam;
color trackColor;
float trackR;
float trackG;
float trackB;
int topLeftX;
int topLeftY;
int bottomRightX;
int bottomRightY;
int maxColorDifference;
//CHERRY
PImage cherryPic;
Cherry[] cherry;
int count = 5;
int speed = 1;
int cherryX = -40;
int cherryY = -40;
int score = 0;
boolean caught;
boolean gameOver;
//basket
PImage basketPic;
int basketWidth = 103;
int basketHeight = 82;
//screen states
final int INACTIVE = 0;
final int ACTIVE = 1;
int state = INACTIVE;
void setup()
{
size( 640, 480 );
webcam = new Capture( this, width, height );
trackColor = color( 255, 128, 64 );
trackR = red(trackColor); //(trackColor >> 16) & 0xff;
trackG = green(trackColor); //(trackColor >> 8) & 0xff;
trackB = blue(trackColor); //trackColor & 0xff;
maxColorDifference = 40;
topLeftX = width;
topLeftY = height;
bottomRightX = 0;
bottomRightY = 0;
//CHERRY
cherryPic = loadImage("cherry.png");
cherry = new Cherry[count];
for (int i=0; i<count; i++) {
cherry[i]=new Cherry(cherryPic, cherryX, cherryY);
}
//BASKET
basketPic = loadImage("basket.png");
webcam.start();
}
void draw()
{
//WEBCAM
if ( webcam.available() ) {
webcam.read();
image( webcam, 0, 0 );
loadPixels();
int counter = 0;
for ( int j = 0; j < webcam.height; j++ ) {
for ( int i = 0; i < webcam.width; i++ ) {
color c = webcam.pixels[counter];
float r = red(c);
float g = green(c);
float b = blue(c);
float colorDifference = dist( r, g, b, trackR, trackG, trackB );
if ( colorDifference < maxColorDifference ) {
if ( i < topLeftX ) {
topLeftX = i;
}
if ( j < topLeftY ) {
topLeftY = j;
}
if ( i > bottomRightX ) {
bottomRightX = i;
}
if ( j > bottomRightY ) {
bottomRightY = j;
}
}
counter++;
}
}
updatePixels();
//show basket
image(basketPic, topLeftX, topLeftY);
//CHERRIES
for (int i = 0; i<count; i++) {
cherry[i].drawCherry();
}
//start game with click on cherry
if (state == INACTIVE) {
fill(255);
textSize(48);
text("Catch It", 240, 100);
String text = "Click Your Tracking Point To Start";
textSize(25);
text(text, 120, 140);
}
//if state == ACTIVE
else {
caught = false;
gameOver = false;
for (int i=0; i<count; i++ ) {
cherry[i].speedItUp(speed);
if (cherry[i].caught(topLeftX, topLeftY, basketWidth, basketHeight)) {
caught = true;
break; //if one cherry is caugt, end if
}
if (cherry[i].posY > height) {
gameOver = true;
}
}
if (caught) {
score+=10;
}
//show score
textSize(18);
text("Score: " + score, 15, 460);
//increase speed when score > 100
if (score == 100) {
speed++;
}
println(score + " speed " + speed);
}
}
//increase speed after 15 seconds
int sec = second();
if (sec == 40) {
speed++;
println(sec + "speed " + speed);
}
//"Game Over" Screen at negative points
if (gameOver) {
background(0);
textSize(90);
text("GAME OVER", 70, 200);
textSize(35);
text("Your Score: " + score, 200, 270);
//textSize(20);
//text("Press ENTER To Start A New Game", 160, 380);
}
// reset tracking points
topLeftX = width;
topLeftY = height;
bottomRightX = 0;
bottomRightY = 0;
}
void mousePressed()
{
if (state == INACTIVE) {
trackColor = webcam.get( mouseX, mouseY );
trackR = red(trackColor);
trackG = green(trackColor);
trackB = blue(trackColor);
state = ACTIVE;
for (int i=0; i<count; i++ ) {
cherry[i].setCord();
}
}
}
//Enter-Taste bewirkt Neustart nur bei Game Over oder bei gewonnenem Spiel
/*void keyPressed()
{
if (gameOver && key==ENTER) {
state = ACTIVE;
}
} */
I worked with this bit of code, which I think should flip the video:
pushMatrix();
scale(-1, 1);
translate(-webcam.width, 0);
image(webcam, 0, 0);
popMatrix();
But whereever I put it, it doesn't work correctly. It doesn't flip the image, or it does but the tracking function doesn't work anymore! Can you please tell me, where I have to put this code? Or which other code would do the same? Or if I missed something else?
Thank in advance.