8 #include <ThirdParty/cereal/include/cereal/archives/binary.hpp> 9 #include <ThirdParty/cereal/include/cereal/archives/json.hpp> 13 CollisionComponent::CollisionComponent(
CollisionShapeType shapeType, b2BodyType bodyType)
18 , shapeType(shapeType)
29 bodyDef.fixedRotation =
true;
30 bodyDef.gravityScale = 0;
31 bodyDef.linearDamping = 9999.0f;
32 bodyDef.type = bodyType;
34 fixtureDef.filter.categoryBits = 0;
36 fixtureDef.isSensor =
false;
39 void CollisionComponent::InitializeComponent()
41 Engine::GetEngine().lock()->GetCollisionSystem().lock()->AddCollisionComponent(GetPointerRefence<CollisionComponent>());
44 CollisionComponent::~CollisionComponent()
46 Engine::GetEngine().lock()->GetCollisionSystem().lock()->RemoveCollisionComponent(
this);
49 void CollisionComponent::OnComponentAdded(eastl::weak_ptr<Component> addedComponent)
51 if (eastl::dynamic_pointer_cast<TransformComponent>(addedComponent.lock()) && transformComponent.expired())
53 transformComponent = eastl::static_pointer_cast<TransformComponent>(addedComponent.lock());
57 void CollisionComponent::OnComponentRemoved(eastl::weak_ptr<Component> removedComponent)
59 if (transformComponent.lock().get() == removedComponent.lock().get())
60 transformComponent.reset();
63 transformComponent = GetComponent<TransformComponent>();
66 eastl::weak_ptr<TransformComponent> CollisionComponent::GetTransformComponent()
const 68 return transformComponent;
75 CollisionComponent::CollisionComponent(CollisionComponent& collisionComponent)
77 fixtureDef = collisionComponent.fixtureDef;
78 bodyDef = collisionComponent.bodyDef;
79 boxShape = collisionComponent.boxShape;
80 boxSize = collisionComponent.boxSize;
81 circleShape = collisionComponent.circleShape;
82 shapeType = collisionComponent.shapeType;
88 bodyDef.type = bodyType;
99 circleShape.m_radius = radius;
104 return circleShape.m_radius;
109 boxShape.SetAsBox(sizeX, sizeY);
110 boxSize = b2Vec2(sizeX, sizeY);
111 fixtureDef.shape = &boxShape;
117 SetAABB(size.x, size.y);
122 return glm::vec2(boxSize.x, boxSize.y);
132 fixtureDef.filter.categoryBits = uint16(layer);
142 fixtureDef.filter.maskBits = uint16(layers);
152 fixtureDef.isSensor = isSensor;
157 return fixtureDef.isSensor;
159 template <
typename archive>
162 int bodyType =
static_cast<int>(this->GetBodyType());
163 float circleRadius = GetCircleRadius();
164 glm::vec2 aaBBsize = this->GetAABBSize();
165 float aaBBsizeArr[] = { aaBBsize.x, aaBBsize.y };
166 int collisionLayer =
static_cast<int>(this->GetCollisionLayer());
167 int detectionLayer =
static_cast<int>(this->GetDetectionLayers());
169 ar(CEREAL_NVP(bodyType), CEREAL_NVP(circleRadius), CEREAL_NVP(aaBBsizeArr), CEREAL_NVP(collisionLayer), CEREAL_NVP(detectionLayer), CEREAL_NVP(
isEnabled));
173 template <
typename archive>
182 ar(bodyType, circleRadius, aaBBsizeArr, collisionLayer, detectionLayer,
isEnabled);
184 SetBodyType(static_cast<b2BodyType>(bodyType));
185 SetCircle(circleRadius);
186 SetAABB(aaBBsizeArr[0], aaBBsizeArr[1]);
187 SetCollisionLayer(static_cast<CollisionLayer>(collisionLayer));
188 SetDetectionLayers(static_cast<CollisionLayer>(detectionLayer));
CollisionLayer GetDetectionLayers() const
Gets the detection layers
void SetIsSensor(bool isSensor)
Sets if the collision object will be a sensor or not. NOTE: A sensor is an object that does do collis...
void LoadComponent(archive ar)
loads the data from the archive to this component
CollisionShapeType GetCollisionShapeType() const
Gets the collision shape type of the collision component
void SetBodyType(b2BodyType bodyType)
Sets the Body Type (Static, Dynamic, Kinematic) for the collision body.
CollisionLayer GetCollisionLayer() const
Gets the collision layer
void SetCircle(const float radius)
Sets the Shape of the component to Circle
CollisionShapeType
Enum for available collision shapes
float GetCircleRadius() const
Get the Radius of the circle shape (still returns even if it isn't a circle shape) ...
void SetAABB(const float sizeX, const float sizeY)
Sets the Shape of the component to AABB
void SetCollisionLayer(CollisionLayer layer)
Sets the collision layer The collision layer is the "type" of the collision body,other collision bodi...
void SaveComponent(archive ar)
glm::vec2 GetAABBSize() const
Gets the size of AABB. (Still returns the size even if the shape isn't AABB)
bool GetIsSensor() const
Returns true if the collision object is a sensor
static eastl::weak_ptr< Engine > GetEngine() noexcept
This method allows you to get the instance of the Engine. This method will automatically initialize t...
void SetDetectionLayers(CollisionLayer layers)
Sets the detection layers The detection layers are what this collision object will be looking for to ...
CollisionLayer
Bitmask Enum that allows for layered collision checking
b2BodyType GetBodyType() const
Gets the Body Type (Static, Dynamic, Kinematic) of the collision body.