#ifndef _H_PHYSICS_H #define _H_PHYSICS_H #include #include #include class EventManager; struct PhysicsComponent { bool isBullet = false; // This ID holds the ID of the gameactor that owns this object, that being themselves or a bullet. unsigned int ID = 0; // If the ID stays 0 then that object has no owner and will be uncollidable struct RigidBody { glm::vec3 position; glm::vec3 velocity; glm::vec3 acceleration; float elasticity = 0.7f; float mass = 1.0f; void applyForce(glm::vec3 dir, float mag) { acceleration += ((dir * mag) / mass); } }rigidBody; struct Collider { enum class Shape { Square, Circle } shape = Shape::Circle; glm::vec3 offset = glm::vec3(0.f); glm::vec3 dimensions; }collider; }; class PhysicsComponentFactory { public: static PhysicsComponent makeBullet(const unsigned int ID, const glm::vec3& pos, float mass, float radius) { PhysicsComponent p; p.ID = ID; p.rigidBody.position = pos; p.rigidBody.mass = mass; p.collider = { PhysicsComponent::Collider::Shape::Circle, glm::vec3(radius, radius, 0.f), glm::vec3(radius) }; p.isBullet = true; return p; } }; class PhysicsEngine { public: PhysicsEngine() {} using CollisionPair = std::pair; void hookEventManager(const std::shared_ptr& eventManager); std::shared_ptr createObject(const unsigned int ID, const glm::vec3& pos, float mass, PhysicsComponent::Collider::Shape shape, glm::vec3 dimensions, glm::vec3 offset = glm::vec3(0.f)); void loadCollisionMap(const std::vector>&, float tileSize); void addObject(const std::shared_ptr&); void removeObject(const std::shared_ptr&); void update(double deltaTime); private: void resolveWorldCollision(const std::shared_ptr&); void resolvePossibleCollisions(); void getPossibleCollisions(); int getTileCollider(const glm::vec3& position); std::vector> objects; std::vector objCollisions; std::vector> collisionMap; float tileSize = 32.f; std::shared_ptr eventManager; }; #endif //_H_PHYSICS_H