1 /*
2 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty.  In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18 
19 #ifndef COMPOUND_SHAPES_H
20 #define COMPOUND_SHAPES_H
21 
22 // TODO_ERIN test joints on compounds.
23 class CompoundShapes : public Test
24 {
25 public:
CompoundShapes()26     CompoundShapes()
27     {
28         {
29             b2BodyDef bd;
30             bd.position.Set(0.0f, 0.0f);
31             b2Body* body = m_world->CreateBody(&bd);
32 
33             b2EdgeShape shape;
34             shape.Set(b2Vec2(50.0f, 0.0f), b2Vec2(-50.0f, 0.0f));
35 
36             body->CreateFixture(&shape, 0.0f);
37         }
38 
39         {
40             b2CircleShape circle1;
41             circle1.m_radius = 0.5f;
42             circle1.m_p.Set(-0.5f, 0.5f);
43 
44             b2CircleShape circle2;
45             circle2.m_radius = 0.5f;
46             circle2.m_p.Set(0.5f, 0.5f);
47 
48             for (int i = 0; i < 10; ++i)
49             {
50                 float32 x = RandomFloat(-0.1f, 0.1f);
51                 b2BodyDef bd;
52                 bd.type = b2_dynamicBody;
53                 bd.position.Set(x + 5.0f, 1.05f + 2.5f * i);
54                 bd.angle = RandomFloat(-b2_pi, b2_pi);
55                 b2Body* body = m_world->CreateBody(&bd);
56                 body->CreateFixture(&circle1, 2.0f);
57                 body->CreateFixture(&circle2, 0.0f);
58             }
59         }
60 
61         {
62             b2PolygonShape polygon1;
63             polygon1.SetAsBox(0.25f, 0.5f);
64 
65             b2PolygonShape polygon2;
66             polygon2.SetAsBox(0.25f, 0.5f, b2Vec2(0.0f, -0.5f), 0.5f * b2_pi);
67 
68             for (int i = 0; i < 10; ++i)
69             {
70                 float32 x = RandomFloat(-0.1f, 0.1f);
71                 b2BodyDef bd;
72                 bd.type = b2_dynamicBody;
73                 bd.position.Set(x - 5.0f, 1.05f + 2.5f * i);
74                 bd.angle = RandomFloat(-b2_pi, b2_pi);
75                 b2Body* body = m_world->CreateBody(&bd);
76                 body->CreateFixture(&polygon1, 2.0f);
77                 body->CreateFixture(&polygon2, 2.0f);
78             }
79         }
80 
81         {
82             b2Transform xf1;
83             xf1.q.Set(0.3524f * b2_pi);
84             xf1.p = xf1.q.GetXAxis();
85 
86             b2Vec2 vertices[3];
87 
88             b2PolygonShape triangle1;
89             vertices[0] = b2Mul(xf1, b2Vec2(-1.0f, 0.0f));
90             vertices[1] = b2Mul(xf1, b2Vec2(1.0f, 0.0f));
91             vertices[2] = b2Mul(xf1, b2Vec2(0.0f, 0.5f));
92             triangle1.Set(vertices, 3);
93 
94             b2Transform xf2;
95             xf2.q.Set(-0.3524f * b2_pi);
96             xf2.p = -xf2.q.GetXAxis();
97 
98             b2PolygonShape triangle2;
99             vertices[0] = b2Mul(xf2, b2Vec2(-1.0f, 0.0f));
100             vertices[1] = b2Mul(xf2, b2Vec2(1.0f, 0.0f));
101             vertices[2] = b2Mul(xf2, b2Vec2(0.0f, 0.5f));
102             triangle2.Set(vertices, 3);
103 
104             for (int32 i = 0; i < 10; ++i)
105             {
106                 float32 x = RandomFloat(-0.1f, 0.1f);
107                 b2BodyDef bd;
108                 bd.type = b2_dynamicBody;
109                 bd.position.Set(x, 2.05f + 2.5f * i);
110                 bd.angle = 0.0f;
111                 b2Body* body = m_world->CreateBody(&bd);
112                 body->CreateFixture(&triangle1, 2.0f);
113                 body->CreateFixture(&triangle2, 2.0f);
114             }
115         }
116 
117         {
118             b2PolygonShape bottom;
119             bottom.SetAsBox( 1.5f, 0.15f );
120 
121             b2PolygonShape left;
122             left.SetAsBox(0.15f, 2.7f, b2Vec2(-1.45f, 2.35f), 0.2f);
123 
124             b2PolygonShape right;
125             right.SetAsBox(0.15f, 2.7f, b2Vec2(1.45f, 2.35f), -0.2f);
126 
127             b2BodyDef bd;
128             bd.type = b2_dynamicBody;
129             bd.position.Set( 0.0f, 2.0f );
130             b2Body* body = m_world->CreateBody(&bd);
131             body->CreateFixture(&bottom, 4.0f);
132             body->CreateFixture(&left, 4.0f);
133             body->CreateFixture(&right, 4.0f);
134         }
135     }
136 
Create()137     static Test* Create()
138     {
139         return new CompoundShapes;
140     }
141 };
142 
143 #endif
144