Colourtracking/Movement Problem [Excercise]
in
Core Library Questions
•
1 year ago
Dear processing forum,
Again stuck with a little problem! Here's the deal:
We created a application which spots colors and reacts to it. If you have the color red, it will track that color and as soon as it gets into contact with a enemy it will wipe that enemy off the screen and adds energy to your energybar.
If your energybar is filled up (10 points) you should have the option to show a green picture and it should wipe all enemies off the screen right away like a power attack.
The problem is:
Now as soon as the bar is fully charged, it wil automaticly wipe all enemies, without needing to see the color green appearing in screen. We fear that it might pick up bits of green in the enviourment, but if we adjust the trashhold, it'll become too picky and refuses to use the power attack, even when a green image is brought into view of the webcam.
What is a good way to fix this problem?
------------------
Question 2:
Currently, our enemies are dropping out of the top towards the bottom of the screen. Instead we'd like them to appear in a random location outside of the screen and jump towards a stationairy point within the screen.
any ideas how we could do this?
Legend:
Ninja = enemy (hostile)
Handschoen = color red / green
Ninja = enemy (hostile)
Handschoen = color red / green
worldRecord = trashhold
Code:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// TAB 1
Handschoen handschoen;
Timer timer;
Ninja[] Ninjas;
int totalNinjas = 0;
////////////////////////////////////////////////////////////////////////////
PImage b;
////////////////////////////////////////////////////////////////////////////
// Importing WEBCAM
import processing.video.*;
Capture video;
Timer timer;
Ninja[] Ninjas;
int totalNinjas = 0;
////////////////////////////////////////////////////////////////////////////
PImage b;
////////////////////////////////////////////////////////////////////////////
// Importing WEBCAM
import processing.video.*;
Capture video;
color trackColor;
color trackColor2;
float currentX, currentY;
color currentColor;
float distance;
float distance2;
////////////////////////////////////////////////////////////////////////////
// DIVERSE BENOEMINGEN
int gameOver = 3;
color trackColor2;
float currentX, currentY;
color currentColor;
float distance;
float distance2;
////////////////////////////////////////////////////////////////////////////
// DIVERSE BENOEMINGEN
int gameOver = 3;
int score = 0;
int meter = 0;
int level = 1;
int levens = 10;
int levelEind = 0;
int meter = 0;
int level = 1;
int levens = 10;
int levelEind = 0;
PFont f;
////////////////////////////////////////////////////////////////////////////
void setup() {
size(800,600);
smooth();
println (millis());
////////////////////////////////////////////////////////////////////////////
//SETUP THE WEBCAM
////////////////////////////////////////////////////////////////////////////
void setup() {
size(800,600);
smooth();
println (millis());
////////////////////////////////////////////////////////////////////////////
//SETUP THE WEBCAM
frameRate(120);
video = new Capture(this,width,height,15);
// Start off tracking for red
trackColor = color(255,0,0);
trackColor = color(0,255,0);
smooth();
////////////////////////////////////////////////////////////////////////////
//DIVERSE BENOEMINGEN
handschoen = new Handschoen(32);
Ninjas = new Ninja[50]; //aantal ninjas op het scherm
timer = new Timer(300); //timer per 2 seconden
timer.start();
video = new Capture(this,width,height,15);
// Start off tracking for red
trackColor = color(255,0,0);
trackColor = color(0,255,0);
smooth();
////////////////////////////////////////////////////////////////////////////
//DIVERSE BENOEMINGEN
handschoen = new Handschoen(32);
Ninjas = new Ninja[50]; //aantal ninjas op het scherm
timer = new Timer(300); //timer per 2 seconden
timer.start();
f = createFont("Arial",12,true);
b = loadImage("original.png");
}
////////////////////////////////////////////////////////////////////////////
void draw() {
background(255);
////////////////////////////////////////////////////////////////////////////
//BEGINSCHERM
if (gameOver == 3) {
if (video.available()) {
video.read();
}
video.loadPixels();
image(video,0,0);
distance2 = dist(255,255,255,0,0,0);
// currentX = 0;
// currentY = 0;
if (mousePressed && (mouseButton == LEFT)) {
// Save color where the mouse is clicked in trackColor variable
int loc = mouseX + mouseY*video.width;
trackColor = video.pixels[loc];
textFont(f,40);
text("De kleur is toegevoegd",width/2,height/2+50);
}
if (mousePressed && (mouseButton == RIGHT)) {
// Save color where the mouse is clicked in trackColor variable
int loc = mouseX + mouseY*video.width;
trackColor2 = video.pixels[loc];
textFont(f,40);
text("De kleur is toegevoegd",width/2,height/2+50);
}
fill(0);
textFont(f,50);
text("TESTING test test",200,height/2);
textFont(f,15);
text("Press Any button to start the game",290,height/2+40);
if ((keyPressed == true)) {
gameOver = 1;
score = 0;
meter = 0;
level = 1;
levens = 10;
levelEind = 0;
totalNinjas = 0;
}
}
else {
////////////////////////////////////////////////////////////////////////////
//SETUP THE WEBCAM
if (video.available()) {
video.read();
}
video.loadPixels();
image(video,0,0);
distance2 = dist(255,255,255,0,0,0);
//COLOURTRACKING
// Capture and display the video
if (video.available()) {
video.read();
}
video.loadPixels();
image(video,0,0);
b = loadImage("original.png");
}
////////////////////////////////////////////////////////////////////////////
void draw() {
background(255);
////////////////////////////////////////////////////////////////////////////
//BEGINSCHERM
if (gameOver == 3) {
if (video.available()) {
video.read();
}
video.loadPixels();
image(video,0,0);
distance2 = dist(255,255,255,0,0,0);
// currentX = 0;
// currentY = 0;
if (mousePressed && (mouseButton == LEFT)) {
// Save color where the mouse is clicked in trackColor variable
int loc = mouseX + mouseY*video.width;
trackColor = video.pixels[loc];
textFont(f,40);
text("De kleur is toegevoegd",width/2,height/2+50);
}
if (mousePressed && (mouseButton == RIGHT)) {
// Save color where the mouse is clicked in trackColor variable
int loc = mouseX + mouseY*video.width;
trackColor2 = video.pixels[loc];
textFont(f,40);
text("De kleur is toegevoegd",width/2,height/2+50);
}
fill(0);
textFont(f,50);
text("TESTING test test",200,height/2);
textFont(f,15);
text("Press Any button to start the game",290,height/2+40);
if ((keyPressed == true)) {
gameOver = 1;
score = 0;
meter = 0;
level = 1;
levens = 10;
levelEind = 0;
totalNinjas = 0;
}
}
else {
////////////////////////////////////////////////////////////////////////////
//SETUP THE WEBCAM
if (video.available()) {
video.read();
}
video.loadPixels();
image(video,0,0);
distance2 = dist(255,255,255,0,0,0);
//COLOURTRACKING
// Capture and display the video
if (video.available()) {
video.read();
}
video.loadPixels();
image(video,0,0);
// Before we begin searching, the "world record" for closest color is set to a high number that is easy for the first pixel to beat.
float worldRecord = 500;
float worldRecord2 = 500;
float worldRecord = 500;
float worldRecord2 = 500;
// XY coordinate of closest color
// Begin loop to walk through every pixel
for (int x = 0; x < video.width; x ++ ) {
for (int y = 0; y < video.height; y ++ ) {
int loc = x + y*video.width;
// What is current color
color currentColor = video.pixels[loc];
float r1 = red(currentColor);
float g1 = green(currentColor);
float b1 = blue(currentColor);
float r2 = red(trackColor);
float g2 = green(trackColor);
float b2 = blue(trackColor);
for (int x = 0; x < video.width; x ++ ) {
for (int y = 0; y < video.height; y ++ ) {
int loc = x + y*video.width;
// What is current color
color currentColor = video.pixels[loc];
float r1 = red(currentColor);
float g1 = green(currentColor);
float b1 = blue(currentColor);
float r2 = red(trackColor);
float g2 = green(trackColor);
float b2 = blue(trackColor);
// Using euclidean distance to comp nare colors
float d = dist(r1,g1,b1,r2,g2,b2); // We are using the dist( ) function to compare the current color with the color we are tracking.
float d = dist(r1,g1,b1,r2,g2,b2); // We are using the dist( ) function to compare the current color with the color we are tracking.
// If current color is more similar to tracked color than
// closest color, save current location and current difference
if (d < worldRecord) {
worldRecord = d;
currentX = x;
currentY = y;
}
if (d < worldRecord2) {
worldRecord2 = d;
currentX = x;
currentY = y;
}
}
}
// closest color, save current location and current difference
if (d < worldRecord) {
worldRecord = d;
currentX = x;
currentY = y;
}
if (d < worldRecord2) {
worldRecord2 = d;
currentX = x;
currentY = y;
}
}
}
//THRESHOLD
if (worldRecord < 50) {
// Draw a circle at the tracked pixel
fill(trackColor);
strokeWeight(4.0);
stroke(0);
}
////////////////////////////////////////////////////////////////////////////
// Bij GAME OVER
if (gameOver == 2) {
textFont(f,48);
textAlign(CENTER);
fill(0);
background(255);
text("GAME OVER",300,height/2+20);
text("Press Any button to continue",350,height/2+40);
image(b, 250, height/2-40);
if ((keyPressed == true)) {
gameOver = 1;
score = 0;
meter = 0;
level = 1;
levens = 10;
levelEind = 0;
totalNinjas = 0;
}
}
////////////////////////////////////////////////////////////////////////////
else {
// Locatie van de handschoen (colourtracking)
handschoen.setLocation(mouseX,mouseY);
// Toont de image van de handschoen
handschoen.display();
// Controleert de timer
if (timer.isFinished()) {
if (totalNinjas < Ninjas.length) {
Ninjas[totalNinjas] = new Ninja();
totalNinjas++;
}
timer.start();
}
////////////////////////////////////////////////////////////////////////////
// Laat de ninjas bewegen op het scherm
for (int i = 0; i < totalNinjas; i++ ) {
if (!Ninjas[i].finish) {
Ninjas[i].move();
Ninjas[i].display();
if (Ninjas[i].contactHandschoen()) {
levelEind++;
Ninjas[i].finish();
// Hierdoor verliest de speler een leven.
levens--;
// Bij 0 levens is de speler GAME OVER
if (levens <= 0) {
gameOver = 2;
}
}
////////////////////////////////////////////////////////////////////////////
// ENERGIEMETER
if (handschoen.intersect(Ninjas[i])) {
Ninjas[i].finish();
meter++;
}
fill(0,255,255);
rect(770,580,20,-meter*5);
if (meter >= 30) {
meter = 30;
text("OPGELADEN",230,390);
}
// if ((trackColor2 > color(0,tra && (meter >= 30)) {
if ((worldRecord2 < 50) && (meter >= 30)) {
fill(trackColor2);
strokeWeight(4.0);
stroke(0);
if (timer.isFinished()) {
if (totalNinjas < Ninjas.length) {
Ninjas[totalNinjas] = new Ninja();
totalNinjas++;
}
timer.start();
}
////////////////////////////////////////////////////////////////////////////
// Laat de ninjas bewegen op het scherm
for (int i = 0; i < totalNinjas; i++ ) {
if (!Ninjas[i].finish) {
Ninjas[i].move();
Ninjas[i].display();
if (Ninjas[i].contactHandschoen()) {
levelEind++;
Ninjas[i].finish();
// Hierdoor verliest de speler een leven.
levens--;
// Bij 0 levens is de speler GAME OVER
if (levens <= 0) {
gameOver = 2;
}
}
////////////////////////////////////////////////////////////////////////////
// ENERGIEMETER
if (handschoen.intersect(Ninjas[i])) {
Ninjas[i].finish();
meter++;
}
fill(0,255,255);
rect(770,580,20,-meter*5);
if (meter >= 30) {
meter = 30;
text("OPGELADEN",230,390);
}
// if ((trackColor2 > color(0,tra && (meter >= 30)) {
if ((worldRecord2 < 50) && (meter >= 30)) {
fill(trackColor2);
strokeWeight(4.0);
stroke(0);
textSize(20);
fill(0);
fill(0);
meter = 0;
totalNinjas = 0;
}
////////////////////////////////////////////////////////////////////////////
// SCORE
if (handschoen.intersect(Ninjas[i])) {
Ninjas[i].finish();
levelEind++;
score++;
}
}
}
////////////////////////////////////////////////////////////////////////////
// EINDE LEVEL
if (levelEind >= Ninjas.length) {
// LEVEL UP
level++;
// Reset alle elementen voor het nieuwe level
levelEind = 0;
levens = levens + 1;
timer.setTime(constrain(300-level*25,0,300));
totalNinjas = 0;
}
totalNinjas = 0;
}
////////////////////////////////////////////////////////////////////////////
// SCORE
if (handschoen.intersect(Ninjas[i])) {
Ninjas[i].finish();
levelEind++;
score++;
}
}
}
////////////////////////////////////////////////////////////////////////////
// EINDE LEVEL
if (levelEind >= Ninjas.length) {
// LEVEL UP
level++;
// Reset alle elementen voor het nieuwe level
levelEind = 0;
levens = levens + 1;
timer.setTime(constrain(300-level*25,0,300));
totalNinjas = 0;
}
////////////////////////////////////////////////////////////////////////////
// HUD met aantal levens en score
textFont(f,14);
fill(0);
text("Levens over: " + levens,10,20);
rect(10,24,levens*10,10);
text("Level: " + level,300,20);
text("Score: " + score,300,40);
}
}
}
//////////////////////////////////////////////////////////////////////////// TAB 2
class Handschoen {
float r; // radius
color col; // kleur
float x,y; // locatie
Handschoen(float tempR) {
r = tempR;
col = color(50,10,10,150);
x = 0;
y = 0;
}
void setLocation(int tempX, int tempY) {
x = currentX;
y = currentY;
}
////////////////////////////////////////////////////////////////////////////
void display() {
stroke(0);
fill(col);
ellipse(x,y,r*2,r*2);
}
// Kijkt of de handschoen een ninja raakt
boolean intersect(Ninja d) {
// De afstand tussen ninja en handschoen meten
float distance = dist(x,y,d.x,d.y);
// Controleert de afstand tussen handschoen en ninja
if (distance < r*2 + d.r*2) {
return true;
} else {
return false;
}
}
}
////////////////////////////////////////////////////////////////////////////
text("Score: " + score,300,40);
}
}
}
//////////////////////////////////////////////////////////////////////////// TAB 2
class Handschoen {
float r; // radius
color col; // kleur
float x,y; // locatie
Handschoen(float tempR) {
r = tempR;
col = color(50,10,10,150);
x = 0;
y = 0;
}
void setLocation(int tempX, int tempY) {
x = currentX;
y = currentY;
}
////////////////////////////////////////////////////////////////////////////
void display() {
stroke(0);
fill(col);
ellipse(x,y,r*2,r*2);
}
// Kijkt of de handschoen een ninja raakt
boolean intersect(Ninja d) {
// De afstand tussen ninja en handschoen meten
float distance = dist(x,y,d.x,d.y);
// Controleert de afstand tussen handschoen en ninja
if (distance < r*2 + d.r*2) {
return true;
} else {
return false;
}
}
}
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////// TAB 3
class Ninja {
float x,y; // locatie
float speed; // snelheid
color c; // kleur
float r; // radius
class Ninja {
float x,y; // locatie
float speed; // snelheid
color c; // kleur
float r; // radius
boolean finish = false;
Ninja() {
r = 8; // Grootte van de ninja
x = random(width); // Random locatie op X-as
y = random(height); // Random locatie op Y-as
y = -r*4; // Begint buiten het scherm
speed = random(2,5); // Willekeurige snelheid
c = color(50,100,150); // Kleur
}
r = 8; // Grootte van de ninja
x = random(width); // Random locatie op X-as
y = random(height); // Random locatie op Y-as
y = -r*4; // Begint buiten het scherm
speed = random(2,5); // Willekeurige snelheid
c = color(50,100,150); // Kleur
}
// Beweging ninjas X-as
void move() {
// Snelheid verhogen X-as
y += speed;
// Snelheid verhogen Y-as
//x += speed;
}
////////////////////////////////////////////////////////////////////////////
// Checkt of ninja de grond/speler raakt
boolean contactHandschoen() {
if (y > height + r*4) {
return true;
}
else {
return false;
}
}
void move() {
// Snelheid verhogen X-as
y += speed;
// Snelheid verhogen Y-as
//x += speed;
}
////////////////////////////////////////////////////////////////////////////
// Checkt of ninja de grond/speler raakt
boolean contactHandschoen() {
if (y > height + r*4) {
return true;
}
else {
return false;
}
}
// Toont de Ninja
void display() {
fill(c);
noStroke();
for (int i = 2; i < r; i++ ) {
image(b,x,y,30,30);
// + i*4,i*2,i*2 op de plek van 30,30 om de ninja schaduw te geven
}
}
void display() {
fill(c);
noStroke();
for (int i = 2; i < r; i++ ) {
image(b,x,y,30,30);
// + i*4,i*2,i*2 op de plek van 30,30 om de ninja schaduw te geven
}
}
// Als de ninja is geraakt
void finish() {
finish = true;
}
}
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////// TAB 4
class Timer {
int savedTime;
int totalTime;
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
void setTime(int t) {
totalTime = t;
}
// Start de Timer
void start() {
// De Timer slaat de tijd op in miliseconden
savedTime = millis();
}
////////////////////////////////////////////////////////////////////////////
// The function isFinished() returns true if 5,000 ms have passed.
// The work of the timer is farmed out to this method.
boolean isFinished() {
// Checkt hoe lang de applicatie open staat
int passedTime = millis()- savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}
////////////////////////////////////////////////////////////////////////////
class Timer {
int savedTime;
int totalTime;
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
void setTime(int t) {
totalTime = t;
}
// Start de Timer
void start() {
// De Timer slaat de tijd op in miliseconden
savedTime = millis();
}
////////////////////////////////////////////////////////////////////////////
// The function isFinished() returns true if 5,000 ms have passed.
// The work of the timer is farmed out to this method.
boolean isFinished() {
// Checkt hoe lang de applicatie open staat
int passedTime = millis()- savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}
////////////////////////////////////////////////////////////////////////////
1