-
Notifications
You must be signed in to change notification settings - Fork 0
/
dominoslab03part1.cpp
319 lines (276 loc) · 8.46 KB
/
dominoslab03part1.cpp
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
/*
* Base code for CS 251 Software Systems Lab
* Department of Computer Science and Engineering, IIT Bombay
*
*/
#include "cs251_base.hpp"
#include "render.hpp"
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include "GL/freeglut.h"
#endif
#include <cstring>
using namespace std;
#include "dominos.hpp"
namespace cs251
{
/** The is the constructor
* This is the documentation block for the constructor.
*/
dominos_t::dominos_t()
{
//Ground
/*! \var b1
* \brief pointer to the body ground
*/
b2Body* b1;
{
b2EdgeShape shape;
shape.Set(b2Vec2(-90.0f, 0.0f), b2Vec2(90.0f, 0.0f));
b2BodyDef bd;
b1 = m_world->CreateBody(&bd);
b1->CreateFixture(&shape, 0.0f);
}
//Top horizontal shelf
{
b2PolygonShape shape;
shape.SetAsBox(6.0f, 0.25f);
b2BodyDef bd;
bd.position.Set(-31.0f, 30.0f);
b2Body* ground = m_world->CreateBody(&bd);
ground->CreateFixture(&shape, 0.0f);
}
//Dominos
{
b2PolygonShape shape;
shape.SetAsBox(0.1f, 1.0f);
b2FixtureDef fd;
fd.shape = &shape;
fd.density = 20.0f;
fd.friction = 0.1f;
for (int i = 0; i < 10; ++i)
{
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.position.Set(-35.5f + 1.0f * i, 31.25f);
b2Body* body = m_world->CreateBody(&bd);
body->CreateFixture(&fd);
}
}
//Another horizontal shelf
{
b2PolygonShape shape;
shape.SetAsBox(7.0f, 0.25f, b2Vec2(-20.f,20.f), 0.0f);
b2BodyDef bd;
bd.position.Set(1.0f, 6.0f);
b2Body* ground = m_world->CreateBody(&bd);
ground->CreateFixture(&shape, 0.0f);
}
//The pendulum that knocks the dominos off
{
b2Body* b2;
{
b2PolygonShape shape;
shape.SetAsBox(0.25f, 1.5f);
b2BodyDef bd;
bd.position.Set(-36.5f, 28.0f);
b2 = m_world->CreateBody(&bd);
b2->CreateFixture(&shape, 10.0f);
}
b2Body* b4;
{
b2PolygonShape shape;
shape.SetAsBox(0.25f, 0.25f);
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.position.Set(-40.0f, 33.0f);
b4 = m_world->CreateBody(&bd);
b4->CreateFixture(&shape, 2.0f);
}
b2RevoluteJointDef jd;
b2Vec2 anchor;
anchor.Set(-37.0f, 40.0f);
jd.Initialize(b2, b4, anchor);
m_world->CreateJoint(&jd);
}
//The train of small spheres
{
b2Body* spherebody;
b2CircleShape circle;
circle.m_radius = 0.5;
b2FixtureDef ballfd;
ballfd.shape = &circle;
ballfd.density = 1.0f;
ballfd.friction = 0.0f;
ballfd.restitution = 0.0f;
for (int i = 0; i < 1; ++i)
{
b2BodyDef ballbd;
ballbd.type = b2_dynamicBody;
ballbd.position.Set(-22.2f + i*1.0, 26.6f);
spherebody = m_world->CreateBody(&ballbd);
spherebody->CreateFixture(&ballfd);
}
}
//The pulley system
{
b2BodyDef *bd = new b2BodyDef;
bd->type = b2_dynamicBody;
bd->position.Set(-10,15);
bd->fixedRotation = true;
//The open box
b2FixtureDef *fd1 = new b2FixtureDef;
fd1->density = 11.2;
fd1->friction = 0.5;
fd1->restitution = 0.f;
fd1->shape = new b2PolygonShape;
b2PolygonShape bs1;
bs1.SetAsBox(2,0.2, b2Vec2(0.f,-1.9f), 0);
fd1->shape = &bs1;
b2FixtureDef *fd2 = new b2FixtureDef;
fd2->density = 11.2;
fd2->friction = 0.5;
fd2->restitution = 0.f;
fd2->shape = new b2PolygonShape;
b2PolygonShape bs2;
bs2.SetAsBox(0.2,2, b2Vec2(2.0f,0.f), 0);
fd2->shape = &bs2;
b2FixtureDef *fd3 = new b2FixtureDef;
fd3->density = 11.2;
fd3->friction = 0.5;
fd3->restitution = 0.f;
fd3->shape = new b2PolygonShape;
b2PolygonShape bs3;
bs3.SetAsBox(0.2,2, b2Vec2(-2.0f,0.f), 0);
fd3->shape = &bs3;
b2Body* box1 = m_world->CreateBody(bd);
box1->CreateFixture(fd1);
box1->CreateFixture(fd2);
box1->CreateFixture(fd3);
//The bar
bd->position.Set(10,15);
fd1->density = 34.0;
b2Body* box2 = m_world->CreateBody(bd);
box2->CreateFixture(fd1);
// The pulley joint
b2PulleyJointDef* myjoint = new b2PulleyJointDef();
b2Vec2 worldAnchorOnBody1(-10, 15); // Anchor point on body 1 in world axis
b2Vec2 worldAnchorOnBody2(10, 15); // Anchor point on body 2 in world axis
b2Vec2 worldAnchorGround1(-10, 20); // Anchor point for ground 1 in world axis
b2Vec2 worldAnchorGround2(10, 20); // Anchor point for ground 2 in world axis
float32 ratio = 1.0f; // Define ratio
myjoint->Initialize(box1, box2, worldAnchorGround1, worldAnchorGround2, box1->GetWorldCenter(), box2->GetWorldCenter(), ratio);
m_world->CreateJoint(myjoint);
}
//The revolving horizontal platform
{
b2PolygonShape shape;
shape.SetAsBox(2.2f, 0.2f);
b2BodyDef bd;
bd.position.Set(14.0f, 14.0f);
bd.type = b2_dynamicBody;
b2Body* body = m_world->CreateBody(&bd);
b2FixtureDef *fd = new b2FixtureDef;
fd->density = 1.f;
fd->shape = new b2PolygonShape;
fd->shape = &shape;
body->CreateFixture(fd);
b2PolygonShape shape2;
shape2.SetAsBox(0.2f, 2.0f);
b2BodyDef bd2;
bd2.position.Set(14.0f, 16.0f);
b2Body* body2 = m_world->CreateBody(&bd2);
b2RevoluteJointDef jointDef;
jointDef.bodyA = body;
jointDef.bodyB = body2;
jointDef.localAnchorA.Set(0,0);
jointDef.localAnchorB.Set(0,0);
jointDef.collideConnected = false;
m_world->CreateJoint(&jointDef);
}
//The heavy sphere on the platform
{
b2Body* sbody;
b2CircleShape circle;
circle.m_radius = 1.0;
b2FixtureDef ballfd;
ballfd.shape = &circle;
ballfd.density = 50.0f;
ballfd.friction = 0.0f;
ballfd.restitution = 0.0f;
b2BodyDef ballbd;
ballbd.type = b2_dynamicBody;
ballbd.position.Set(14.0f, 18.0f);
sbody = m_world->CreateBody(&ballbd);
sbody->CreateFixture(&ballfd);
}
//The see-saw system at the bottom
{
//The triangle wedge
b2Body* sbody;
b2PolygonShape poly;
b2Vec2 vertices[3];
vertices[0].Set(-1,0);
vertices[1].Set(1,0);
vertices[2].Set(0,1.5);
poly.Set(vertices, 3);
b2FixtureDef wedgefd;
wedgefd.shape = &poly;
wedgefd.density = 10.0f;
wedgefd.friction = 0.0f;
wedgefd.restitution = 0.0f;
b2BodyDef wedgebd;
wedgebd.position.Set(30.0f, 0.0f);
sbody = m_world->CreateBody(&wedgebd);
sbody->CreateFixture(&wedgefd);
//The plank on top of the wedge
b2PolygonShape shape;
shape.SetAsBox(15.0f, 0.2f);
b2BodyDef bd2;
bd2.position.Set(30.0f, 1.5f);
bd2.type = b2_dynamicBody;
b2Body* body = m_world->CreateBody(&bd2);
b2FixtureDef *fd2 = new b2FixtureDef;
fd2->density = 1.f;
fd2->shape = new b2PolygonShape;
fd2->shape = &shape;
body->CreateFixture(fd2);
b2RevoluteJointDef jd;
b2Vec2 anchor;
anchor.Set(30.0f, 1.5f);
jd.Initialize(sbody, body, anchor);
m_world->CreateJoint(&jd);
//The light box on the right side of the see-saw
b2PolygonShape shape2;
shape2.SetAsBox(2.0f, 2.0f);
b2BodyDef bd3;
bd3.position.Set(40.0f, 2.0f);
bd3.type = b2_dynamicBody;
b2Body* body3 = m_world->CreateBody(&bd3);
b2FixtureDef *fd3 = new b2FixtureDef;
fd3->density = 0.01f;
fd3->shape = new b2PolygonShape;
fd3->shape = &shape2;
body3->CreateFixture(fd3);
}
}
sim_t *sim = new sim_t("Dominos", dominos_t::create);
}