diff --git a/src/Components/Physics.h b/src/Components/Physics.h index 8fd3f69..2b5065c 100644 --- a/src/Components/Physics.h +++ b/src/Components/Physics.h @@ -11,7 +11,7 @@ struct Physics : Component Physics() : Mass(1.f), Static(false), Phantom(false), CalculateCenterOfMass(true), CenterOfMass(glm::vec3(0)), InitialLinearVelocity(glm::vec3(0)), InitialAngularVelocity(glm::vec3(0)), LinearDamping(0.f), AngularDamping(0.05f), GravityFactor(1.f), Friction(0.5f), Restitution(0.4f), MaxLinearVelocity(200.f), MaxAngularVelocity(200.f), - CollisionLayer(0), CollisionSystemGroup(0), CollisionSubSystemId(0), CollisionSubSystemDontCollideWith(0){} + CollisionLayer(0), CollisionSystemGroup(0), CollisionSubSystemId(0), CollisionSubSystemDontCollideWith(0), CollisionEvent(false){} float Mass; bool Static; @@ -34,6 +34,7 @@ struct Physics : Component int CollisionSubSystemId; int CollisionSubSystemDontCollideWith; + bool CollisionEvent; virtual Physics* Clone() const override { return new Physics(*this); } }; diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 96f094b..342e559 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -101,7 +101,7 @@ void GameWorld::Initialize() auto physics = AddComponent(ground); physics->Mass = 10; physics->Static = true; - physics->CollisionLayer = 0; + physics->CollisionLayer = 1; auto groundshape = CreateEntity(ground); auto transformshape = AddComponent(groundshape); @@ -316,6 +316,7 @@ void GameWorld::Initialize() auto physics = AddComponent(shot); physics->Mass = 25.f; physics->Static = false; + physics->CollisionEvent = true; auto modelComponent = AddComponent(shot); modelComponent->ModelFile = "Models/Placeholders/rocket/Rocket.obj"; auto tankShellComponent = AddComponent(shot); @@ -678,7 +679,7 @@ void GameWorld::Initialize() auto physics = AddComponent(tank); physics->Mass = 63000 - 16000; physics->Static = false; - physics->CollisionLayer = 1; + physics->CollisionLayer = 3; auto vehicle = AddComponent(tank); vehicle->MaxTorque = 36000.f; vehicle->MaxSteeringAngle = 90.f; @@ -739,6 +740,7 @@ void GameWorld::Initialize() auto physics = AddComponent(shot); physics->Mass = 25.f; physics->Static = false; + physics->CollisionEvent = true; auto modelComponent = AddComponent(shot); modelComponent->ModelFile = "Models/Placeholders/rocket/Rocket.obj"; auto tankShellComponent = AddComponent(shot); diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index 31945c8..fad8815 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -54,7 +54,7 @@ private: bool tempSpawnedExplosions; - EventRelay m_EKeyUp; + EventRelay m_EKeyUp; bool OnKeyUp(const Events::KeyUp &e); }; diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 59b2e43..35b78f3 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -110,14 +110,61 @@ void Systems::PhysicsSystem::Initialize() m_PhysicsWorld->unmarkForWrite(); m_collisionResolution = new MyCollisionResolution(this); + + hkpBoxShape* shape = new hkpBoxShape(hkVector4(5, 5, 5), 0); + + PhantomCallbackShape* phantom = new PhantomCallbackShape(this); + hkpBvShape* phantomShape = new hkpBvShape(shape, phantom); + + hkMassProperties massProperties; + hkpInertiaTensorComputer::computeBoxVolumeMassProperties(hkVector4(5, 5, 5),1, massProperties); + + hkpRigidBodyCinfo rigidBodyInfo; + { + rigidBodyInfo.m_shape = shape; + rigidBodyInfo.m_motionType = hkpMotion::MOTION_FIXED; + rigidBodyInfo.m_position = hkVector4(0, 0, 0); + + rigidBodyInfo.m_inertiaTensor = massProperties.m_inertiaTensor; + rigidBodyInfo.m_centerOfMass = massProperties.m_centerOfMass; + rigidBodyInfo.m_mass = massProperties.m_mass; + } + // Create RigidBody + + hkpRigidBody* rigidBody = new hkpRigidBody(rigidBodyInfo); + m_PhysicsWorld->markForWrite(); + m_PhysicsWorld->addEntity(rigidBody); + m_PhysicsWorld->unmarkForWrite(); + + shape->removeReference(); + rigidBody->removeReference(); } + enum + { + GROUND_LAYER = 1, + VEHICLE1_LAYER = 2, + VEHICLE2_LAYER = 3, + EXPLOSION_LAYER = 4, + }; { Events::DisableCollisions e; - e.Layer1 = 1; - e.Layer2 = 2; + e.Layer1 = GROUND_LAYER; + e.Layer2 = EXPLOSION_LAYER; EventBroker->Publish(e); } + /*{ + Events::DisableCollisions e; + e.Layer1 = VEHICLE1_LAYER; + e.Layer2 = EXPLOSION_LAYER; + EventBroker->Publish(e); + } + { + Events::DisableCollisions e; + e.Layer1 = VEHICLE2_LAYER; + e.Layer2 = EXPLOSION_LAYER; + EventBroker->Publish(e); + }*/ } void Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf) @@ -277,7 +324,20 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) hkArray shapeArray; for (auto &shapeData : m_Shapes[entity]) { - shapeArray.pushBack(shapeData.Shape); + auto childTransformComponent = m_World->GetComponent(shapeData.Entity); + hkpShape* shape; + + if(shapeData.ConvexShape != nullptr) + { + hkQsTransform transform( GLMVEC3_TO_HKVECTOR4(childTransformComponent->Position), GLMQUAT_TO_HKQUATERNION(childTransformComponent->Orientation), GLMVEC3_TO_HKVECTOR4(childTransformComponent->Scale)); + hkpConvexTransformShape* transformedBoxShape = new hkpConvexTransformShape( shapeData.ConvexShape, transform ); + shapeArray.pushBack(transformedBoxShape); + } + + if(shapeData.Shape != nullptr) + { + shapeArray.pushBack(shapeData.Shape); + } } @@ -285,15 +345,26 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) hkpListShape* listShape = new hkpListShape(shapeArray.begin(), shapeArray.getSize(), hkpShapeContainer::REFERENCE_POLICY_INCREMENT); // Save the listShape for further use m_ListShapes[entity] = listShape; - //shape = listShape; - hkpBoxShape* box = new hkpBoxShape(listShape->m_aabbHalfExtents, 0.0f); - shape = new hkpBvShape(listShape, box); + hkMassProperties massProperties; + if(physicsComponent->Phantom) + { + PhantomCallbackShape* phantom = new PhantomCallbackShape(this); + shape = new hkpBvShape(listShape, phantom); + hkpInertiaTensorComputer::computeSphereVolumeMassProperties(1, physicsComponent->Mass, massProperties); + } + else + { + hkpBoxShape* box = new hkpBoxShape(listShape->m_aabbHalfExtents, 0.0f); + shape = new hkpBvShape(listShape, box); + hkpInertiaTensorComputer::computeShapeVolumeMassProperties(shape, physicsComponent->Mass, massProperties); + } + // Clean up for less memory usage m_Shapes.erase(entity); - hkMassProperties massProperties; - hkpInertiaTensorComputer::computeShapeVolumeMassProperties(shape, physicsComponent->Mass, massProperties); + + hkpRigidBodyCinfo rigidBodyInfo; { @@ -344,7 +415,10 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) m_PhysicsWorld->markForWrite(); vehicleSetup.buildVehicle(m_World, m_PhysicsWorld, *m_Vehicles[entity], entity, m_Wheels); // Add the vehicle's entities and phantoms to the world - rigidBody->addContactListener( m_collisionResolution ); + if(physicsComponent->CollisionEvent) + { + rigidBody->addContactListener( m_collisionResolution ); + } m_Vehicles[entity]->addToWorld(m_PhysicsWorld); m_RigidBodies[entity] = rigidBody; m_RigidBodyEntities[rigidBody] = entity; @@ -362,7 +436,10 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) else { m_PhysicsWorld->markForWrite(); - rigidBody->addContactListener( m_collisionResolution ); + if(physicsComponent->CollisionEvent) + { + rigidBody->addContactListener( m_collisionResolution ); + } m_PhysicsWorld->addEntity(rigidBody); m_RigidBodies[entity] = rigidBody; m_RigidBodyEntities[rigidBody] = entity; @@ -388,15 +465,35 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) hkVector4 scale = GLMVEC3_TO_HKVECTOR4(childTransformComponent->Scale); hkQsTransform transform(position, rotation, scale); - staticCompoundShape->addInstance(shapeData.Shape, transform); + if(shapeData.ConvexShape != nullptr) + { + staticCompoundShape->addInstance(shapeData.ConvexShape, transform); + } + + if(shapeData.Shape != nullptr) + { + staticCompoundShape->addInstance(shapeData.Shape, transform); + } } // This must be called after adding the instances and before using the shape. staticCompoundShape->bake(); shape = staticCompoundShape; - m_Shapes.erase(entity); hkMassProperties massProperties; - hkpInertiaTensorComputer::computeShapeVolumeMassProperties(shape, physicsComponent->Mass, massProperties); + if(physicsComponent->Phantom) + { + PhantomCallbackShape* phantom = new PhantomCallbackShape(this); + shape = new hkpBvShape(shape, phantom); + hkpInertiaTensorComputer::computeSphereVolumeMassProperties(1, physicsComponent->Mass, massProperties); + } + else + { + hkpInertiaTensorComputer::computeShapeVolumeMassProperties(shape, physicsComponent->Mass, massProperties); + } + + m_Shapes.erase(entity); + + hkpRigidBodyCinfo rigidBodyInfo; { @@ -424,7 +521,6 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) rigidBodyInfo.m_maxLinearVelocity = physicsComponent->MaxLinearVelocity; rigidBodyInfo.m_maxAngularVelocity = physicsComponent->MaxAngularVelocity; rigidBodyInfo.m_collisionFilterInfo = hkpGroupFilter::calcFilterInfo(physicsComponent->CollisionLayer, physicsComponent->CollisionSystemGroup, physicsComponent->CollisionSubSystemId, physicsComponent->CollisionSubSystemDontCollideWith); - } // Create RigidBody hkpRigidBody* rigidBody = new hkpRigidBody(rigidBodyInfo); @@ -447,24 +543,17 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) if(sphereComponent) { hkpSphereShape* sphereShape = new hkpSphereShape(sphereComponent->Radius); - - hkQsTransform transform( GLMVEC3_TO_HKVECTOR4(transformComponent->Position), GLMQUAT_TO_HKQUATERNION(transformComponent->Orientation), GLMVEC3_TO_HKVECTOR4(transformComponent->Scale)); - hkpConvexTransformShape* transformedSphereShape = new hkpConvexTransformShape( sphereShape, transform ); - - m_Shapes[entityParent].push_back(ShapeArrayData(entity, transformedSphereShape)); + m_Shapes[entityParent].push_back(ShapeArrayData(entity, sphereShape, nullptr)); - sphereShape->removeReference(); + //sphereShape->removeReference(); } //TODO: COMMENT THIS SECTION else if(boxComponent) { hkReal thickness = 0.05; hkpBoxShape* boxShape = new hkpBoxShape(hkVector4(boxComponent->Width- thickness, boxComponent->Height -thickness, boxComponent->Depth - thickness), thickness); - - hkQsTransform transform( GLMVEC3_TO_HKVECTOR4(transformComponent->Position), GLMQUAT_TO_HKQUATERNION(transformComponent->Orientation), GLMVEC3_TO_HKVECTOR4(transformComponent->Scale)); - hkpConvexTransformShape* transformedBoxShape = new hkpConvexTransformShape( boxShape, transform ); - m_Shapes[entityParent].push_back(ShapeArrayData(entity, transformedBoxShape)); - boxShape->removeReference(); + m_Shapes[entityParent].push_back(ShapeArrayData(entity, boxShape, nullptr)); + //boxShape->removeReference(); } else if(meshShapeComponent) { @@ -513,7 +602,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) m_ExtendedMeshShapes[entity].Code = code; m_ExtendedMeshShapes[entity].MoppShape = moppShape; - m_Shapes[entityParent].push_back(ShapeArrayData(entity, moppShape)); //HACK: Should maybe have transform, not sure yet + m_Shapes[entityParent].push_back(ShapeArrayData(entity, nullptr, moppShape)); } } diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index d58be0f..ed6bde4 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -76,6 +76,8 @@ #include +#include + #include "Components/TankShell.h" #include #include "Events/EnableCollisions.h" @@ -101,7 +103,7 @@ public: Events::Collision e; e.Entity1 = entity1; e.Entity2 = entity2; - //m_PhysicsSystem->EventBroker->Publish(e); + m_PhysicsSystem->EventBroker->Publish(e); } private: @@ -118,20 +120,14 @@ public: virtual void phantomEnterEvent( const hkpCollidable* collidableA, const hkpCollidable* collidableB, const hkpCollisionInput& env ) { - Events::ApplyPointImpulse e; - e.Entity = m_PhysicsSystem->m_RigidBodyEntities[hkpGetRigidBody(collidableB)]; - e.Position = HKVECTOR4_TO_GLMVEC3(hkpGetRigidBody(collidableB)->getPosition()); - - glm::vec3 impulse; - impulse = HKVECTOR4_TO_GLMVEC3(hkpGetRigidBody(collidableA)->getPosition()) - HKVECTOR4_TO_GLMVEC3(hkpGetRigidBody(collidableB)->getPosition()); - e.Impulse = impulse; - - m_PhysicsSystem->EventBroker->Publish(e); + + LOG_INFO("AAAAAAAAAAAAAAAAAAAAAAAAAH!"); } virtual void phantomLeaveEvent( const hkpCollidable* collidableA, const hkpCollidable* collidableB ) { - + + LOG_INFO("AAAAAAAAAAAAAAAAAAAAAAAAAH!"); } private: @@ -167,9 +163,9 @@ private: EventRelay m_EApplyPointImpulse; bool OnApplyPointImpulse(const Events::ApplyPointImpulse &event); - EventRelay m_EEnableCollisions; + EventRelay m_EEnableCollisions; bool OnEnableCollisions(const Events::EnableCollisions &e); - EventRelay m_EDisableCollisions; + EventRelay m_EDisableCollisions; bool OnDisableCollisions(const Events::DisableCollisions &e); @@ -198,12 +194,14 @@ private: struct ShapeArrayData { - ShapeArrayData(EntityID entity, hkpShape* shape) + ShapeArrayData(EntityID entity, hkpConvexShape* convexShape, hkpShape* shape) { Entity = entity; + ConvexShape = convexShape; Shape = shape; } EntityID Entity; + hkpConvexShape* ConvexShape; hkpShape* Shape; }; std::unordered_map> m_Shapes; diff --git a/src/Systems/TankSteeringSystem.cpp b/src/Systems/TankSteeringSystem.cpp index 7fca5c2..281e03a 100644 --- a/src/Systems/TankSteeringSystem.cpp +++ b/src/Systems/TankSteeringSystem.cpp @@ -120,8 +120,9 @@ bool Systems::TankSteeringSystem::OnCollision( const Events::Collision &e ) transformHit->Position = transform->Position; auto physics = m_World->AddComponent(hitSphere); - physics->Static = false; - physics->Phantom = true; + physics->Static = true; + physics->Phantom = false; + physics->CollisionLayer = 4; { auto shape = m_World->CreateEntity(hitSphere); @@ -132,7 +133,7 @@ bool Systems::TankSteeringSystem::OnCollision( const Events::Collision &e ) } m_World->CommitEntity(hitSphere); } - + m_World->RemoveEntity(e.Entity1); } @@ -153,8 +154,9 @@ bool Systems::TankSteeringSystem::OnCollision( const Events::Collision &e ) transformHit->Position = transform->Position; auto physics = m_World->AddComponent(hitSphere); - physics->Static = false; - physics->Phantom = true; + physics->Static = true; + physics->Phantom = false; + physics->CollisionLayer = 4; { auto shape = m_World->CreateEntity(hitSphere); diff --git a/src/Systems/TankSteeringSystem.h b/src/Systems/TankSteeringSystem.h index f348b02..1b38fe1 100644 --- a/src/Systems/TankSteeringSystem.h +++ b/src/Systems/TankSteeringSystem.h @@ -40,7 +40,7 @@ namespace Systems void UpdateEntity(double dt, EntityID entity, EntityID parent) override; private: - EventRelay m_ECollision; + EventRelay m_ECollision; bool OnCollision(const Events::Collision &e); class TankSteeringInputController;