Start of a movement system with collisions against AABBs

This commit is contained in:
2016-01-13 17:53:54 +01:00
parent d6ef527f25
commit 71d936f8d1
10 changed files with 179 additions and 18 deletions
@@ -1,3 +1,6 @@
#ifndef CollidableOctreeSystem_h__
#define CollidableOctreeSystem_h__
#include "../Core/System.h"
#include "../Core/Octree.h"
#include "Collision.h"
@@ -16,4 +19,6 @@ public:
private:
Octree* m_Octree;
};
};
#endif
+6
View File
@@ -45,6 +45,12 @@ bool RayVsModel(const Ray& ray,
float& outUCoord,
float& outVCoord);
bool AABBvsTriangles(const AABB& box,
const std::vector<RawModel::Vertex>& modelVertices,
const std::vector<unsigned int>& modelIndices,
const glm::mat4& modelMatrix,
glm::vec3& outResolutionVector);
//Return true if the boxes are intersecting.
bool AABBVsAABB(const AABB& a, const AABB& b);
//Return true if the boxes are intersecting.
+20
View File
@@ -0,0 +1,20 @@
#ifndef EComponentAttached_h__
#define EComponentAttached_h__
#include "EventBroker.h"
#include "World.h"
#include "Entity.h"
#include "ComponentWrapper.h"
namespace Events
{
struct ComponentAttached : Event
{
EntityWrapper Entity;
ComponentWrapper Component;
};
}
#endif
+18
View File
@@ -4,4 +4,22 @@
typedef unsigned int EntityID;
const static unsigned int EntityID_Invalid = -1;
class World;
struct EntityWrapper
{
EntityWrapper(::World* world, EntityID id)
: World(world)
, ID(id)
{ }
::World* World;
EntityID ID;
bool operator==(const EntityWrapper& e)
{
return (this->World == e.World) && (this->ID == e.ID);
}
operator EntityID() { return this->ID; }
};
#endif
+12
View File
@@ -0,0 +1,12 @@
#include "Common.h"
#include "Core/System.h"
class PlayerMovementSystem : public PureSystem
{
public:
PlayerMovementSystem(EventBroker* eventBroker)
: PureSystem(eventBroker, "Player")
{ }
virtual void UpdateComponent(World* world, ComponentWrapper& player, double dt);
};
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Entity xmlns:c="components" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../Types/Entity.xsd">
<Components>
<c:Transform/>
</Components>
<Children>
<Entity>
<Components>
<c:AABB/>
<c:Collidable>
<Static>false</Static>
</c:Collidable>
<c:Transform>
<Position X="1.5" Y="0" Z="-7"/>
</c:Transform>
</Components>
<Children/>
</Entity>
<Entity>
<Components>
<c:Collidable>
<Static>false</Static>
</c:Collidable>
<c:Model>
<Resource>Models/Core/UnitCube.obj</Resource>
<Color A="1" B="0" G="1" R="0"/>
</c:Model>
<c:Transform>
<Position X="2" Y="0" Z="-7"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity>
+9 -17
View File
@@ -15,7 +15,7 @@
<Color A="1" B="1" G="0" R="0"/>
</c:Model>
<c:Transform>
<Position X="-0.104774237" Y="2.44278431" Z="0"/>
<Position X="-2" Y="0" Z="-7"/>
</c:Transform>
</Components>
<Children/>
@@ -23,38 +23,30 @@
<Entity>
<Components>
<c:AABB/>
<c:Collidable/>
<c:Collidable>
<Static>false</Static>
</c:Collidable>
<c:Model>
<Resource>Models/Core/UnitCube.obj</Resource>
<Color A="1" B="0" G="0" R="1"/>
</c:Model>
<c:Transform>
<Position X="0.548663795" Y="3.91025019" Z="0"/>
<Position X="1.5" Y="0" Z="-7"/>
</c:Transform>
</Components>
<Children/>
</Entity>
<Entity>
<Components>
<c:AABB/>
<c:Collidable/>
<c:Collidable>
<Static>false</Static>
</c:Collidable>
<c:Model>
<Resource>Models/Core/UnitCube.obj</Resource>
<Color A="1" B="0" G="1" R="0"/>
</c:Model>
<c:Transform>
<Position X="-1.35118687" Y="3.90650201" Z="0"/>
</c:Transform>
</Components>
<Children/>
</Entity>
<Entity>
<Components>
<c:Model>
<Resource>Models/Core/UnitCube.obj</Resource>
</c:Model>
<c:Transform>
<Scale X="48.5000038" Y="0.200000003" Z="0"/>
<Position X="2" Y="0" Z="-7"/>
</c:Transform>
</Components>
<Children/>
+40
View File
@@ -199,6 +199,46 @@ bool RayVsModel(const Ray& ray,
return hit;
}
bool AABBvsTriangles(const AABB& box, const std::vector<RawModel::Vertex>& modelVertices, const std::vector<unsigned int>& modelIndices, const glm::mat4& modelMatrix, glm::vec3& outResolutionVector)
{
bool hit = false;
const glm::vec3& origin = box.Origin();
const glm::vec3& min = box.MinCorner();
const glm::vec3& max = box.MaxCorner();
outResolutionVector.x = INFINITY;
for (int i = 0; i < modelIndices.size(); ++i) {
glm::vec3 p = modelVertices[i].Position;
p = glm::vec3(modelMatrix * glm::vec4(p.x, p.y, p.z, 1));
float distFromOrigin = glm::abs(origin.x - p.x);
float penetration = box.HalfSize().x - distFromOrigin;
if (penetration > 0 && penetration < glm::abs(outResolutionVector.x)) {
if (p.x > origin.x) {
outResolutionVector.x = -penetration;
} else {
outResolutionVector.x = penetration;
}
hit = true;
}
//glm::vec3 pLocal = origin - p;
//for (int axis = 0; axis < 3; ++axis) {
// if (p[axis] < min[axis] || p[axis] > max[axis]) {
// continue;
// }
// if (glm::abs(pLocal[axis]) < box.HalfSize()[axis]) {
// outResolutionVector[axis] = (glm::sign(pLocal[axis]) * box.HalfSize()[axis]) - pLocal[axis];
// hit = true;
// }
//}
}
return hit;
}
bool IsSameBoxProbably(const AABB& first, const AABB& second, const float epsilon)
{
const glm::vec3& ma1 = first.MaxCorner();
+23
View File
@@ -17,6 +17,7 @@ void CollisionSystem::UpdateComponent(World* world, ComponentWrapper& cAABB, dou
return;
}
// Collide against octree
std::vector<AABB> octreeResult;
m_Octree->BoxesInSameRegion(*boundingBox, octreeResult);
for (auto& boxB : octreeResult) {
@@ -28,6 +29,28 @@ void CollisionSystem::UpdateComponent(World* world, ComponentWrapper& cAABB, dou
(glm::vec3&)cTransform["Position"] += resolutionVector;
}
}
// HACK: Temporarily collide against all collidable models since they're not in the octree yet
//auto otherCollidables = world->GetComponents("Model");
//for (auto& cModel : *otherCollidables) {
// if (cModel.EntityID == entity) {
// continue;
// }
// if (!world->HasComponent(cModel.EntityID, "Collidable")) {
// continue;
// }
// auto absPosition = RenderQueueFactory::AbsolutePosition(world, cModel.EntityID);
// auto absOrientation = RenderQueueFactory::AbsoluteOrientation(world, cModel.EntityID);
// auto absScale = RenderQueueFactory::AbsoluteScale(world, cModel.EntityID);
// glm::mat4 modelMatrix = glm::translate(absPosition); // *glm::toMat4(absOrientation) * glm::scale(absScale);
// auto model = ResourceManager::Load<Model>(cModel["Resource"]);
// glm::vec3 resolutionVector;
// if (Collision::AABBvsTriangles(boxA, model->m_Vertices, model->m_Indices, modelMatrix, resolutionVector)) {
// (glm::vec3&)cTransform["Position"] += resolutionVector;
// }
//}
}
bool CollisionSystem::OnKeyUp(const Events::KeyUp & event)
+7
View File
@@ -0,0 +1,7 @@
#include "RaptorCopterSystem.h"
void PlayerMovementSystem::UpdateComponent(World* world, ComponentWrapper& player, double dt)
{
}