Debian Codegraph @

Tests/Collision/collision.cpp

raw
1
2 #include <ClanLib/core.h>
3 #include <ClanLib/application.h>
4 #include <ClanLib/display.h>
5 #include <ClanLib/gl.h>
6
7
8 void print_usage()
9 {
10 // Explain the keys in the terminal
11 std::cout << "----------: Controls :----------" << std::endl;
12 std::cout << "Arrows: move the triangle" << std::endl;
13 std::cout << "Shift+Arrows: move the other outline" << std::endl;
14 std::cout << "'e': rotate triangle" << std::endl;
15 std::cout << "'r': rotate other figure" << std::endl;
16 std::cout << "'i': toggle drawing of sub-circles on triangle" << std::endl;
17 std::cout << "'o': toggle drawing of sub-circles on other figure" << std::endl;
18 std::cout << "'s': toggle drawing of surfaces" << std::endl;
19 std::cout << "+/-: (in/de)crease the radius-multiplier" << std::endl;
20 std::cout << "'g': use smallest enclosing disc only" << std::endl;
21 std::cout << "'c': replace contours with convex hull" << std::endl;
22 std::cout << "'h': show this help again" << std::endl;
23 std::cout << "1-8: scale the middle outline" << std::endl;
24 };
25
26 class SurfaceApp : public CL_ClanApplication
27 {
28 public:
29 virtual int main(int argc, char **argv)
30 {
31 // Create a console window for text-output if not available
32 CL_ConsoleWindow console("Console");
33 console.redirect_stdio();
34
35 try
36 {
37 std::string file1("images/triangle.png");
38 //std::string file2("images/weird.png");
39 //std::string file2("images/edge_test2.png");
40 std::string file2("images/inside_test5.png");
41
42 if( argc == 3 )
43 {
44 file1 = argv[1];
45 file2 = argv[2];
46 }
47
48 CL_SetupCore c_init;
49 CL_SetupDisplay d_init;
50 CL_SetupGL g_init;
51
52 CL_DisplayWindow window("Surface outline", 800, 600);
53
54 int tri_x_pos = 0;
55 int tri_y_pos = 0;
56 int other_x_pos = CL_Display::get_width()/2;
57 int other_y_pos = CL_Display::get_height()/2;
58
59 draw_limit = 0;
60
61 bool draw_sub_on_co1 = true;
62 bool draw_sub_on_co2 = true;
63 bool draw_surfaces = false;
64 float sub_circle_multiplier = 3.5f;
65
66 ////////////////////////////////////////////
67 // load resources:
68
69 CL_Surface surface(file1);
70 CL_Surface surface2(file2);
71
72 surface.set_rotation_hotspot(origin_center);
73 surface.set_alignment(origin_center);
74
75 surface2.set_rotation_hotspot(origin_center);
76 surface2.set_alignment(origin_center);
77
78 ////////////////////////////////////////////
79 // Collision code
80
81 // load outlines
82 CL_CollisionOutline co1(file1);
83 CL_CollisionOutline co2(file2, 128, accuracy_raw, true);
84
85 // print some info about the outlines:
86 unsigned int size = co1.get_contours().size();
87
88 std::cout << "outline 1: " << co1.get_contours().size() << " contour"<<(co1.get_contours().size()==1 ? "" : "s")<<"\n";
89 std::cout << "outline 2: " << co2.get_contours().size() << " contour"<<(co2.get_contours().size()==1 ? "" : "s")<<"\n";
90
91 std::vector<CL_Contour>::iterator it;
92
93 std::cout << "outline 1: " << std::endl;
94 int i=1;
95 for( it = co1.get_contours().begin(); it!=co1.get_contours().end(); ++it )
96 {
97 std::cout << "\tcontour " << i << ": " << (*it).points.size() << " points\n";
98 i++;
99 }
100
101 std::cout << "outline 2: " << std::endl;
102 i=1;
103 for( it = co2.get_contours().begin(); it!=co2.get_contours().end(); ++it )
104 {
105 std::cout << "\tcontour " << i << ": " << (*it).points.size() << " points\n";
106 i++;
107 }
108
109
110 co1.set_alignment(origin_center);
111 co1.set_rotation_hotspot(origin_center);
112 co1.enable_collision_info(true,true,true,true);
113 co1.set_inside_test(true);
114
115 co2.set_alignment(origin_center);
116 co2.set_rotation_hotspot(origin_center);
117 co2.enable_collision_info(true,true,true,true);
118 co2.set_inside_test(true);
119
120 print_usage();
121
122 // Loop until the user hits escape:
123 while (CL_Keyboard::get_keycode(CL_KEY_ESCAPE) == false)
124 {
125 CL_Display::clear(CL_Color::ghostwhite);
126
127 if( CL_Keyboard::get_keycode(CL_KEY_SHIFT) )
128 {
129 // Control Other
130 if( CL_Keyboard::get_keycode(CL_KEY_RIGHT) )
131 other_x_pos+=1;
132 if( CL_Keyboard::get_keycode(CL_KEY_LEFT) )
133 other_x_pos-=1;
134
135 if( CL_Keyboard::get_keycode(CL_KEY_UP) )
136 other_y_pos-=1;
137 if( CL_Keyboard::get_keycode(CL_KEY_DOWN) )
138 other_y_pos+=1;
139 }
140 else
141 {
142 // Control Triangle
143 if( CL_Keyboard::get_keycode(CL_KEY_RIGHT) )
144 tri_x_pos+=1;
145 if( CL_Keyboard::get_keycode(CL_KEY_LEFT) )
146 tri_x_pos-=1;
147
148 if( CL_Keyboard::get_keycode(CL_KEY_UP) )
149 tri_y_pos-=1;
150 if( CL_Keyboard::get_keycode(CL_KEY_DOWN) )
151 tri_y_pos+=1;
152 }
153
154 if( CL_Keyboard::get_keycode(CL_KEY_E) )
155 {
156 surface.rotate(0.1f);
157 co1.rotate(0.1f);
158 }
159 if( CL_Keyboard::get_keycode(CL_KEY_R) )
160 {
161 co2.rotate(0.1f);
162 surface2.rotate(0.1f);
163 }
164 if( CL_Keyboard::get_keycode(CL_KEY_1) )
165 {
166 co2.set_scale(1.0f, 1.0f);
167 surface2.set_scale(1.0f, 1.0f);
168 CL_System::sleep(100);
169 }
170 if( CL_Keyboard::get_keycode(CL_KEY_2) )
171 {
172 co2.set_scale(2.0f, 2.0f);
173 surface2.set_scale(2.0f, 2.0f);
174 CL_System::sleep(100);
175 }
176 if( CL_Keyboard::get_keycode(CL_KEY_3) )
177 {
178 co2.set_scale(3.0f, 3.0f);
179 surface2.set_scale(3.0f, 3.0f);
180 CL_System::sleep(100);
181 }
182 if( CL_Keyboard::get_keycode(CL_KEY_4) )
183 {
184 co2.set_scale(4.0f, 4.0f);
185 surface2.set_scale(4.0f, 4.0f);
186 CL_System::sleep(100);
187 }
188 if( CL_Keyboard::get_keycode(CL_KEY_5) )
189 {
190 co2.set_scale(5.0f, 5.0f);
191 surface2.set_scale(5.0f, 5.0f);
192 CL_System::sleep(100);
193 }
194 if( CL_Keyboard::get_keycode(CL_KEY_6) )
195 {
196 co2.set_scale(6.0f, 6.0f);
197 surface2.set_scale(6.0f, 6.0f);
198 CL_System::sleep(100);
199 }
200 if( CL_Keyboard::get_keycode(CL_KEY_7) )
201 {
202 co2.set_scale(7.0f, 7.0f);
203 surface2.set_scale(7.0f, 7.0f);
204 CL_System::sleep(100);
205 }
206 if( CL_Keyboard::get_keycode(CL_KEY_8) )
207 {
208 co2.set_scale(8.0f, 8.0f);
209 surface2.set_scale(8.0f, 8.0f);
210 CL_System::sleep(100);
211 }
212 if( CL_Keyboard::get_keycode(CL_KEY_I) )
213 {
214 draw_sub_on_co1 = !draw_sub_on_co1;
215 CL_System::sleep(100);
216 }
217 if( CL_Keyboard::get_keycode(CL_KEY_O) )
218 {
219 draw_sub_on_co2 = !draw_sub_on_co2;
220 CL_System::sleep(100);
221 }
222 if( CL_Keyboard::get_keycode(CL_KEY_S) )
223 {
224 draw_surfaces = !draw_surfaces;
225 CL_System::sleep(100);
226 }
227 if( CL_Keyboard::get_keycode(CL_KEY_SUBTRACT) )
228 {
229 sub_circle_multiplier -= 0.2f;
230 co1.calculate_sub_circles(sub_circle_multiplier);
231 co2.calculate_sub_circles(sub_circle_multiplier);
232 CL_System::sleep(50);
233 }
234 if( CL_Keyboard::get_keycode(CL_KEY_ADD) )
235 {
236 sub_circle_multiplier += 0.2f;
237 co1.calculate_sub_circles(sub_circle_multiplier);
238 co2.calculate_sub_circles(sub_circle_multiplier);
239 CL_System::sleep(50);
240 }
241 if( CL_Keyboard::get_keycode(CL_KEY_G) )
242 {
243 co1.calculate_smallest_enclosing_discs();
244 co2.calculate_smallest_enclosing_discs();
245 CL_System::sleep(50);
246 }
247 if( CL_Keyboard::get_keycode(CL_KEY_C) )
248 {
249 co1.calculate_convex_hulls();
250 co2.calculate_convex_hulls();
251 CL_System::sleep(200);
252 }
253 if( CL_Keyboard::get_keycode(CL_KEY_H) )
254 {
255 print_usage();
256 CL_System::sleep(200);
257 }
258
259 // -----------------------------------
260 // surfaces
261 if(draw_surfaces)
262 {
263 surface.draw(tri_x_pos,tri_y_pos);
264 surface2.draw(other_x_pos, other_y_pos);
265 }
266
267 // -----------------------------------
268 // co1
269 co1.set_translation((float)tri_x_pos, (float)tri_y_pos);
270 co1.draw(0.0,0.0,CL_Color::limegreen);
271 if(draw_sub_on_co1)
272 co1.draw_sub_circles(0.0, 0.0, CL_Color::blue);
273
274 // -----------------------------------
275 // co2
276 co2.set_translation((float)other_x_pos, (float)other_y_pos);
277 co2.draw(0.0, 0.0, CL_Color::red );
278 if(draw_sub_on_co2)
279 co2.draw_sub_circles(0.0, 0.0, CL_Color::blue );
280
281
282 // -----------------------------------
283 // collision testing
284 if( co2.collide(co1) )
285 {
286 CL_Display::fill_rect(CL_Rect(0,0,800,600), CL_Color(55,40,250,20));
287 const std::vector<CL_CollidingContours> &colpointinfo = co2.get_collision_info();
288 for(int c = 0; c < colpointinfo.size(); c++)
289 {
290 std::cout << "c1:("<<colpointinfo[c].contour1<<",c2:"<<colpointinfo[c].contour2<<") (inside:"<<colpointinfo[c].inside<<")\n";
291 for(int p = 0; p < colpointinfo[c].points.size(); p++)
292 {
293 CL_Pointf p1 = colpointinfo[c].points[p].point;
294 CL_Pointf p2 = p1 + CL_Pointf(colpointinfo[c].points[p].normal.x * 20.0f, colpointinfo[c].points[p].normal.y * 20.0f);
295 CL_Display::draw_line(
296 int(p1.x+0.5f), int(p1.y+0.5f),
297 int(p2.x+0.5f), int(p2.y+0.5f),
298 (colpointinfo[c].points[p].is_entry ? CL_Color::green : CL_Color::red));
299 std::cout << "p1:("<<p1.x<<","<<p1.y<<") " << (colpointinfo[c].points[p].is_entry ? "entry" : "exit") << "\n";
300 }
301 // Paint the pen-depth and normal from the deepest points
302 {
303 CL_Pointf p1 = colpointinfo[c].contour1_deep_point;
304 CL_Pointf p2 = p1 + CL_Pointf(colpointinfo[c].penetration_normal.x * colpointinfo[c].penetration_depth, colpointinfo[c].penetration_normal.y * colpointinfo[c].penetration_depth);
305 CL_Display::draw_line(int(p1.x+0.5f), int(p1.y+0.5f),int(p2.x+0.5f), int(p2.y+0.5f),CL_Color::blue);
306
307 p1 = colpointinfo[c].contour2_deep_point;
308 p2 = p1 + CL_Pointf(colpointinfo[c].penetration_normal.x * colpointinfo[c].penetration_depth, colpointinfo[c].penetration_normal.y * colpointinfo[c].penetration_depth);
309 CL_Display::draw_line(int(p1.x+0.5f), int(p1.y+0.5f),int(p2.x+0.5f), int(p2.y+0.5f),CL_Color::blue);
310 }
311 }
312 }
313
314 // Flip front and backbuffer. This makes the changes visible:
315 CL_Display::flip();
316
317 // Update keyboard input and handle system events:
318 CL_System::keep_alive(10);
319 }
320 }
321 catch (CL_Error err)
322 {
323 std::cout << "Error: " << err.message.c_str() << std::endl;
324
325 // Display console close message and wait for a key
326 console.display_close_message();
327 }
328
329 return 0;
330 }
331
332 unsigned int draw_limit;
333
334 } app;