Collisionfilters
This commit is contained in:
@@ -36,7 +36,9 @@ void Systems::PhysicsSystem::Initialize()
|
||||
EVENT_SUBSCRIBE_MEMBER(m_ESetVelocity, &Systems::PhysicsSystem::OnSetVelocity);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EApplyForce, &Systems::PhysicsSystem::OnApplyForce);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EApplyPointImpulse, &Systems::PhysicsSystem::OnApplyPointImpulse);
|
||||
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EEnableCollisions, &Systems::PhysicsSystem::OnEnableCollisions);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EDisableCollisions, &Systems::PhysicsSystem::OnDisableCollisions);
|
||||
|
||||
hkMemorySystem::FrameInfo finfo(6000 * 1024); // Allocate 6MB of Physics solver buffer
|
||||
hkMemoryRouter* memoryRouter = hkMemoryInitUtil::initDefault(hkMallocAllocator::m_defaultMallocAllocator, finfo);
|
||||
hkBaseSystem::init(memoryRouter, HavokErrorReport);
|
||||
@@ -102,14 +104,20 @@ void Systems::PhysicsSystem::Initialize()
|
||||
m_Context = new hkpPhysicsContext;
|
||||
hkpPhysicsContext::registerAllPhysicsProcesses(); // all the physics viewers
|
||||
m_Context->addWorld(m_PhysicsWorld); // add the physics world so the viewers can see it
|
||||
|
||||
SetupVisualDebugger(m_Context);
|
||||
|
||||
m_CollisionFilter = new hkpGroupFilter();
|
||||
m_PhysicsWorld->setCollisionFilter( m_CollisionFilter );
|
||||
m_PhysicsWorld->unmarkForWrite();
|
||||
|
||||
m_collisionResolution = new MyCollisionResolution(this);
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
Events::DisableCollisions e;
|
||||
e.Layer1 = 1;
|
||||
e.Layer2 = 2;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
}
|
||||
|
||||
void Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf)
|
||||
@@ -298,8 +306,21 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
|
||||
rigidBodyInfo.m_rotation.set(rotation(0), rotation(1), rotation(2), rotation(3));
|
||||
|
||||
rigidBodyInfo.m_inertiaTensor = massProperties.m_inertiaTensor;
|
||||
//rigidBodyInfo.m_centerOfMass = massProperties.m_centerOfMass; //HACK: CENTER OF MASS ALWAYS IN THE CENTER
|
||||
if(physicsComponent->CalculateCenterOfMass)
|
||||
physicsComponent->CenterOfMass = HKVECTOR4_TO_GLMVEC3(massProperties.m_centerOfMass);
|
||||
rigidBodyInfo.m_centerOfMass = GLMVEC3_TO_HKVECTOR4(physicsComponent->CenterOfMass);
|
||||
rigidBodyInfo.m_mass = massProperties.m_mass;
|
||||
rigidBodyInfo.m_linearVelocity = GLMVEC3_TO_HKVECTOR4(physicsComponent->InitialLinearVelocity);
|
||||
rigidBodyInfo.m_angularVelocity = GLMVEC3_TO_HKVECTOR4(physicsComponent->InitialAngularVelocity);
|
||||
rigidBodyInfo.m_linearDamping = physicsComponent->LinearDamping;
|
||||
rigidBodyInfo.m_angularDamping = physicsComponent->AngularDamping;
|
||||
rigidBodyInfo.m_gravityFactor = physicsComponent->GravityFactor;
|
||||
rigidBodyInfo.m_linearDamping = physicsComponent->LinearDamping;
|
||||
rigidBodyInfo.m_friction = physicsComponent->Friction;
|
||||
rigidBodyInfo.m_restitution = physicsComponent->Restitution;
|
||||
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);
|
||||
@@ -386,10 +407,24 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
|
||||
hkQuaternion rotation = GLMQUAT_TO_HKQUATERNION(absoluteTransform.Orientation);
|
||||
rigidBodyInfo.m_position.set(position(0), position(1), position(2), position(3));
|
||||
rigidBodyInfo.m_rotation.set(rotation(0), rotation(1), rotation(2), rotation(3));
|
||||
|
||||
|
||||
rigidBodyInfo.m_inertiaTensor = massProperties.m_inertiaTensor;
|
||||
//rigidBodyInfo.m_centerOfMass = massProperties.m_centerOfMass; //HACK: CENTER OF MASS ALWAYS IN THE CENTER
|
||||
if(physicsComponent->CalculateCenterOfMass)
|
||||
physicsComponent->CenterOfMass = HKVECTOR4_TO_GLMVEC3(massProperties.m_centerOfMass);
|
||||
rigidBodyInfo.m_centerOfMass = GLMVEC3_TO_HKVECTOR4(physicsComponent->CenterOfMass);
|
||||
rigidBodyInfo.m_mass = massProperties.m_mass;
|
||||
rigidBodyInfo.m_linearVelocity = GLMVEC3_TO_HKVECTOR4(physicsComponent->InitialLinearVelocity);
|
||||
rigidBodyInfo.m_angularVelocity = GLMVEC3_TO_HKVECTOR4(physicsComponent->InitialAngularVelocity);
|
||||
rigidBodyInfo.m_linearDamping = physicsComponent->LinearDamping;
|
||||
rigidBodyInfo.m_angularDamping = physicsComponent->AngularDamping;
|
||||
rigidBodyInfo.m_gravityFactor = physicsComponent->GravityFactor;
|
||||
rigidBodyInfo.m_linearDamping = physicsComponent->LinearDamping;
|
||||
rigidBodyInfo.m_friction = physicsComponent->Friction;
|
||||
rigidBodyInfo.m_restitution = physicsComponent->Restitution;
|
||||
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);
|
||||
@@ -402,8 +437,6 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
|
||||
|
||||
shape->removeReference();
|
||||
rigidBody->removeReference();
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -595,3 +628,21 @@ void Systems::PhysicsSystem::OnEntityRemoved( EntityID entity )
|
||||
m_PhysicsWorld->unmarkForWrite();
|
||||
}
|
||||
}
|
||||
|
||||
bool Systems::PhysicsSystem::OnEnableCollisions( const Events::EnableCollisions &e )
|
||||
{
|
||||
m_CollisionFilter->enableCollisionsBetween(e.Layer1, e.Layer2);
|
||||
m_PhysicsWorld->markForWrite();
|
||||
m_PhysicsWorld->setCollisionFilter(m_CollisionFilter);
|
||||
m_PhysicsWorld->unmarkForWrite();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Systems::PhysicsSystem::OnDisableCollisions( const Events::DisableCollisions &e )
|
||||
{
|
||||
m_CollisionFilter->disableCollisionsBetween(e.Layer1, e.Layer2);
|
||||
m_PhysicsWorld->markForWrite();
|
||||
m_PhysicsWorld->setCollisionFilter(m_CollisionFilter);
|
||||
m_PhysicsWorld->unmarkForWrite();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -78,6 +78,9 @@
|
||||
|
||||
#include "Components/TankShell.h"
|
||||
#include <Physics2012/Collide/Shape/Misc/PhantomCallback/hkpPhantomCallbackShape.h>
|
||||
#include "Events/EnableCollisions.h"
|
||||
#include "Events/DisableCollisions.h"
|
||||
|
||||
namespace Systems
|
||||
{
|
||||
class PhysicsSystem : public System
|
||||
@@ -95,20 +98,18 @@ public:
|
||||
EntityID entity1 = m_PhysicsSystem->m_RigidBodyEntities[event.getBody(0)];
|
||||
EntityID entity2 = m_PhysicsSystem->m_RigidBodyEntities[event.getBody(1)];
|
||||
|
||||
m_PhysicsSystem->m_PhysicsWorld->unmarkForRead();
|
||||
Events::Collision e;
|
||||
e.Entity1 = entity1;
|
||||
e.Entity2 = entity2;
|
||||
m_PhysicsSystem->EventBroker->Publish(e);
|
||||
m_PhysicsSystem->m_PhysicsWorld->markForRead();
|
||||
//m_PhysicsSystem->EventBroker->Publish(e);
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
Systems::PhysicsSystem* m_PhysicsSystem;
|
||||
};
|
||||
friend class MyCollisionResolution;
|
||||
|
||||
/*class PhantomCallbackShape: public hkpPhantomCallbackShape
|
||||
class PhantomCallbackShape: public hkpPhantomCallbackShape
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -124,6 +125,8 @@ public:
|
||||
glm::vec3 impulse;
|
||||
impulse = HKVECTOR4_TO_GLMVEC3(hkpGetRigidBody(collidableA)->getPosition()) - HKVECTOR4_TO_GLMVEC3(hkpGetRigidBody(collidableB)->getPosition());
|
||||
e.Impulse = impulse;
|
||||
|
||||
m_PhysicsSystem->EventBroker->Publish(e);
|
||||
}
|
||||
|
||||
virtual void phantomLeaveEvent( const hkpCollidable* collidableA, const hkpCollidable* collidableB )
|
||||
@@ -135,7 +138,7 @@ public:
|
||||
Systems::PhysicsSystem* m_PhysicsSystem;
|
||||
};
|
||||
friend class PhantomCallbackShape;
|
||||
*/
|
||||
|
||||
PhysicsSystem(World* world, std::shared_ptr<::EventBroker> eventBroker)
|
||||
: System(world, eventBroker) { }
|
||||
|
||||
@@ -152,6 +155,7 @@ public:
|
||||
private:
|
||||
double m_Accumulator;
|
||||
hkpWorld* m_PhysicsWorld;
|
||||
hkpGroupFilter* m_CollisionFilter;
|
||||
|
||||
// Events
|
||||
EventRelay<Events::TankSteer> m_ETankSteer;
|
||||
@@ -163,6 +167,12 @@ private:
|
||||
EventRelay<Events::ApplyPointImpulse> m_EApplyPointImpulse;
|
||||
bool OnApplyPointImpulse(const Events::ApplyPointImpulse &event);
|
||||
|
||||
EventRelay<Events::EnableCollisions> m_EEnableCollisions;
|
||||
bool OnEnableCollisions(const Events::EnableCollisions &e);
|
||||
EventRelay<Events::DisableCollisions> m_EDisableCollisions;
|
||||
bool OnDisableCollisions(const Events::DisableCollisions &e);
|
||||
|
||||
|
||||
void SetUpPhysicsState(EntityID entity, EntityID parent);
|
||||
void TearDownPhysicsState(EntityID entity, EntityID parent);
|
||||
|
||||
|
||||
@@ -114,6 +114,25 @@ bool Systems::TankSteeringSystem::OnCollision( const Events::Collision &e )
|
||||
health2->health -= tankShell1->Damage;
|
||||
}
|
||||
|
||||
{
|
||||
auto hitSphere = m_World->CreateEntity();
|
||||
auto transformHit = m_World->AddComponent<Components::Transform>(hitSphere);
|
||||
transformHit->Position = transform->Position;
|
||||
|
||||
auto physics = m_World->AddComponent<Components::Physics>(hitSphere);
|
||||
physics->Static = false;
|
||||
physics->Phantom = true;
|
||||
|
||||
{
|
||||
auto shape = m_World->CreateEntity(hitSphere);
|
||||
auto transformHit = m_World->AddComponent<Components::Transform>(shape);
|
||||
auto sphereShape = m_World->AddComponent<Components::SphereShape>(shape);
|
||||
sphereShape->Radius = 40.f*0.5f; //HACK:
|
||||
m_World->CommitEntity(shape);
|
||||
}
|
||||
m_World->CommitEntity(hitSphere);
|
||||
}
|
||||
|
||||
m_World->RemoveEntity(e.Entity1);
|
||||
}
|
||||
|
||||
@@ -128,6 +147,26 @@ bool Systems::TankSteeringSystem::OnCollision( const Events::Collision &e )
|
||||
health1->health -= tankShell1->Damage;
|
||||
}
|
||||
|
||||
{
|
||||
auto hitSphere = m_World->CreateEntity();
|
||||
auto transformHit = m_World->AddComponent<Components::Transform>(hitSphere);
|
||||
transformHit->Position = transform->Position;
|
||||
|
||||
auto physics = m_World->AddComponent<Components::Physics>(hitSphere);
|
||||
physics->Static = false;
|
||||
physics->Phantom = true;
|
||||
|
||||
{
|
||||
auto shape = m_World->CreateEntity(hitSphere);
|
||||
auto transformHit = m_World->AddComponent<Components::Transform>(shape);
|
||||
auto sphereShape = m_World->AddComponent<Components::SphereShape>(shape);
|
||||
sphereShape->Radius = 40.f*0.5f; //HACK:
|
||||
m_World->CommitEntity(shape);
|
||||
}
|
||||
m_World->CommitEntity(hitSphere);
|
||||
}
|
||||
|
||||
|
||||
m_World->RemoveEntity(e.Entity2);
|
||||
}
|
||||
}
|
||||
@@ -183,6 +222,20 @@ bool Systems::TankSteeringSystem::TankSteeringInputController::OnCommand(const E
|
||||
m_Shoot = val > 0;
|
||||
}
|
||||
|
||||
else if(event.Command == "EnableCollisions")
|
||||
{
|
||||
Events::EnableCollisions e;
|
||||
e.Layer1 = 1;
|
||||
e.Layer2 = 2;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
else if(event.Command == "DisableCollisions")
|
||||
{
|
||||
Events::DisableCollisions e;
|
||||
e.Layer1 = 1;
|
||||
e.Layer2 = 2;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
#include "Components/TankShell.h"
|
||||
#include "Components/SphereShape.h"
|
||||
|
||||
#include "Events/EnableCollisions.h"
|
||||
#include "Events/DisableCollisions.h"
|
||||
namespace Systems
|
||||
{
|
||||
|
||||
|
||||
Reference in New Issue
Block a user