Newer
Older
Xavier CORBILLON
committed
#include "simul/objet.h"
Xavier CORBILLON
committed
hilnius
committed
Objet::Objet(b2World & world, Position p, Type type, Angle theta, QColor color) : p(p), type(type), theta(theta), world(&world), p_color(color), radius(0.)
hilnius
committed
{
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(p.x/100.,p.y/100.);
bodyDef.angle = theta;
body = world.CreateBody(&bodyDef);
{
b2CircleShape circle;
b2FixtureDef fixtureDef;
fixtureDef.shape = &circle;
circle.m_radius = 0.381f;
circle.m_p.Set(0.,0.);
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.4f;
fixtureDef.filter.maskBits = 0x3;
fixtureDef.filter.categoryBits = 0x1;
body->CreateFixture(&fixtureDef);
radius = circle.m_radius;
break;
}
{
b2CircleShape circle;
b2FixtureDef fixtureDef;
fixtureDef.shape = &circle;
circle.m_radius = 0.29f;
circle.m_p.Set(0.,0.);
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.4f;
fixtureDef.filter.maskBits = 0x3;
fixtureDef.filter.categoryBits = 0x1;
body->CreateFixture(&fixtureDef);
radius = circle.m_radius;
break;
}
case SANDCYLINDER:
{
b2CircleShape circle;
b2FixtureDef fixtureDef;
fixtureDef.shape = &circle;
circle.m_radius = 0.29f;
circle.m_p.Set(0.,0.);
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.4f;
fixtureDef.filter.maskBits = 0x3;
fixtureDef.filter.categoryBits = 0x1;
body->CreateFixture(&fixtureDef);
radius = circle.m_radius;
break;
case SANDCUBE:
{
b2Vec2 vertices[4];
vertices[0].Set(0.0f, -0.58f);
vertices[1].Set(0.0f, 0.0f);
vertices[2].Set(-0.58f, 0.0f);
vertices[3].Set(-0.58f, -0.58f);
int32 count = 4;
b2PolygonShape polygon;
polygon.Set(vertices, count);
b2FixtureDef fixtureDef;
fixtureDef.shape = &polygon;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.4f;
fixtureDef.filter.maskBits = 0x3;
fixtureDef.filter.categoryBits = 0x1;
body->CreateFixture(&fixtureDef);
radius = 0.58f;
break;
}
case PORTE:
{ b2Vec2 vertices[4];
vertices[0].Set(0.0f, -0.1f);
vertices[1].Set(0.0f, 0.0f);
vertices[2].Set(-1.2f, 0.0f);
vertices[3].Set(-1.2f, -0.1f);
int32 count = 4;
b2PolygonShape polygon;
polygon.Set(vertices, count);
b2FixtureDef fixtureDef;
fixtureDef.shape = &polygon;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.4f;
fixtureDef.filter.maskBits = 0x3;
fixtureDef.filter.categoryBits = 0x1;
body->CreateFixture(&fixtureDef);
radius = 1.2f;
break;
}
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
{ b2Vec2 vertices[4];
vertices[0].Set(0.0f, -2.32f);
vertices[1].Set(0.0f, 0.0f);
vertices[2].Set(-5.1f, 0.0f);
vertices[3].Set(-5.1f, -2.32f);
int32 count = 4;
b2PolygonShape polygon;
polygon.Set(vertices, count);
b2FixtureDef fixtureDef;
fixtureDef.shape = &polygon;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.4f;
fixtureDef.filter.maskBits = 0x3;
fixtureDef.filter.categoryBits = 0x1;
body->CreateFixture(&fixtureDef);
radius = 5.1f;
break;
}
case POISSON:
{
break;
}
case FILET:
{
break;
hilnius
committed
}
Xavier CORBILLON
committed
Objet::~Objet()
{
world->DestroyBody(body);
Xavier CORBILLON
committed
}
hilnius
committed
void Objet::paint(QPainter &pa)
{
switch (type)
{
case SHELL:
{
pa.setBrush(p_color);
pa.setPen(p_color);
pa.drawEllipse(QPoint(p.x,p.y),38.1,38.1);
break;
}
{ QConicalGradient conicalGradient(QPoint(p.x,p.y), 0);
conicalGradient.setColorAt(0.2,QColor(232, 140, 0));
conicalGradient.setColorAt(0.9, Qt::black);
pa.setBrush(conicalGradient);
pa.drawEllipse(QPoint(p.x,p.y), 29, 29);
break;
pa.setBrush(p_color);
pa.setPen(p_color);
pa.drawEllipse(QPoint(p.x,p.y),29,29);
p_color = QColor(0, 0, 0);
pa.setBrush(p_color);
pa.setPen(p_color);
QRectF rectangle(p.x-29, p.y-29,58,58);
int startAngle = 0 * 16;
int spanAngle = 360 * 16;
pa.drawArc(rectangle, startAngle, spanAngle);
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
break;
}
case SANDCUBE:
{p_color = QColor(232, 140, 0);
pa.setBrush(p_color);
pa.setPen(p_color);
pa.drawRect(p.x, p.y,58,58);
p_color = QColor(0, 0, 0);
pa.setBrush(p_color);
pa.setPen(p_color);
pa.drawLine(QPointF(p.x, p.y), QPointF(p.x+58, p.y));
pa.drawLine(QPointF(p.x+58, p.y),QPointF (p.x+58, p.y+58));
pa.drawLine(QPointF(p.x+58, p.y+58), QPointF(p.x, p.y+58));
pa.drawLine(QPointF(p.x, p.y+58), QPointF(p.x, p.y));
break;/*pa.translate(p.x, p.y);
p_color = QColor(232, 140, 0);
pa.setBrush(p_color);
pa.setPen(p_color);
pa.drawRect(592,842, 58, 58);
pa.drawRect(650,842, 58, 58);
pa.drawRect(592,900, 58, 58);
pa.drawRect(650,900, 58, 58);
p_color = QColor(0, 0, 0);
pa.setBrush(p_color);
pa.setPen(p_color);
pa.drawLine(QPointF(592,842), QPointF(708,842));
pa.drawLine(QPointF(592,842), QPointF(592,958));
pa.drawLine(QPointF(592,958), QPointF(708,958));
pa.drawLine(QPointF(708,958), QPointF(708,842));
pa.drawLine(QPointF(650,842), QPointF(650,958));
pa.drawLine(QPointF(592,900), QPointF(708,900));
pa.translate(-p.x, -p.y);
break;*/
hilnius
committed
}
case PORTE:
{
pa.setBrush(p_color);
pa.setPen(p_color);
pa.translate(p.x+240, p.y+0);
pa.rotate(8);
pa.drawRect(0, 0, 120, 10);
pa.rotate(-8);
pa.translate(300, 0);
pa.rotate(8);
pa.drawRect(0, 0, 120, 10);
pa.rotate(-8);
pa.translate(-p.x-540, 0);
break;
hilnius
committed
}
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
{ p_color = QColor(135,206,250);
pa.setBrush(p_color);
pa.setPen(p_color);
pa.drawRect(498,2000, 410, 232);
pa.drawRect(2110,2000, 410, 232);
break;
}
case POISSON:
{
pa.setBrush(p_color);
pa.setPen(p_color);
pa.translate(p.x, p.y);
pa.drawEllipse(0,0,82,50);
pa.translate(130, 30);
pa.rotate(80);
pa.drawEllipse(0,0,82,50);
pa.translate(130, 40);
pa.rotate(80);
pa.drawEllipse(0,0,82,50);
pa.translate(130, 50);
pa.rotate(80);
pa.drawEllipse(0,0,82,50);
pa.rotate(-240);
pa.translate(-p.x-390, -p.y-120);
break;
}
case FILET:
{ pa.translate(p.x, p.y);
QPen pen;
pen.setColor(QColor(0, 0, 0));
QBrush brush(Qt::black);
brush.setStyle(Qt::Dense5Pattern);
pa.setPen(pen);
pa.setBrush(brush);
pa.drawRect(0, 0, 1144, 232);
pa.translate(-p.x, -p.y);
break;
default:
break;
}
return;
}
hilnius
committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
void Objet::updatePos()
{
p.x = 100*body->GetPosition().x;
p.y = 100*body->GetPosition().y;
theta = body->GetAngle()-3.14;
float friction = 0.5;
b2Vec2 velocity = body->GetLinearVelocity();
float32 speed = velocity.Length();
if(speed > FLT_EPSILON)
{
float32 newSpeed = speed - friction;
if(newSpeed < 0)
newSpeed = 0;
velocity *= newSpeed/speed;
body->SetLinearVelocity(velocity);
}
float angularFriction = 0.5;
float angular = body->GetAngularVelocity();
if(angular > 0)
{
angular -= angularFriction;
if(angular < 0)
angular = 0;
}
else
{
angular += angularFriction;
if(angular > 0)
angular = 0;
}
body->SetAngularVelocity(angular);
}
void Objet::moveOutOfField()
{
body->SetTransform(b2Vec2(-1000., -1000.), body->GetAngle());
}
Position Objet::getPosition()
{
return p;
}
Objet::Type Objet::getType()
{
return type;
}
bool Objet::isNear(Position p, Distance radius)
{
return (delta.getNorme() < radius + this->radius);
}