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
executable file
·144 lines (104 loc) · 3.88 KB
/
testApp.cpp
File metadata and controls
executable file
·144 lines (104 loc) · 3.88 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
#include "testApp.h"
static bool removeShapeOffScreen(ofPtr<ofxBox2dBaseShape> shape) {
if (!ofRectangle(0, -400, ofGetWidth(), ofGetHeight()+400).inside(shape.get()->getPosition())) {
return true;
}
return false;
}
//--------------------------------------------------------------
void testApp::setup() {
ofDisableAntiAliasing();
ofBackgroundHex(0x1F2C30);
ofSetLogLevel(OF_LOG_NOTICE);
box2d.init();
box2d.setGravity(0, 10);
box2d.setFPS(30.0);
box2d.registerGrabbing();
box2d.createGround();
ground.setPhysics(0, 0, 0);
ground.setup(box2d.getWorld(), ofGetWidth()/2, ofGetHeight()+200, 400);
groundMesh.setUsage(GL_STATIC_DRAW);
groundMesh.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
for(int i=0; i<100; i++) {
float t = ofMap(i, 0, 99, 0.0, TWO_PI);
ofVec3f v(ofGetWidth()/2, ofGetHeight()+200);
groundMesh.addVertex(v);
v.x += cos(t)*400;
v.y += sin(t)*400;
groundMesh.addVertex(v);
}
}
//--------------------------------------------------------------
void testApp::update() {
ofRemove(circles, removeShapeOffScreen);
ofRemove(boxes, removeShapeOffScreen);
box2d.update();
float r = ofRandom(4, 20);
circles.push_back(ofPtr<ofxBox2dCircle>(new ofxBox2dCircle));
circles.back().get()->setPhysics(3.0, 0.53, 0.1);
circles.back().get()->setup(box2d.getWorld(), ofGetWidth()/2+ofRandom(-100, 100), -200+ofRandom(30, 100), r);
float w = ofRandom(4, 20);
float h = ofRandom(4, 20);
boxes.push_back(ofPtr<ofxBox2dRect>(new ofxBox2dRect));
boxes.back().get()->setPhysics(3.0, 0.53, 0.1);
boxes.back().get()->setup(box2d.getWorld(), ofGetWidth()/2+ofRandom(-100, 100), -200+ofRandom(30, 100), w, h);
}
//--------------------------------------------------------------
void testApp::draw() {
for(int i=0; i<circles.size(); i++) {
ofFill();
ofSetHexColor(0x90d4e3);
circles[i].get()->draw();
}
for(int i=0; i<boxes.size(); i++) {
ofFill();
ofSetHexColor(0xe63b8b);
boxes[i].get()->draw();
}
ofSetColor(255, 100);
groundMesh.draw();
string info = "";
info += "Press [c] for circles\n";
info += "Press [b] for blocks\n";
info += "Total Bodies: "+ofToString(box2d.getBodyCount())+"\n";
info += "Total Joints: "+ofToString(box2d.getJointCount())+"\n\n";
info += "FPS: "+ofToString(ofGetFrameRate())+"\n";
ofSetColor(255);
ofDrawBitmapString(info, 30, 30);
}
//--------------------------------------------------------------
void testApp::keyPressed(int key) {
if(key == '1') {
for(int i=0; i<10; i++) {
float r = ofRandom(2, 5);
circles.push_back(ofPtr<ofxBox2dCircle>(new ofxBox2dCircle));
circles.back().get()->setPhysics(3.0, 0.53, 0.1);
circles.back().get()->setup(box2d.getWorld(), ofGetMouseX()+ofRandom(-10, 10), ofGetMouseY()+ofRandom(-10, 10), r);
}
}
if(key == '2') {
float w = ofRandom(2, 20);
float h = ofRandom(2, 20);
boxes.push_back(ofPtr<ofxBox2dRect>(new ofxBox2dRect));
boxes.back().get()->setPhysics(3.0, 0.53, 0.1);
boxes.back().get()->setup(box2d.getWorld(), ofGetMouseX(), ofGetMouseY(), w, h);
}
}
//--------------------------------------------------------------
void testApp::keyReleased(int key) {
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ) {
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button) {
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button) {
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button) {
}
//--------------------------------------------------------------
void testApp::resized(int w, int h){
}