1#include "GhostSystem.h"
2#include "PhysicsManager.h"
4#include <glm/gtc/type_ptr.hpp>
6#include "Systems/Core/TransformSystem.h"
10#include "Services/Services.h"
11#include "Services/Time.h"
12#include "Services/TaskManager.h"
14#include "EntityStore.h"
16#include "CollisionComponent.h"
18#include "Platform/Instrumentation.h"
23 void updateProperties(
GhostData& ghostData)
25 ghostData.collisionShape->setLocalScaling(toBullet(ghostData.scale));
27 const float margin = glm::min(glm::min(ghostData.scale.x, ghostData.scale.y), ghostData.scale.z);
28 const float marginMultiplier = 0.1f;
29 auto diff = ghostData.scale - glm::vec3(1, 1, 1);
31 if (glm::any(glm::greaterThan(glm::abs(diff), glm::vec3(0.01f, 0.01f, 0.01f)))) {
32 ghostData.collisionShape->setMargin(margin * marginMultiplier);
42 for (
auto & ghostComp :
pool) {
43 if (!ghostComp.isActive())
continue;
45 auto & ghostData = getData(&ghostComp);
48 context->transformSystem->updateLocalToWorldTransform(*trComp,
true);
49 const auto & worldTransform =
context->transformSystem->getLocalToWorld(trComp);
50 const auto scale = glm::vec3(glm::length(worldTransform[0]), glm::length(worldTransform[1]), glm::length(worldTransform[2]));
51 const auto rotation = glm::normalize(glm::quat_cast(glm::mat3(worldTransform * glm::scale(glm::vec3(1.0f / scale.x, 1.0f / scale.y, 1.0f / scale.z)))));
52 const auto position = glm::vec4(worldTransform[3]);
53 const btTransform origin(toBullet(rotation), toBullet(position));
55 if (!ghostData.collisionObject) {
57 if (!collComp || !collComp->collisionShape) {
60 ghostData.collisionShape = collComp->collisionShape;
65 ghostData.scale = scale;
66 updateProperties(ghostData);
68 ghostData.shared = std::make_unique<SharedRigidBodyData>();
71 ghostData.collisionObject = std::make_unique<btPairCachingGhostObject>();
72 ghostData.collisionObject->setUserPointer(ghostData.shared.get());
74 ghostData.collisionObject->setCollisionShape(collComp->collisionShape);
75 ghostData.collisionObject->setCollisionFlags(ghostData.collisionObject->getCollisionFlags() | btCollisionObject::CF_CHARACTER_OBJECT);
79 manager->addCollisionObject(ghostData.collisionObject.get());
82 if (glm::any(glm::greaterThan(glm::abs(scale - ghostData.scale), glm::vec3(0.001f)))) {
83 manager->removeCollisionObject(ghostData.collisionObject.get());
84 ghostData.scale = scale;
85 updateProperties(ghostData);
86 manager->addCollisionObject(ghostData.collisionObject.get());
88 ghostData.collisionObject->setWorldTransform(origin);
95 auto & ghostData = getData(ghostComp);
97 if (ghostData.collisionObject) {
99 manager->removeCollisionObject(ghostData.collisionObject.get());
Context * context
Pointer to the Context instance the system lives in.
virtual void destroyComponent(ComponentHandle)
Destroy the component held by the given handle.
void update()
Updates the system state to that of the current frame.
ComponentPool< ComponentType > pool
Pool of components managed by the system.
A Context instance contains all the services, systems and runtime components needed to use Cogs.
std::unique_ptr< class Services > services
Services.
class EntityStore * store
Entity store.
EntityPtr getEntity(const StringView &name, bool logIfNotFound=true) const
Retrieve a reference to the shared entity pointer to the Entity with the given name.
void destroyComponent(ComponentHandle component) final
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
Handle to a Component instance.
ComponentType * resolveComponent() const