testApp.h
#ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
#include "ofxOpenCv.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
ofVideoGrabber movie;
ofxCvColorImage rgb,hsb;
ofxCvGrayscaleImage hue,sat,bri,filteredLeft,filteredRight;
ofxCvContourFinder contoursLeft,contoursRight;
int w,h;
int findHueLeft;
int findHueRight;
float Steering;
float SteerAbs;
int RedX;
int RedY;
int BlueX;
int BlueY;
int CarX;
int CarY;
int Fuel0X;
int Fuel0Y;
int Fuel1X;
int Fuel1Y;
int n;
ofImage car[10];
ofPoint pos[10], speed[10];
int f ;
ofImage fuel[10];
ofPoint fpos[10], fspeed[10];
int counter;
int pcounter;
};
#endif
testApp.cpp
#include "testApp.h"
//--------------------------------------------------------------
//Light blue circle = top = LEFT click
//Blue circle = bottom = RIGHT click
void testApp::setup() {
ofSetFrameRate(60);
ofBackground(0,0,0);
w = 320;
h = 240;
movie.initGrabber(w, h, true);
//reserve memory for cv images
rgb.allocate(w, h);
hsb.allocate(w, h);
hue.allocate(w, h);
sat.allocate(w, h);
bri.allocate(w, h);
filteredLeft.allocate(w, h);
filteredRight.allocate(w, h);
//Car setup
n=1;
for(int i=0; i<n; i++) {
pos[i].x=650;
pos[i].y=600;
speed[i].x=0;
speed[i].y=0;
cout << pos[i].x << ", " << pos[i].y << endl;
car[i].loadImage("car2.png");
car[i].setAnchorPercent(0.5, 0.5);
}
//Fuel tanks setup
/*
f=5;
for(int i=0; i<f; i++) {
fpos[i].x=ofRandom(450,950);
fpos[i].y=ofRandom(-100,100);
fspeed[i].x=0;
fspeed[i].y=ofRandom(1.0,3.0);
//cout << fpos[i].x << ", " << fpos[i].y << endl;
fuel[i].loadImage("fuel.png");
fuel[i].setAnchorPercent(0.5, 0.5);
}
*/
f=2;
pcounter=1500;
}
//--------------------------------------------------------------
void testApp::update(){
movie.update();
if (movie.isFrameNew()) {
//copy webcam pixels to rgb image
rgb.setFromPixels(movie.getPixels(), w, h);
//mirror horizontal
rgb.mirror(false, true);
//duplicate rgb
hsb = rgb;
//convert to hsb
hsb.convertRgbToHsv();
//store the three channels as grayscale images
hsb.convertToGrayscalePlanarImages(hue, sat, bri);
//filter image based on the hue value were looking for
for (int i=0; i<w*h; i++) {
filteredLeft.getPixels()[i] = ofInRange(hue.getPixels()[i],findHueLeft-5,findHueLeft+5) ? 255 : 0;
}
filteredLeft.flagImageChanged();
//run the contour finder on the filtered image to find blobs with a certain hue
contoursLeft.findContours(filteredLeft, 50, w*h/2, 1, false);
}
if (movie.isFrameNew()) {
//copy webcam pixels to rgb image
rgb.setFromPixels(movie.getPixels(), w, h);
//mirror horizontal
rgb.mirror(false, true);
//duplicate rgb
hsb = rgb;
//convert to hsb
hsb.convertRgbToHsv();
//store the three channels as grayscale images
hsb.convertToGrayscalePlanarImages(hue, sat, bri);
//filter image based on the hue value were looking for
for (int i=0; i<w*h; i++) {
filteredRight.getPixels()[i] = ofInRange(hue.getPixels()[i],findHueRight-5,findHueRight+5) ? 255 : 0;
}
filteredRight.flagImageChanged();
//run the contour finder on the filtered image to find blobs with a certain hue
contoursRight.findContours(filteredRight, 50, w*h/2, 1, false);
}
//================Car movement========================
//Steering values to left
if (RedX <= BlueX-35 && RedY < BlueY) {
Steering = (1.0/(((BlueY-RedY)*0.5) / ((BlueX-RedX)*0.5)));
for(int i=0; i<n; i++) {
speed[i].x=Steering*0.4;
pos[i].x-=speed[i].x;
if (pos[i].x > 1000){
pos[i].x = 1000;
}
if (pos[i].x < 400){
pos[i].x = 400;
}
}
}
//Steering values to right
if (RedX >= BlueX+35 && RedY < BlueY) {
Steering = (1.0/(((BlueY-RedY)*0.5) / ((RedX-BlueX)*0.5)));
for(int i=0; i<n; i++) {
speed[i].x=Steering*0.4;
pos[i].x+=speed[i].x;
if (pos[i].x > 1000){
pos[i].x = 1000;
}
if (pos[i].x < 400){
pos[i].x = 400;
}
}
}
//Neutral steering position
if (abs(RedX-BlueX)<35) {
Steering = 0;
for(int i=0; i<n; i++) {
speed[i].x=Steering*0.4;
pos[i].x+=speed[i].x;
if (pos[i].x > 1000){
pos[i].x = 1000;
}
if (pos[i].x < 400){
pos[i].x = 400;
}
}
}
//Max positions
if (Steering>=15 && RedX>BlueX) {
Steering = 15;
for(int i=0; i<n; i++) {
speed[i].x=Steering*0.4;
pos[i].x+=speed[i].x;
if (pos[i].x > 1000){
pos[i].x = 1000;
}
if (pos[i].x < 400){
pos[i].x = 400;
}
}
}
if (Steering>=15 && RedX<BlueX) {
Steering = 15;
for(int i=0; i<n; i++) {
speed[i].x=Steering*0.4;
pos[i].x-=speed[i].x;
if (pos[i].x > 1000){
pos[i].x = 1000;
}
if (pos[i].x < 400){
pos[i].x = 400;
}
}
}
/* if (RedY >= BlueY) {
Steering = 15;
}
*/
if (Steering<1.0) {
Steering = 0;
for(int i=0; i<n; i++) {
speed[i].x=Steering*0.4;
pos[i].x-=speed[i].x;
if (pos[i].x > 1000){
pos[i].x = 1000;
}
if (pos[i].x < 400){
pos[i].x = 400;
}
}
}
//=========================Fuel Tanks============================
counter++;
if (counter % 300 == 0) {
for(int i=0; i<f; i++) {
fpos[i].x=ofRandom(450,950);
fpos[i].y=-50;
fspeed[i].x=0;
fspeed[i].y=ofRandom(2.0,3.0);
//cout << fpos[i].x << ", " << fpos[i].y << endl;
fuel[i].loadImage("fuel.png");
fuel[i].setAnchorPercent(0.5, 0.5);
}
}
//============Points!=========================
if (abs(CarX - Fuel0X) < 50 && abs(CarY - Fuel0Y) < 100){
pcounter = pcounter+8;
}
if (abs(CarX - Fuel1X) < 50 && abs(CarY - Fuel1Y) < 100){
pcounter = pcounter+8;
}
pcounter--;
}
//--------------------------------------------------------------
void testApp::draw(){
ofSetColor(255,255,255);
//draw all cv images
rgb.draw(0,0);
/*
hsb.draw(640,0);
hue.draw(0,240);
sat.draw(320,240);
bri.draw(640,240);
filteredLeft.draw(0,480);
filteredRight.draw(0,480);
*/
contoursLeft.draw(0,480);
ofSetColor(255, 0, 0);
ofFill();
//draw red circles for found blob (left=red)
for (int i=0; i<contoursLeft.nBlobs; i++) {
ofCircle(contoursLeft.blobs[i].centroid.x, contoursLeft.blobs[i].centroid.y, 20);
RedX = contoursLeft.blobs[i].centroid.x;
RedY = contoursLeft.blobs[i].centroid.y;
}
contoursRight.draw(0,480);
ofSetColor(255, 0, 0);
ofFill();
//draw red circles for found blob (right=blue)
for (int i=0; i<contoursRight.nBlobs; i++) {
ofCircle(contoursRight.blobs[i].centroid.x, contoursRight.blobs[i].centroid.y, 20);
BlueX = contoursRight.blobs[i].centroid.x;
BlueY = contoursRight.blobs[i].centroid.y;
}
//First some info for on the screen...
string RedXcord = "RedX :"+ofToString(RedX);
ofDrawBitmapString(RedXcord, 10,300);
string RedYcord = "RedY :"+ofToString(RedY);
ofDrawBitmapString(RedYcord, 100,300);
string BlueXcord = "BlueX :"+ofToString(BlueX);
ofDrawBitmapString(BlueXcord, 10,330);
string BlueYcord = "BlueY :"+ofToString(BlueY);
ofDrawBitmapString(BlueYcord, 100,330);
string Steer = "steer: "+ofToString(Steering);
ofDrawBitmapString(Steer, 10,480);
string XCar = "CarX: "+ofToString(CarX);
ofDrawBitmapString(XCar, 10,360);
string YCar = "CarY: "+ofToString(CarY);
ofDrawBitmapString(YCar, 100,360);
string XFuel0 = "Fuel0X: "+ofToString(Fuel0X);
ofDrawBitmapString(XFuel0, 10,390);
string YFuel0 = "Fuel0Y: "+ofToString(Fuel0Y);
ofDrawBitmapString(YFuel0, 100,390);
string XFuel1 = "Fuel1X: "+ofToString(Fuel1X);
ofDrawBitmapString(XFuel1, 10,420);
string YFuel1 = "Fuel1Y: "+ofToString(Fuel1Y);
ofDrawBitmapString(YFuel1, 100,420);
string Gascount = "gascounter: "+ofToString(counter);
ofDrawBitmapString(Gascount, 10, 450);
ofSetColor(255, 255, 0);
string Points = "Fuel Points :"+ofToString(pcounter);
ofDrawBitmapString(Points, 10,260);
ofBackground(0,0,0); // background color
ofSetColor(255,255,255); // text white
/* =======Drawing the fuels===========
for (int i=0; i<f; i++) {
fuel[i].draw(fpos[i].x, fpos[i].y);
if (fpos[i].y > 760){
fuel[i].clear();
if (i<3){
fuel[i].draw(fpos[i].x, fpos[i].y);
}
}
}
*/
//=======Drawing the car and gas===========
for (int i=0; i<(f+1); i++) {
fuel[i].draw(fpos[i].x, fpos[i].y);
fpos[i].y+=fspeed[i].y;
Fuel0X = fpos[0].x+30;
Fuel0Y = fpos[0].y+30;
Fuel1X = fpos[1].x+30;
Fuel1Y = fpos[1].y+30;
}
for (int i=0; i<n; i++) {
car[i].draw(pos[i].x, pos[i].y);
CarX = ((pos[i].x)+50);
CarY = ((pos[i].y)+100);
}
if (pcounter <= 0) {
car[0].clear();
ofDrawBitmapString("GAME OVER!", 600,650);
}
if (pcounter >= 6000) {
car[0].clear();
ofDrawBitmapString("YOU WIN!", 600,650);
}
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button) {
//calculate local mouse x,y in image
int mx = x % w;
int my = y % h;
if (button == 0) {
//get hue value on left mouse click
findHueRight = hue.getPixels()[my*w+mx];
}
if (button == 2) {
//get hue value on right mouse click
findHueLeft = hue.getPixels()[my*w+mx];
}
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
if (key == 'q') {
Steering = 0;
}
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}