diff --git a/src/Events/Collision.h b/src/Events/Collision.h new file mode 100644 index 0000000..b1e55b3 --- /dev/null +++ b/src/Events/Collision.h @@ -0,0 +1,18 @@ +#ifndef Events_Collision_h__ +#define Events_Collision_h__ + +#include "Entity.h" +#include "EventBroker.h" + +namespace Events +{ + +struct Collision : Event +{ + EntityID Entity1; + EntityID Entity2; +}; + +} + +#endif // Events_Collision_h__ \ No newline at end of file diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 084e945..e88afbd 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -335,7 +335,7 @@ void GameWorld::Initialize() auto cameraTower = CreateEntity(tower); { auto transform = AddComponent(cameraTower); - transform->Position.z = 11.f; + transform->Position.z = 14.f; transform->Position.y = 4.f; //transform->Orientation = glm::quat(glm::vec3(glm::pi() / 8.f, 0.f, 0.f)); auto cameraComp = AddComponent(cameraTower); @@ -754,7 +754,7 @@ void GameWorld::Initialize() auto cameraTower = CreateEntity(tower); { auto transform = AddComponent(cameraTower); - transform->Position.z = 11.f; + transform->Position.z = 14.f; transform->Position.y = 4.f; //transform->Orientation = glm::quat(glm::vec3(glm::pi() / 8.f, 0.f, 0.f)); auto cameraComp = AddComponent(cameraTower); diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 19c00f0..c2d0346 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -357,18 +357,7 @@ void Renderer::AddTextureToDraw(Texture* texture, glm::vec3 position, glm::quat { glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale); - glm::vec3 camToParticle = glm::normalize(m_Camera->Position() - position); - glm::vec3 up = glm::vec3(0,1,0); - glm::vec3 rightVec = glm::normalize(glm::cross(up, camToParticle)); - glm::vec3 up2 = glm::normalize(glm::cross(camToParticle, rightVec)); - - glm::mat4 billboardMatrix; - billboardMatrix[0] = glm::vec4(rightVec, 0); - billboardMatrix[1] = glm::vec4(up2, 0); - billboardMatrix[2] = glm::vec4(camToParticle, 0); - //billboardMatrix[3] = glm::vec4(position, 0); - - TexturesToRender.push_back(std::make_tuple(texture, modelMatrix, billboardMatrix)); + TexturesToRender.push_back(std::make_tuple(texture, modelMatrix, position)); } void Renderer::AddPointLightToDraw( @@ -770,10 +759,22 @@ void Renderer::DrawFBOScene(Viewport &viewport) { Texture* texture; glm::mat4 modelMatrix; - glm::mat4 billboardMatrix; - std::tie(texture, modelMatrix, billboardMatrix) = tuple; + glm::vec3 position; + std::tie(texture, modelMatrix, position) = tuple; //MVP = cameraMatrix * glm::inverse(glm::toMat4(m_Camera->Orientation()) * modelMatrix ); + + glm::vec3 camToParticle = glm::normalize(viewport.Camera->Position() - position); + glm::vec3 up = glm::vec3(0,1,0); + glm::vec3 rightVec = glm::normalize(glm::cross(up, camToParticle)); + glm::vec3 up2 = glm::normalize(glm::cross(camToParticle, rightVec)); + + glm::mat4 billboardMatrix; + billboardMatrix[0] = glm::vec4(rightVec, 0); + billboardMatrix[1] = glm::vec4(up2, 0); + billboardMatrix[2] = glm::vec4(camToParticle, 0); + //billboardMatrix[3] = glm::vec4(position, 0); + MVP = cameraMatrix * modelMatrix * billboardMatrix; depthMVP = depthCameraMatrix * modelMatrix; diff --git a/src/Renderer.h b/src/Renderer.h index 3bfa5ce..b2c2a91 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -26,7 +26,7 @@ public: int Height() const { return m_Height; } std::list> ModelsToRender; - std::list> TexturesToRender; + std::list> TexturesToRender; std::list> AABBsToRender; Renderer(); diff --git a/src/Shaders/Fragment.glsl b/src/Shaders/Fragment.glsl index 87a95fa..47af3a6 100755 --- a/src/Shaders/Fragment.glsl +++ b/src/Shaders/Fragment.glsl @@ -40,7 +40,7 @@ void main() { // Diffuse Texture - frag_Diffuse = texture(DiffuseTexture, Input.TextureCoord) * Shadow(Input.ShadowCoord); + frag_Diffuse = texture(DiffuseTexture, Input.TextureCoord); // * Shadow(Input.ShadowCoord); // G-buffer Position frag_Position = vec4(Input.Position.xyz, 1.0); diff --git a/src/System.h b/src/System.h index b40bdef..f795d5e 100755 --- a/src/System.h +++ b/src/System.h @@ -33,6 +33,7 @@ public: virtual void OnComponentRemoved(std::string type, Component* component) { } // Called when components are committed to an entity virtual void OnEntityCommit(EntityID entity) { } + virtual void OnEntityRemoved(EntityID entity) { } protected: World* m_World; diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 3c3c1f6..290e24d 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -78,7 +78,7 @@ void Systems::PhysicsSystem::Initialize() worldInfo.m_broadPhaseBorderBehaviour = hkpWorldCinfo::BROADPHASE_BORDER_DO_NOTHING; // You must specify the size of the broad phase - objects should not be simulated outside this region - worldInfo.setBroadPhaseWorldSize(1000.0f); + worldInfo.setBroadPhaseWorldSize(1500.0f); m_PhysicsWorld = new hkpWorld(worldInfo); // When the simulation type is SIMULATION_TYPE_MULTITHREADED, in the debug build, the sdk performs checks @@ -107,7 +107,7 @@ void Systems::PhysicsSystem::Initialize() m_PhysicsWorld->unmarkForWrite(); - m_collisionResolution = new MyCollisionResolution; + m_collisionResolution = new MyCollisionResolution(this); } } @@ -326,7 +326,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) rigidBody->addContactListener( m_collisionResolution ); m_Vehicles[entity]->addToWorld(m_PhysicsWorld); m_RigidBodies[entity] = rigidBody; - m_collisionResolution->m_RigidBodies[rigidBody] = entity; + m_RigidBodyEntities[rigidBody] = entity; // The vehicle is an action @@ -344,7 +344,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) rigidBody->addContactListener( m_collisionResolution ); m_PhysicsWorld->addEntity(rigidBody); m_RigidBodies[entity] = rigidBody; - m_collisionResolution->m_RigidBodies[rigidBody] = entity; + m_RigidBodyEntities[rigidBody] = entity; m_PhysicsWorld->unmarkForWrite(); shape->removeReference(); @@ -397,7 +397,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) m_PhysicsWorld->markForWrite(); m_PhysicsWorld->addEntity(rigidBody); m_RigidBodies[entity] = rigidBody; - m_collisionResolution->m_RigidBodies[rigidBody] = entity; + m_RigidBodyEntities[rigidBody] = entity; m_PhysicsWorld->unmarkForWrite(); shape->removeReference(); @@ -577,3 +577,17 @@ bool Systems::PhysicsSystem::OnApplyPointImpulse( const Events::ApplyPointImpuls m_PhysicsWorld->unmarkForWrite(); return true; } + +void Systems::PhysicsSystem::OnEntityRemoved( EntityID entity ) +{ + // TODO: + /*auto rigidBodyIt = m_RigidBodies.find(entity); + if (rigidBodyIt == m_RigidBodies.end()) + return; + + auto rigidBody = rigidBodyIt->second; + m_RigidBodies.erase(entity); + m_RigidBodyEntities.erase(rigidBody); + rigidBody->removeReference();*/ + +} diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index 54e61f7..f687ab7 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -27,6 +27,7 @@ #include "Events/SetVelocity.h" #include "Events/ApplyForce.h" #include "Events/ApplyPointImpulse.h" +#include "Events/Collision.h" #include "OBJ.h" // Math and base include @@ -75,27 +76,45 @@ #include -class MyCollisionResolution: public hkReferencedObject, public hkpContactListener -{ -public: - std::unordered_map m_RigidBodies; - - virtual void contactPointCallback( const hkpContactPointEvent& event ) - { - - EntityID entity1 = m_RigidBodies[event.getBody(0)]; - EntityID entity2 = m_RigidBodies[event.getBody(1)]; - //LOG_INFO("Entities colliding: %i, %i ", entity1, entity2); - - } -}; - - +#include "Components/Model.h" namespace Systems { class PhysicsSystem : public System { public: + class MyCollisionResolution: public hkReferencedObject, public hkpContactListener + { + public: + + MyCollisionResolution(Systems::PhysicsSystem* physicsSystem) + : m_PhysicsSystem(physicsSystem) { } + + virtual void contactPointCallback( const hkpContactPointEvent& event ) + { + EntityID entity1 = m_PhysicsSystem->m_RigidBodyEntities[event.getBody(0)]; + EntityID entity2 = m_PhysicsSystem->m_RigidBodyEntities[event.getBody(1)]; + + Events::Collision e; + e.Entity1 = entity1; + e.Entity2 = entity2; + m_PhysicsSystem->EventBroker->Publish(e); + + auto modelComponent = m_PhysicsSystem->m_World->GetComponent(entity1); + + if (modelComponent && modelComponent->ModelFile == "Models/Placeholders/rocket/Rocket.obj" && entity2 == 6) + { + m_PhysicsSystem->m_PhysicsWorld->markForWrite(); + m_PhysicsSystem->m_RigidBodies[entity1]->setPosition(hkVector4(2000, -2000, 2000)); + m_PhysicsSystem->m_PhysicsWorld->unmarkForWrite(); + } + } + + private: + Systems::PhysicsSystem* m_PhysicsSystem; + }; + + friend class MyCollisionResolution; + PhysicsSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) : System(world, eventBroker) { } @@ -107,7 +126,8 @@ public: void OnComponentCreated(std::string type, std::shared_ptr component) override; void OnComponentRemoved(std::string type, Component* component) override; void OnEntityCommit(EntityID entity) override; - + void OnEntityRemoved(EntityID entity) override; + private: double m_Accumulator; hkpWorld* m_PhysicsWorld; @@ -132,6 +152,7 @@ private: void SetupPhysics(hkpWorld* physicsWorld); std::unordered_map m_RigidBodies; + std::unordered_map m_RigidBodyEntities; hkJobThreadPool* m_ThreadPool; hkJobQueue* m_JobQueue; diff --git a/src/Systems/TankSteeringSystem.cpp b/src/Systems/TankSteeringSystem.cpp index 3d36977..5502b35 100644 --- a/src/Systems/TankSteeringSystem.cpp +++ b/src/Systems/TankSteeringSystem.cpp @@ -11,6 +11,8 @@ void Systems::TankSteeringSystem::RegisterComponents( ComponentFactory* cf ) void Systems::TankSteeringSystem::Initialize() { + EVENT_SUBSCRIBE_MEMBER(m_ECollision, &Systems::TankSteeringSystem::OnCollision); + for (int i = 0; i < 4; i++) { m_TankInputControllers[i] = std::shared_ptr(new TankSteeringInputController(EventBroker, i + 1)); @@ -93,6 +95,32 @@ void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit } } +bool Systems::TankSteeringSystem::OnCollision( const Events::Collision &e ) +{ + // TODO: + auto modelComponent = m_World->GetComponent(e.Entity1); + + if (modelComponent && modelComponent->ModelFile == "Models/Placeholders/rocket/Rocket.obj") + { + auto transform = m_World->GetComponent(e.Entity1); + m_World->GetSystem()->CreateExplosion( + transform->Position, + 1, + 60, + "Textures/Sprites/NewtonTreeDeleteASAPPlease.png", + glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)), + 40, + glm::pi(), + 0.5f + ); + //m_World->RemoveEntity(e.Entity1); + } + + //LOG_INFO("Ent1 %i Ent2 %i", e.Entity1, e.Entity2); + + return true; +} + void Systems::TankSteeringSystem::TankSteeringInputController::Update( double dt ) { PositionX = m_Horizontal; diff --git a/src/Systems/TankSteeringSystem.h b/src/Systems/TankSteeringSystem.h index 370ba92..5a64617 100644 --- a/src/Systems/TankSteeringSystem.h +++ b/src/Systems/TankSteeringSystem.h @@ -5,6 +5,7 @@ #include "Events/SetVelocity.h" #include "Events/ApplyForce.h" #include "Events/ApplyPointImpulse.h" +#include "Events/Collision.h" #include "Components/Transform.h" #include "Components/TankSteering.h" #include "Components/TowerSteering.h" @@ -12,7 +13,9 @@ #include "Components/Physics.h" #include "Components/Vehicle.h" #include "Components/Player.h" +#include "Components/Model.h" #include "Systems/TransformSystem.h" +#include "Systems/ParticleSystem.h" #include "InputController.h" namespace Systems @@ -31,6 +34,9 @@ namespace Systems void UpdateEntity(double dt, EntityID entity, EntityID parent) override; private: + EventRelay m_ECollision; + bool OnCollision(const Events::Collision &e); + class TankSteeringInputController; std::array, 4> m_TankInputControllers; diff --git a/src/World.cpp b/src/World.cpp index 2a25856..d9d36be 100755 --- a/src/World.cpp +++ b/src/World.cpp @@ -83,6 +83,12 @@ void World::RemoveEntity(EntityID entity) m_EntitiesToRemove.push_back(pair.first); } } + // Trigger events + for (auto pair : m_Systems) + { + auto system = pair.second; + system->OnEntityRemoved(entity); + } } void World::ProcessEntityRemovals() diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 6a99e47..9270387 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -163,6 +163,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 2bf19bf..4463763 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -397,6 +397,9 @@ Gameplay\Components + + Physics\Events +