Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <memory>
- #include <list>
- #include <SFML/System.hpp>
- #define PIX_IN_METER (50)
- #define METER_IN_PIX (1.0f/50.0f)
- #define B2_DEGTORAD 0.0174532925199432957f
- #define B2_RADTODEG 57.295779513082320876f
- namespace rg {
- namespace phys {
- typedef double unit_t;
- class Vec2 {
- public:
- unit_t x, y;
- Vec2(const unit_t x = 0, const unit_t y = 0) : x(x), y(y) {}
- Vec2(const sf::Vector2<double>& vec) : x(vec.x), y(vec.y) {}
- Vec2(const sf::Vector2<float>& vec) : x(vec.x), y(vec.y) {}
- virtual ~Vec2() {}
- template<typename T>
- operator sf::Vector2<T>() const {
- return sf::Vector2<T>(static_cast<T>(this->x), static_cast<T>(this->y));
- }
- };
- class AABB {
- public:
- Vec2 min, max;
- AABB() {}
- AABB(const Vec2& min, const Vec2& max) : min(min), max(max) {}
- virtual ~AABB() {}
- inline bool is_cross(const AABB& other) {
- if (max.x < other.min.x || min.x > other.max.x) return false;
- else if (max.y < other.min.y || min.y > other.max.y) return false;
- else return true;
- }
- };
- class World;
- class Body {
- public:
- typedef void* user_data_t;
- Body(World& world);
- ~Body();
- Body(const Body& other) = delete;
- Body& operator=(const Body& other) = delete;
- inline const World& world() const { return m_world; }
- inline World& world() { return m_world; }
- inline const Vec2& pos() const { return m_pos; }
- inline Vec2& pos() { return m_pos; }
- inline const AABB& aabb() const { return m_aabb; }
- inline AABB& aabb() { return m_aabb; }
- inline const Vec2& velocity() const { return m_velocity; }
- inline Vec2& velocity() { return m_velocity; }
- inline const user_data_t& user_data() const { return m_user_data; }
- inline user_data_t& user_data() { return m_user_data; }
- private:
- World& m_world;
- Vec2 m_pos;
- AABB m_aabb;
- Vec2 m_velocity;
- user_data_t m_user_data;
- };
- class World {
- public:
- World();
- virtual ~World();
- void step(const unit_t& seconds);
- std::shared_ptr<Body> create_body();
- void free_body(const Body* body);
- const std::list< std::shared_ptr<Body> >& get_bodies() const { return m_bodies; }
- private:
- std::list< std::shared_ptr<Body> > m_bodies;
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment