forked from vanderlin/ofxBox2d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestApp.cpp
More file actions
202 lines (141 loc) · 5.28 KB
/
Copy pathtestApp.cpp
File metadata and controls
202 lines (141 loc) · 5.28 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
ofSetFrameRate(60);
// the world bounds
boundsA.set(0, 0, 500, 500);
boundsB.set(500, 0, 500, 500);
// setup world A
box2dA.init();
box2dA.setFPS(60);
box2dA.setGravity(0, -10);
box2dA.createBounds(boundsA);
box2dA.registerGrabbing();
// setup world B
box2dB.init();
box2dB.setFPS(60);
box2dB.setGravity(0, 10);
box2dB.createBounds(boundsB);
box2dB.registerGrabbing();
// add some cirlces to world A
for(int i=0; i<10; i++) {
shared_ptr<ofxBox2dCircle> c = shared_ptr<ofxBox2dCircle>(new ofxBox2dCircle);
c.get()->setPhysics(1, 0.5, 1);
c.get()->setup(box2dA.getWorld(), 250+ofRandom(-50, 50), 10, ofRandom(10,30));
circlesA.push_back(c);
}
// add some cirlces to world B
for(int i=0; i<10; i++) {
shared_ptr<ofxBox2dCircle> c = shared_ptr<ofxBox2dCircle>(new ofxBox2dCircle);
c.get()->setPhysics(1, 0.5, 1);
c.get()->setup(box2dB.getWorld(), 750+ofRandom(-50, 50), 10, ofRandom(10,30));
circlesB.push_back(c);
}
// we can also have a vector of any shape that is
// shared between both worlds
for(int i=0; i<20; i++) {
shared_ptr<ofxBox2dRect> r = shared_ptr<ofxBox2dRect>(new ofxBox2dRect);
r.get()->setPhysics(1, 0.7, 0.9);
// add to world A
if(i <= 9) {
r.get()->setup(box2dA.getWorld(), 250+ofRandom(-50, 50), 10, ofRandom(10,30), ofRandom(10,30));
}
// add to world B
else {
r.get()->setup(box2dB.getWorld(), 750+ofRandom(-50, 50), 10, ofRandom(10,30), ofRandom(10,30));
}
// add to one vector
sharedRects.push_back(r);
}
}
//--------------------------------------------------------------
void testApp::update(){
box2dA.update();
box2dB.update();
}
//--------------------------------------------------------------
void testApp::drawGravity(ofPoint p, ofPoint gravity) {
float angle = (atan2(gravity.y, gravity.x) * 180 / PI) - 90;
float len = MIN(200, gravity.length()*10); // scale it up a bit
ofPushMatrix();
ofTranslate(p.x, p.y);
ofRotate(angle);
ofDrawLine(0, 0, 0, len);
ofDrawTriangle(0, len,
-5, len-10,
5, len-10);
ofPopMatrix();
}
//--------------------------------------------------------------
void testApp::draw(){
// world A
boundsA.inside(ofGetMouseX(), ofGetMouseY()) ? ofSetColor(80) : ofSetColor(100);
ofFill();
ofRect(boundsA);
// world A
boundsB.inside(ofGetMouseX(), ofGetMouseY()) ? ofSetColor(180) : ofSetColor(200);
ofFill();
ofRect(boundsB);
ofFill();
// A World Circles
for (int i=0; i<circlesA.size(); i++) {
ofSetHexColor(0xBFE364);
circlesA[i].get()->draw();
}
// B World Circles
for (int i=0; i<circlesB.size(); i++) {
ofSetHexColor(0xE83AAB);
circlesB[i].get()->draw();
}
// Shared Rects
for (int i=0; i<sharedRects.size(); i++) {
ofSetHexColor(0x2F9BA1);
ofFill();
sharedRects[i].get()->draw();
}
ofSetColor(255); ofDrawBitmapString("World A\nGravity "+ofToString(box2dA.getGravity().x, 1)+","+ofToString(box2dA.getGravity().y, 1), 250, ofGetHeight()/2);
ofSetColor(90); ofDrawBitmapString("World B\nGravity "+ofToString(box2dB.getGravity().x, 1)+","+ofToString(box2dB.getGravity().y, 1), 750, ofGetHeight()/2);
ofPoint centerA(250, 250);
ofPoint centerB(750, 250);
ofSetColor(255);
drawGravity(centerA, box2dA.getGravity());
drawGravity(centerB, box2dB.getGravity());
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ) {
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
float maxGravity = 100;
if(boundsA.inside(x, y)) {
float gx = ofMap(x, 0, 500, -maxGravity, maxGravity);
float gy = ofMap(y, 0, ofGetHeight(), -maxGravity, maxGravity);
box2dA.setGravity(gx, gy);
}
else if(boundsB.inside(x, y)) {
float gx = ofMap(x, 500, ofGetWidth(), -maxGravity, maxGravity);
float gy = ofMap(y, 0, ofGetHeight(), -maxGravity, maxGravity);
box2dB.setGravity(gx, gy);
}
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){
}