-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhysicsSystem.cpp
More file actions
95 lines (81 loc) · 2.44 KB
/
PhysicsSystem.cpp
File metadata and controls
95 lines (81 loc) · 2.44 KB
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
#include "PhysicsSystem.h"
#include "PhysicsComponent.h"
#include <iostream>
#include "EngineTime.h"
PhysicsSystem::PhysicsSystem()
{
std::cout << "Loading Physics System. \n";
// Create the physics engine and world
this->physicsCommon = new PhysicsCommon();
PhysicsWorld::WorldSettings settings;
settings.defaultVelocitySolverNbIterations = 50;
settings.gravity = Vector3(0, -9.81, 0);
this->physicsWorld = this->physicsCommon->createPhysicsWorld(settings);
std::cout << "Successfully created physics world. \n";
}
PhysicsSystem::~PhysicsSystem()
{
delete this->physicsCommon;
}
void PhysicsSystem::registerComponent(PhysicsComponent* component)
{
std::cout << "Registering Physics component: " << component->getName() << "\n";
this->componentTable[component->getName()] = component;
this->componentList.push_back(component);
}
void PhysicsSystem::unregisterComponent(PhysicsComponent* component)
{
std::cout << "Unregistering Physics component: " << component->getName() << "\n";
if (this->componentTable[component->getName()] != NULL) {
this->componentTable.erase(component->getName());
int index = -1;
for (int i = 0; i < this->componentList.size(); i++) {
if (this->componentList[i] == component) {
index = i;
break;
}
}
if (index != -1) {
this->componentList.erase(this->componentList.begin() + index);
}
}
else {
std::cout << "Component " << component->getName() << " not registered in physics component. \n";
}
}
void PhysicsSystem::unregisterComponentByName(String name)
{
if (this->componentTable[name] != NULL) {
this->unregisterComponent(this->componentTable[name]);
}
}
PhysicsComponent* PhysicsSystem::findComponentByName(String name)
{
if (this->componentTable[name] == NULL) {
std::cout << "Component " << name << " not registered in physics component. \n";
}
return this->componentTable[name];
}
PhysicsSystem::ComponentList PhysicsSystem::getAllComponents()
{
return this->componentList;
}
void PhysicsSystem::updateAllComponents()
{
//do not update during first frame. Delta time is still 0.
if (EngineTime::getDeltaTime() > 0.0f) {
//update physics world
this->physicsWorld->update(EngineTime::getDeltaTime());
for (int i = 0; i < this->componentList.size(); i++) {
this->componentList[i]->perform(EngineTime::getDeltaTime());
}
}
}
PhysicsWorld* PhysicsSystem::getPhysicsWorld()
{
return this->physicsWorld;
}
PhysicsCommon* PhysicsSystem::getPhysicsCommon()
{
return this->physicsCommon;
}