Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofBackground(10);
ofEnableDepthTest();
cam.setup(320, 240);
}
//--------------------------------------------------------------
void ofApp::update(){
cam.update();
// send camera to optical flow algorithm
if(cam.isFrameNew())
fb.calcOpticalFlow(cam);
avgFlow = fb.getAverageFlow();
smoothAvgFlow = onePole(smoothAvgFlow, avgFlow);
movePlayerUsingFlow();
}
//--------------------------------------------------------------
void ofApp::draw() {
ofSetColor(255);
ofSetLineWidth(1);
ofPushMatrix();
// draw camera feed in top right corner
ofTranslate(ofGetWidth()-cam.getWidth(), 0);
fb.draw(0,0);
cam.draw(0,0);
ofPopMatrix();
ofPushMatrix();
// draw camera feed in top right corner
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofDrawLine(0, 0,smoothAvgFlow.x * 100, smoothAvgFlow.y * 100);
ofPopMatrix();
//printing out location
ofDrawBitmapString("Player loaction: " +
to_string(smoothAvgFlow.x) +
to_string(smoothAvgFlow.y), 20, 50);
game.run();
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
if(key == OF_KEY_LEFT) game.move(-2);
if(key == OF_KEY_RIGHT) game.move(2);
}
ofVec2f ofApp::onePole(ofVec2f current, ofVec2f prev){
//coefficient
float a = 0.9;
return prev + a * (current - prev);
}
void ofApp::movePlayerUsingFlow(){
float speed = 0.5;
float threshold = 0.1;
if(smoothAvgFlow.x < -threshold){
game.move(-speed);
}
else if (smoothAvgFlow.x > threshold){
game.move(speed);
}
}