-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathofApp.cpp
More file actions
81 lines (58 loc) · 1.69 KB
/
ofApp.cpp
File metadata and controls
81 lines (58 loc) · 1.69 KB
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
76
77
78
79
80
81
#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);
cout << smoothAvgFlow << endl;
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();
ofTranslate(ofGetWidth() / 2, ofGetHeight() / 2);
//ofDrawLine(0, 0, avgFlow.x * 100, avgFlow.y * 100);
ofDrawLine(0, 0, smoothAvgFlow.x * 100, smoothAvgFlow.y * 100);
game.run();
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
if(key == OF_KEY_LEFT) game.move(-2);
if(key == OF_KEY_RIGHT) game.move(2);
}
//--------------------------------------------------------------
void ofApp::movePlayerUsingFlow() {
float speed = 0.9;
float threshold = 0.1;
if (smoothAvgFlow.x < -threshold)
{
game.move(-speed);
}
else if (smoothAvgFlow.x > threshold)
{
game.move(speed);
}
}
//--------------------------------------------------------------
ofVec2f ofApp::onePole(ofVec2f current, ofVec2f prev) {
//coefficient
float a = 0.9;
return prev + a * (current - prev);
}