This repository has been archived by the owner on Jan 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fishtank.cpp
77 lines (73 loc) · 2.05 KB
/
fishtank.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
/**
* Authors - William Erignac
*
* The FishTank is a static, rectangular GameObject with 3 sides
* and a sensor in the middle.
*/
#include "fishtank.h"
/**
* @brief Constructs a FishTank.
* NOTE: The FishTank doesn't have a physics body upon
* construction, only a definition.
*/
FishTank::FishTank(std::string name, QPointF position, double rotation, QPointF scale) :
PhysicsGameObject(name, position, rotation, scale, constructBodyDefinition(), QImage(":/res/simpleTank.png"))
{}
/**
* @brief Assigns this FishTank a body and adds the 3 walls
* and sensor to the body.
*/
void FishTank::setBody(b2Body* newBody)
{
// Make the sensor...
b2PolygonShape sensorShape;
sensorShape.SetAsBox(5, 2.5);
b2FixtureDef hitBoxDefinition;
hitBoxDefinition.shape = &sensorShape;
hitBoxDefinition.isSensor = true;
hitBoxDefinition.density = 1;
hitBoxDefinition.friction = 1;
newBody->CreateFixture(&hitBoxDefinition);
//Make the bottom...
b2PolygonShape bottomShape;
bottomShape.SetAsBox(5, 1, b2Vec2(0, -2.5), 0);
b2FixtureDef bottomFixture;
bottomFixture.shape = &bottomShape;
bottomFixture.isSensor = false;
bottomFixture.density = 1;
bottomFixture.friction = 1;
newBody->CreateFixture(&bottomFixture);
//Make the left wall...
{
b2PolygonShape wall;
wall.SetAsBox(0.5, 2.5, b2Vec2(-5, 0), 0);
b2FixtureDef wallFixture;
wallFixture.shape = &wall;
wallFixture.isSensor = false;
wallFixture.density = 1;
wallFixture.friction = 1;
newBody->CreateFixture(&wallFixture);
}
//Make the right wall...
{
b2PolygonShape wall;
wall.SetAsBox(0.5, 2.5, b2Vec2(5, 0), 0);
b2FixtureDef wallFixture;
wallFixture.shape = &wall;
wallFixture.isSensor = false;
wallFixture.density = 1;
wallFixture.friction = 1;
newBody->CreateFixture(&wallFixture);
}
//THIS MUST ALWAYS BE CALLED AT THE END OF A SETBODY FOR ANY PHYSICSGAMEOBJECT.
PhysicsGameObject::setBody(newBody);
}
/**
* @brief Returns a body definition for the FishTank.
*/
b2BodyDef FishTank::constructBodyDefinition()
{
b2BodyDef definition;
definition.type = b2_staticBody;
return definition;
}