moar collisionz

This commit is contained in:
Adam
2014-03-13 00:29:47 +01:00
parent 01743867f0
commit 2beaa22758
4 changed files with 85 additions and 30 deletions
+33 -13
View File
@@ -7,7 +7,7 @@ void GameWorld::Initialize()
//AddSystem("LevelGenerationSystem");
AddSystem("InputSystem");
//AddSystem("CollisionSystem");
AddSystem("CollisionSystem");
//AddSystem("ParticleSystem");
AddSystem("PlayerSystem");
AddSystem("SoundSystem");
@@ -17,19 +17,26 @@ void GameWorld::Initialize()
std::shared_ptr<Components::Model> model;
std::shared_ptr<Components::PointLight> pointLight;
std::shared_ptr<Components::Camera> camera;
std::shared_ptr<Components::Bounds> bounds;
std::shared_ptr<Components::Collision> collision;
EntityID ent;
// Camera
ent = CreateEntity();
SetProperty(ent, "Name", std::string("Camera"));
transform = AddComponent<Components::Transform>(ent, "Transform");
AddComponent<Components::Input>(ent, "Input");
entcamera = CreateEntity();
SetProperty(entcamera, "Name", std::string("Camera"));
transform = AddComponent<Components::Transform>(entcamera, "Transform");
AddComponent<Components::Input>(entcamera, "Input");
transform->Position = glm::vec3(0.f, 2.f, 10.f);
camera = AddComponent<Components::Camera>(ent, "Camera");
camera = AddComponent<Components::Camera>(entcamera, "Camera");
camera->FOV = 45.f;
camera->FarClip = 1000.f;
camera->NearClip = 0.01f;
transform->Orientation = glm::angleAxis<float>(glm::radians(25.0f),glm::vec3(1,0,0));
collision = AddComponent<Components::Collision>(entcamera, "Collision");
collision->Phantom = false;
bounds = AddComponent<Components::Bounds>(entcamera, "Bounds");
bounds->Origin = transform->Position;
bounds->VolumeVector = glm::vec3(4.f,2.f,2.f);
// Fucking lights
ent = CreateEntity();
@@ -84,22 +91,35 @@ void GameWorld::Initialize()
model->ModelFile = "plane.obj";
// Player
ent = CreateEntity();
transform = AddComponent<Components::Transform>(ent, "Transform");
player1 = CreateEntity();
transform = AddComponent<Components::Transform>(player1, "Transform");
transform->Position = glm::vec3(0.f, 0.f, 0.f);
model = AddComponent<Components::Model>(ent, "Model");
model = AddComponent<Components::Model>(player1, "Model");
model->ModelFile = "ship.obj";
collision = AddComponent<Components::Collision>(player1, "Collision");
collision->Phantom = false;
bounds = AddComponent<Components::Bounds>(player1, "Bounds");
bounds->Origin = transform->Position;
bounds->VolumeVector = glm::vec3(10.f,10.f,5.f);
ent = CreateEntity();
transform = AddComponent<Components::Transform>(ent, "Transform");
player2 = CreateEntity();
transform = AddComponent<Components::Transform>(player2, "Transform");
transform->Position = glm::vec3(10.f, 0.f, 0.f);
model = AddComponent<Components::Model>(ent, "Model");
model = AddComponent<Components::Model>(player2, "Model");
model->ModelFile = "ship.obj";
collision = AddComponent<Components::Collision>(player2, "Collision");
collision->Phantom = false;
bounds = AddComponent<Components::Bounds>(player2, "Bounds");
bounds->Origin = transform->Position;
bounds->VolumeVector = glm::vec3(2.f,2.f,2.f);
}
void GameWorld::Update(double dt)
{
World::Update(dt);
if(GetSystem<Systems::CollisionSystem>("CollisionSystem")->Intersects(player1, entcamera))
assert(false);
}
void GameWorld::RegisterComponents()
@@ -124,7 +144,7 @@ void GameWorld::RegisterSystems()
{
//m_SystemFactory.Register("LevelGenerationSystem", [this]() { return new Systems::LevelGenerationSystem(this); });
m_SystemFactory.Register("InputSystem", [this]() { return new Systems::InputSystem(this, m_Renderer); });
//m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); });
m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); });
//m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this); });
m_SystemFactory.Register("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); });
m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this); });
+1
View File
@@ -42,6 +42,7 @@ public:
private:
std::shared_ptr<Renderer> m_Renderer;
EntityID entcamera, player1, player2;
};
#endif // GameWorld_h__
+50 -17
View File
@@ -17,28 +17,65 @@ void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID
bool Systems::CollisionSystem::Intersects(EntityID entity1, EntityID entity2)
{
auto collisionComponent1 = m_World->GetComponent<Components::Collision>(entity1, "Collision");
if(collisionComponent1 == nullptr)
return false;
auto collisionComponent2 = m_World->GetComponent<Components::Collision>(entity2, "Collision");
if(collisionComponent2 == nullptr)
return false;
collisionComponent1->CollidingEntities.clear();
collisionComponent2->CollidingEntities.clear();
auto bounds1 = m_World->GetComponent<Components::Bounds>(entity1, "Bounds");
auto bounds2 = m_World->GetComponent<Components::Bounds>(entity2, "Bounds");
float width = bounds1->VolumeVector.x;
float height = bounds1->VolumeVector.y;
float depth = bounds1->VolumeVector.z;
glm::vec3 max1 = glm::vec3(bounds1->Origin.x + bounds1->VolumeVector.x, bounds1->Origin.y + bounds1->VolumeVector.y, bounds1->Origin.z + bounds1->VolumeVector.z);
glm::vec3 min1 = glm::vec3(bounds1->Origin.x - bounds1->VolumeVector.x, bounds1->Origin.y - bounds1->VolumeVector.y, bounds1->Origin.z - bounds1->VolumeVector.z);
glm::vec3 max2 = glm::vec3(bounds2->Origin.x + bounds2->VolumeVector.x, bounds2->Origin.y + bounds2->VolumeVector.y, bounds2->Origin.z + bounds2->VolumeVector.z);
glm::vec3 min2 = glm::vec3(bounds2->Origin.x - bounds2->VolumeVector.x, bounds2->Origin.y - bounds2->VolumeVector.y, bounds2->Origin.z - bounds2->VolumeVector.z);
GLfloat vertices1[] = {
bounds1->Origin.x - width / 2, bounds1->Origin.y - height / 2, bounds1->Origin.z - depth / 2, 1.0,
bounds1->Origin.x + width / 2, bounds1->Origin.y - height / 2, bounds1->Origin.z - depth / 2, 1.0,
bounds1->Origin.x + width / 2, bounds1->Origin.y + height / 2, bounds1->Origin.z - depth / 2, 1.0,
bounds1->Origin.x - width / 2, bounds1->Origin.y + height / 2, bounds1->Origin.z - depth / 2, 1.0,
bounds1->Origin.x - width / 2, bounds1->Origin.y - height / 2, bounds1->Origin.z + depth / 2, 1.0,
bounds1->Origin.x + width / 2, bounds1->Origin.y - height / 2, bounds1->Origin.z + depth / 2, 1.0,
bounds1->Origin.x + width / 2, bounds1->Origin.y + height / 2, bounds1->Origin.z + depth / 2, 1.0,
bounds1->Origin.x - width / 2, bounds1->Origin.y + height / 2, bounds1->Origin.z + depth / 2, 1.0,
for(int z = min1.z; z < max1.z; z++)
if (z > min2.z && z < max2.z)
for (int y = min1.y; y < max1.y; y++)
if(y > min2.y && y < max2.y)
for (int x = min1.x; x < max1.x; x++)
if(x > min2.x && x < max2.x)
{
//vi har en kollision!!! kanske :(
collisionComponent1->CollidingEntities.push_back(entity2);
collisionComponent2->CollidingEntities.push_back(entity1);
return true;
}
return false;
}
void Systems::CollisionSystem::CreateBoundingBox(std::shared_ptr<Components::Bounds> bounds)
{
float width = abs(bounds->VolumeVector.x);
float height = abs(bounds->VolumeVector.y);
float depth = abs(bounds->VolumeVector.z);
GLfloat vertices[] = {
bounds->Origin.x - width, bounds->Origin.y - height, bounds->Origin.z - depth, 1.0,
bounds->Origin.x + width, bounds->Origin.y - height, bounds->Origin.z - depth, 1.0,
bounds->Origin.x + width, bounds->Origin.y + height, bounds->Origin.z - depth, 1.0,
bounds->Origin.x - width, bounds->Origin.y + height, bounds->Origin.z - depth, 1.0,
bounds->Origin.x - width, bounds->Origin.y - height, bounds->Origin.z + depth, 1.0,
bounds->Origin.x + width, bounds->Origin.y - height, bounds->Origin.z + depth, 1.0,
bounds->Origin.x + width, bounds->Origin.y + height, bounds->Origin.z + depth, 1.0,
bounds->Origin.x - width, bounds->Origin.y + height, bounds->Origin.z + depth, 1.0,
};
GLuint vbo_vertices;
glGenBuffers(1, &vbo_vertices);
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices1), vertices1, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
GLushort elements[] = {
@@ -52,8 +89,4 @@ bool Systems::CollisionSystem::Intersects(EntityID entity1, EntityID entity2)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_elements);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
return true;
}
@@ -20,6 +20,7 @@ public:
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
bool Intersects(EntityID, EntityID);
void CreateBoundingBox(std::shared_ptr<Components::Bounds>);
};
}