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 APPLY_FORCE_H
20 #define APPLY_FORCE_H
21 
22 class ApplyForce : public Test
23 {
24 public:
ApplyForce()25     ApplyForce()
26     {
27         m_world->SetGravity(b2Vec2(0.0f, 0.0f));
28 
29         const float32 k_restitution = 0.4f;
30 
31         b2Body* ground;
32         {
33             b2BodyDef bd;
34             bd.position.Set(0.0f, 20.0f);
35             ground = m_world->CreateBody(&bd);
36 
37             b2EdgeShape shape;
38 
39             b2FixtureDef sd;
40             sd.shape = &shape;
41             sd.density = 0.0f;
42             sd.restitution = k_restitution;
43 
44             // Left vertical
45             shape.Set(b2Vec2(-20.0f, -20.0f), b2Vec2(-20.0f, 20.0f));
46             ground->CreateFixture(&sd);
47 
48             // Right vertical
49             shape.Set(b2Vec2(20.0f, -20.0f), b2Vec2(20.0f, 20.0f));
50             ground->CreateFixture(&sd);
51 
52             // Top horizontal
53             shape.Set(b2Vec2(-20.0f, 20.0f), b2Vec2(20.0f, 20.0f));
54             ground->CreateFixture(&sd);
55 
56             // Bottom horizontal
57             shape.Set(b2Vec2(-20.0f, -20.0f), b2Vec2(20.0f, -20.0f));
58             ground->CreateFixture(&sd);
59         }
60 
61         {
62             b2Transform xf1;
63             xf1.q.Set(0.3524f * b2_pi);
64             xf1.p = xf1.q.GetXAxis();
65 
66             b2Vec2 vertices[3];
67             vertices[0] = b2Mul(xf1, b2Vec2(-1.0f, 0.0f));
68             vertices[1] = b2Mul(xf1, b2Vec2(1.0f, 0.0f));
69             vertices[2] = b2Mul(xf1, b2Vec2(0.0f, 0.5f));
70 
71             b2PolygonShape poly1;
72             poly1.Set(vertices, 3);
73 
74             b2FixtureDef sd1;
75             sd1.shape = &poly1;
76             sd1.density = 4.0f;
77 
78             b2Transform xf2;
79             xf2.q.Set(-0.3524f * b2_pi);
80             xf2.p = -xf2.q.GetXAxis();
81 
82             vertices[0] = b2Mul(xf2, b2Vec2(-1.0f, 0.0f));
83             vertices[1] = b2Mul(xf2, b2Vec2(1.0f, 0.0f));
84             vertices[2] = b2Mul(xf2, b2Vec2(0.0f, 0.5f));
85 
86             b2PolygonShape poly2;
87             poly2.Set(vertices, 3);
88 
89             b2FixtureDef sd2;
90             sd2.shape = &poly2;
91             sd2.density = 2.0f;
92 
93             b2BodyDef bd;
94             bd.type = b2_dynamicBody;
95             bd.angularDamping = 5.0f;
96             bd.linearDamping = 0.1f;
97 
98             bd.position.Set(0.0f, 2.0f);
99             bd.angle = b2_pi;
100             bd.allowSleep = false;
101             m_body = m_world->CreateBody(&bd);
102             m_body->CreateFixture(&sd1);
103             m_body->CreateFixture(&sd2);
104         }
105 
106         {
107             b2PolygonShape shape;
108             shape.SetAsBox(0.5f, 0.5f);
109 
110             b2FixtureDef fd;
111             fd.shape = &shape;
112             fd.density = 1.0f;
113             fd.friction = 0.3f;
114 
115             for (int i = 0; i < 10; ++i)
116             {
117                 b2BodyDef bd;
118                 bd.type = b2_dynamicBody;
119 
120                 bd.position.Set(0.0f, 5.0f + 1.54f * i);
121                 b2Body* body = m_world->CreateBody(&bd);
122 
123                 body->CreateFixture(&fd);
124 
125                 float32 gravity = 10.0f;
126                 float32 I = body->GetInertia();
127                 float32 mass = body->GetMass();
128 
129                 // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m)
130                 float32 radius = b2Sqrt(2.0f * I / mass);
131 
132                 b2FrictionJointDef jd;
133                 jd.localAnchorA.SetZero();
134                 jd.localAnchorB.SetZero();
135                 jd.bodyA = ground;
136                 jd.bodyB = body;
137                 jd.collideConnected = true;
138                 jd.maxForce = mass * gravity;
139                 jd.maxTorque = mass * radius * gravity;
140 
141                 m_world->CreateJoint(&jd);
142             }
143         }
144     }
145 
Keyboard(unsigned char key)146     void Keyboard(unsigned char key)
147     {
148         switch (key)
149         {
150         case 'w':
151             {
152                 b2Vec2 f = m_body->GetWorldVector(b2Vec2(0.0f, -200.0f));
153                 b2Vec2 p = m_body->GetWorldPoint(b2Vec2(0.0f, 2.0f));
154                 m_body->ApplyForce(f, p);
155             }
156             break;
157 
158         case 'a':
159             {
160                 m_body->ApplyTorque(50.0f);
161             }
162             break;
163 
164         case 'd':
165             {
166                 m_body->ApplyTorque(-50.0f);
167             }
168             break;
169 
170         default:
171             break;
172         }
173     }
174 
Create()175     static Test* Create()
176     {
177         return new ApplyForce;
178     }
179 
180     b2Body* m_body;
181 };
182 
183 #endif
184