8 #define INT_TO_VOID_PTR(val) ((void*)(size_t) val) 13 CollisionSystem::CollisionSystem() : world_(b2Vec2(0, 0)), isRunning_(
false)
15 world_.SetAllowSleeping(
false);
22 void CollisionSystem::AddCollisionComponent(eastl::weak_ptr<CollisionComponent> componentToAdd)
24 collisionComponents_.push_back(componentToAdd);
27 componentToAdd.lock()->body = world_.CreateBody(&(componentToAdd.lock()->bodyDef));
28 componentToAdd.lock()->body->CreateFixture(&(componentToAdd.lock()->fixtureDef));
29 componentToAdd.lock()->body->GetFixtureList()->SetUserData(
INT_TO_VOID_PTR(componentToAdd.lock()->GetOwner().lock()->GetID()));
34 if (world_.GetBodyCount() == 0)
36 if (componentToRemove ==
nullptr)
38 if (componentToRemove->body ==
nullptr)
41 world_.DestroyBody(componentToRemove->body);
44 void CollisionSystem::OnLevelLoaded()
46 if (collisionComponents_.size() > 0)
50 void CollisionSystem::OnLevelUnloaded()
52 if (isRunning_ ==
false)
56 collisionComponents_.clear();
74 if (isRunning_ ==
false)
77 world_.SetContactListener(&entityContactCallback_);
80 for (
auto colComp : collisionComponents_)
82 eastl::weak_ptr<TransformComponent> transform = colComp.lock()->GetTransformComponent();
83 if (transform.expired() ==
false)
86 const glm::vec3 position = transform.lock()->GetPosition();
87 colComp.lock()->body->SetTransform(b2Vec2(position.x, position.y), colComp.lock()->body->GetAngle());
91 const float deltaTime =
Engine::GetEngine().lock()->GetTime().lock()->GetDeltaTime();
92 world_.Step(deltaTime, 8, 4);
95 for (
auto colComp : collisionComponents_)
97 eastl::weak_ptr<TransformComponent> transform = colComp.lock()->GetTransformComponent();
98 if (transform.expired() ==
false)
101 const glm::vec3 oldPosition = transform.lock()->GetPosition();
102 const b2Vec2& position = colComp.lock()->body->GetPosition();
104 transform.lock()->SetPosition(glm::vec3(position.x, position.y, oldPosition.z));
112 callback.detectionLayers = detectionLayers;
117 return eastl::weak_ptr<Entity>();
120 world_.RayCast(&callback, b2Vec2(start.x, start.y), b2Vec2(end.x, end.y));
122 return callback.entity;
128 return eastl::weak_ptr<Entity>();
138 return collisionComponents_;
141 void CollisionSystem::ClearWorld()
143 b2Body* body = world_.GetBodyList();
145 while (body !=
nullptr)
147 b2Body* nextBody = body->GetNext();
148 world_.DestroyBody(body);
void Start()
Starts the Collision System, gathering all required component data and generating collision bodies fr...
void Update()
Updates the collision world. Takes transform data of collision components, changes it...
bool IsRunning() const
Returns true if the Collision system is running
eastl::weak_ptr< Entity > RayQueryAllHit(const glm::vec2 &start, const glm::vec2 &end) const
Returns all collision-objects hit by the ray. TODO - Implementation
The Collision component is an entities link to the Collision System and allows for interaction with t...
eastl::vector< eastl::weak_ptr< CollisionComponent > > GetActiveCollisionComponents() const
Gets all of the collision components that are currently active in the world.
Gets the first entity hit by the ray Callback class used to get collision data from ray queries ...
static eastl::weak_ptr< Engine > GetEngine() noexcept
This method allows you to get the instance of the Engine. This method will automatically initialize t...
CollisionLayer
Bitmask Enum that allows for layered collision checking
eastl::weak_ptr< Entity > RayQueryFirstHit(const glm::vec2 &start, const glm::vec2 &end, CollisionLayer detectionLayers) const
Returns the first collision-body hit by the ray. TODO - Implementation
void Stop()
Stops the Collision System, emptying m_collisionComponents Prevents further calls to Update()...
#define INT_TO_VOID_PTR(val)