Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
CollisionComponent.cpp
Go to the documentation of this file.
2 
3 #include "Engine/engine.hpp"
7 
8 #include <ThirdParty/cereal/include/cereal/archives/binary.hpp>
9 #include <ThirdParty/cereal/include/cereal/archives/json.hpp>
10 
11 namespace Engine
12 {
13  CollisionComponent::CollisionComponent(CollisionShapeType shapeType, b2BodyType bodyType)
14  : bodyDef()
15  , fixtureDef()
16  , circleShape()
17  , boxShape()
18  , shapeType(shapeType)
19  {
20  // Set the Shape
21  if (shapeType == CollisionShapeType::CIRCLE)
22  SetCircle(1);
23  else if (shapeType == CollisionShapeType::BOX)
24  SetAABB(1, 1);
25  else
26  assert(false); // Unregistered CollisionObjectType!
27 
28  // Set body settings
29  bodyDef.fixedRotation = true;
30  bodyDef.gravityScale = 0;
31  bodyDef.linearDamping = 9999.0f;
32  bodyDef.type = bodyType;
33  // Set Fixture Settings
34  fixtureDef.filter.categoryBits = 0;
35  fixtureDef.filter.maskBits = uint16(CollisionLayer::SOLID);
36  fixtureDef.isSensor = false;
37  }
38 
39  void CollisionComponent::InitializeComponent()
40  {
41  Engine::GetEngine().lock()->GetCollisionSystem().lock()->AddCollisionComponent(GetPointerRefence<CollisionComponent>());
42  }
43 
44  CollisionComponent::~CollisionComponent()
45  {
46  Engine::GetEngine().lock()->GetCollisionSystem().lock()->RemoveCollisionComponent(this);
47  }
48 
49  void CollisionComponent::OnComponentAdded(eastl::weak_ptr<Component> addedComponent)
50  {
51  if (eastl::dynamic_pointer_cast<TransformComponent>(addedComponent.lock()) && transformComponent.expired())
52  {
53  transformComponent = eastl::static_pointer_cast<TransformComponent>(addedComponent.lock());
54  }
55  }
56 
57  void CollisionComponent::OnComponentRemoved(eastl::weak_ptr<Component> removedComponent)
58  {
59  if (transformComponent.lock().get() == removedComponent.lock().get())
60  transformComponent.reset();
61 
62  //In case we have multiple transformcomponents on this entity for some reason.
63  transformComponent = GetComponent<TransformComponent>();
64  }
65 
66  eastl::weak_ptr<TransformComponent> CollisionComponent::GetTransformComponent() const
67  {
68  return transformComponent;
69  }
70 
71  CollisionComponent::CollisionComponent() : CollisionComponent(CollisionShapeType::BOX, b2_staticBody)
72  {
73  }
74 
75  CollisionComponent::CollisionComponent(CollisionComponent& collisionComponent)
76  {
77  fixtureDef = collisionComponent.fixtureDef;
78  bodyDef = collisionComponent.bodyDef;
79  boxShape = collisionComponent.boxShape;
80  boxSize = collisionComponent.boxSize;
81  circleShape = collisionComponent.circleShape;
82  shapeType = collisionComponent.shapeType;
83  body = nullptr;
84  }
85 
86  void CollisionComponent::SetBodyType(b2BodyType bodyType)
87  {
88  bodyDef.type = bodyType;
89  }
90 
92  {
93  return bodyDef.type;
94  }
95 
96  void CollisionComponent::SetCircle(const float radius)
97  {
98  shapeType = CollisionShapeType::CIRCLE;
99  circleShape.m_radius = radius;
100  }
101 
103  {
104  return circleShape.m_radius;
105  }
106 
107  void CollisionComponent::SetAABB(const float sizeX, const float sizeY)
108  {
109  boxShape.SetAsBox(sizeX, sizeY);
110  boxSize = b2Vec2(sizeX, sizeY);
111  fixtureDef.shape = &boxShape;
112  shapeType = CollisionShapeType::BOX;
113  }
114 
115  void CollisionComponent::SetAABB(const glm::vec2& size)
116  {
117  SetAABB(size.x, size.y);
118  }
119 
121  {
122  return glm::vec2(boxSize.x, boxSize.y);
123  }
124 
126  {
127  return shapeType;
128  }
129 
131  {
132  fixtureDef.filter.categoryBits = uint16(layer);
133  }
134 
136  {
137  return CollisionLayer(fixtureDef.filter.categoryBits);
138  }
139 
141  {
142  fixtureDef.filter.maskBits = uint16(layers);
143  }
144 
146  {
147  return CollisionLayer(fixtureDef.filter.maskBits);
148  }
149 
151  {
152  fixtureDef.isSensor = isSensor;
153  }
154 
156  {
157  return fixtureDef.isSensor;
158  }
159  template <typename archive>
161  {
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());
168 
169  ar(CEREAL_NVP(bodyType), CEREAL_NVP(circleRadius), CEREAL_NVP(aaBBsizeArr), CEREAL_NVP(collisionLayer), CEREAL_NVP(detectionLayer), CEREAL_NVP(isEnabled));
170  }
171 
172 
173  template <typename archive>
175  {
176  int bodyType;
177  float circleRadius;
178  float aaBBsizeArr[];
179  int collisionLayer;
180  int detectionLayer;
181 
182  ar(bodyType, circleRadius, aaBBsizeArr, collisionLayer, detectionLayer, isEnabled);
183 
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));
189  }
190 }
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&#39;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...
glm::vec2 GetAABBSize() const
Gets the size of AABB. (Still returns the size even if the shape isn&#39;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...
Definition: engine.cpp:130
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.