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
+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;
}