-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPhysicsWorld.cpp
238 lines (204 loc) · 8.17 KB
/
PhysicsWorld.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
#include "PhysicsWorld.h"
enum collisionCategory {
C_NONE = 0x0000,
C_PLAYER = 0x0001,
C_ENEMY = 0x0002,
C_PLATFORM = 0x0004,
C_BULLET = 0x0008,
C_FIRE = 0x0016,
C_STAR = 0x0032,
C_ENEMYSPAWNER = 0x0064,
C_PLAYERSPAWNER = 0x0128,
C_WALL = 0x0256
};
PhysicsWorld::PhysicsWorld() {
// Initialization
// make it 30 so we fall faster than real life
gravity = new b2Vec2(0.0f, -40.0f);
world = new b2World(*gravity);
world->SetAllowSleeping(false);
contactListener = new ContactListener();
world->SetContactListener(contactListener);
contactListener->Attach(&GameEntityCreator::getInstance());
GameEntityCreator::getInstance().Attach(this);
}
PhysicsWorld& PhysicsWorld::getInstance()
{
static PhysicsWorld physicsWorld;
return physicsWorld;
}
// Adds an entity to the physics world by it's ID
void PhysicsWorld::AddObject(EntityID id) {
EntityCoordinator& coordinator = EntityCoordinator::getInstance();
// get the components of the entity
PhysicsComponent* physComponent = &coordinator.GetComponent<PhysicsComponent>(id);
Transform* transformComponent = &coordinator.GetComponent<Transform>(id);
MovementComponent* moveComponent = &coordinator.GetComponent<MovementComponent>(id);
moveComponent->physComponent = physComponent;
// create physics body for entity
b2BodyDef bodyDef = b2BodyDef();
bodyDef.type = physComponent->bodyType;
bodyDef.position.Set(physComponent->x, physComponent->y);
bodyDef.userData.pointer = reinterpret_cast<uintptr_t>(physComponent);
bodyDef.bullet = coordinator.entityHasTag(BULLET, id);
b2World* world = PhysicsWorld::getInstance().world;
physComponent->box2dBody = world->CreateBody(&bodyDef);
B2DBodyAddGuardFunction(physComponent->box2dBody,id);
physComponent->entityID = id;
physComponent->box2dBody->SetFixedRotation(true);
if (physComponent->box2dBody) {
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(physComponent->halfWidth, physComponent->halfHeight);
// set physics body fixture
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = physComponent->density;
fixtureDef.friction = physComponent->friction;
fixtureDef.restitution = 0;
// set collision filter based on tag
if (coordinator.entityHasTag(PLAYER, id)) {
physComponent->box2dBody->SetGravityScale(1.5);
fixtureDef.filter.categoryBits = C_PLAYER;
fixtureDef.filter.maskBits = C_PLATFORM | C_ENEMY;
}
else if (coordinator.entityHasTag(ENEMY, id)) {
physComponent->box2dBody->SetGravityScale(1.5);
moveComponent->setVelocity(2.0, 0); // THIS IS TEMPORARY,
fixtureDef.filter.categoryBits = C_ENEMY;
fixtureDef.filter.maskBits = C_PLAYER | C_PLATFORM | C_BULLET;
}
else if (coordinator.entityHasTag(PLATFORM, id)) {
fixtureDef.filter.categoryBits = C_PLATFORM;
fixtureDef.filter.maskBits = C_PLAYER | C_ENEMY | C_BULLET;
}
else if (coordinator.entityHasTag(BULLET, id)) {
fixtureDef.filter.categoryBits = C_BULLET;
fixtureDef.filter.maskBits = C_PLATFORM | C_ENEMY | C_WALL;
}
else if (coordinator.entityHasTag(FIRE, id)) {
fixtureDef.filter.categoryBits = C_FIRE;
fixtureDef.filter.maskBits = C_PLAYER | C_ENEMY;
}
else if (coordinator.entityHasTag(STAR, id)) {
fixtureDef.filter.categoryBits = C_STAR;
fixtureDef.filter.maskBits = C_PLAYER;
}
else if (coordinator.entityHasTag(ENEMYSPAWNER, id)) {
fixtureDef.filter.categoryBits = C_ENEMYSPAWNER;
fixtureDef.filter.maskBits = C_PLAYER;
}
else if (coordinator.entityHasTag(PLAYERSPAWNER, id)) {
fixtureDef.filter.categoryBits = C_PLAYERSPAWNER;
fixtureDef.filter.maskBits = C_NONE;
}
else if (coordinator.entityHasTag(WALL, id)) {
fixtureDef.filter.categoryBits = C_WALL;
fixtureDef.filter.maskBits = C_PLAYER | C_ENEMY | C_BULLET;
}
physComponent->box2dBody->CreateFixture(&fixtureDef);
}
}
// Update all the physics bodies currently active in the world
void PhysicsWorld::Update(EntityCoordinator* coordinator) {
if (world) {
world->Step(timeStep, velocityIterations, positionIterations);
std::shared_ptr<EntityQuery> entityQuery = coordinator->GetEntityQuery({
coordinator->GetComponentType<PhysicsComponent>(),
coordinator->GetComponentType<Transform>(),
coordinator->GetComponentType<MovementComponent>()
}, {});
int entitiesFound = entityQuery->totalEntitiesFound();
ComponentIterator<PhysicsComponent> physCompIterator = ComponentIterator<PhysicsComponent>(entityQuery);
ComponentIterator<Transform> transformCompIterator = ComponentIterator<Transform>(entityQuery);
ComponentIterator<MovementComponent> moveCompIterator = ComponentIterator<MovementComponent>(entityQuery);
for (int i = 0; i < entitiesFound; i++) {
PhysicsComponent* physComponent = physCompIterator.nextComponent();
Transform* transform = transformCompIterator.nextComponent();
MovementComponent* moveComponent = moveCompIterator.nextComponent();
if (physComponent->isFlaggedForDelete) {
EntityCoordinator::getInstance().scheduleEntityToDelete(physComponent->entityID);
continue;
}
UpdateTransform(transform, physComponent);
UpdateMovementComponent(moveComponent, physComponent);
}
}
}
// Destroy an object from physics world
void PhysicsWorld::DestoryObject(EntityID id)
{
EntityCoordinator& coordinator = EntityCoordinator::getInstance();
PhysicsComponent* physComponent = &coordinator.GetComponent<PhysicsComponent>(id);
physComponent->isFlaggedForDelete = true;
}
void PhysicsWorld::B2DBodyDeleteGuardFunction(b2Body* body,EntityID id)
{
std::vector<Tag> tags = EntityCoordinator::getInstance().getTagsForEntity(id);
auto activeFind = activeBodies.find(body);
if (activeFind == activeBodies.end())
{
}
else
{
activeBodies.erase(body);
}
auto deactiveFind = deactivatedBodies.find(body);
if (deactiveFind != deactivatedBodies.end())
{
}
else
{
deactivatedBodies.emplace(body);
}
PhysicsWorld::getInstance().world->DestroyBody(body);
}
void PhysicsWorld::B2DBodyAddGuardFunction(b2Body* body, EntityID id)
{
auto activeFind = activeBodies.find(body);
if (activeFind != activeBodies.end())
{
}
else
{
activeBodies.emplace(body);
}
auto deactiveFind = deactivatedBodies.find(body);
if (deactiveFind != deactivatedBodies.end())
{
deactivatedBodies.erase(body);
}
}
ContactListener* PhysicsWorld::GetContactListener()
{
return contactListener;
}
// Update an entity's movement component based on the physics body
void PhysicsWorld::UpdateMovementComponent(MovementComponent* moveComponent, PhysicsComponent* physComponent) {
moveComponent->xVelocity = physComponent->box2dBody->GetLinearVelocity().x;
moveComponent->yVelocity = physComponent->box2dBody->GetLinearVelocity().y;
moveComponent->update();
}
// Update an entity's transform component based on the physics body
void PhysicsWorld::UpdateTransform(Transform* transform, PhysicsComponent* physComponent) {
transform->setPosition(physComponent->box2dBody->GetPosition().x, physComponent->box2dBody->GetPosition().y);
}
void PhysicsWorld::Receive(Event e, void* args)
{
if (e == Event::B2BODY_TO_DELETE)
{
B2BodyDeleteEventArgs* eventArgs = (B2BodyDeleteEventArgs*)args;
B2DBodyDeleteGuardFunction(eventArgs->body, eventArgs->id);
delete eventArgs;
}
else if (e == Event::B2BODY_ADD)
{
EntityID id = *((EntityID*)args);
AddObject(id);
}
}
// delete physics related parts when done
PhysicsWorld::~PhysicsWorld() {
if (gravity) delete gravity;
if (world) delete world;
if (contactListener) delete contactListener;
}