Guest User

phys.hpp

a guest
Feb 12th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <memory>
  4. #include <list>
  5.  
  6. #include <SFML/System.hpp>
  7.  
  8. #define PIX_IN_METER (50)
  9. #define METER_IN_PIX (1.0f/50.0f)
  10.  
  11. #define B2_DEGTORAD 0.0174532925199432957f
  12. #define B2_RADTODEG 57.295779513082320876f
  13.  
  14. namespace rg {
  15. namespace phys {
  16.  
  17. typedef double  unit_t;
  18.  
  19. class Vec2 {
  20. public:
  21.     unit_t  x, y;
  22.  
  23.     Vec2(const unit_t  x = 0, const unit_t  y = 0) : x(x), y(y) {}
  24.     Vec2(const sf::Vector2<double>&  vec) : x(vec.x), y(vec.y) {}
  25.     Vec2(const sf::Vector2<float>&  vec) : x(vec.x), y(vec.y) {}
  26.  
  27.     virtual ~Vec2() {}
  28.    
  29.     template<typename T>
  30.     operator sf::Vector2<T>() const {
  31.         return sf::Vector2<T>(static_cast<T>(this->x), static_cast<T>(this->y));
  32.     }
  33. };
  34.  
  35. class AABB {
  36. public:
  37.     Vec2  min, max;
  38.  
  39.     AABB() {}
  40.     AABB(const Vec2&  min, const Vec2&  max) : min(min), max(max) {}
  41.     virtual ~AABB() {}
  42.    
  43.     inline bool  is_cross(const AABB&  other) {
  44.         if (max.x < other.min.x || min.x > other.max.x) return false;
  45.         else if (max.y < other.min.y || min.y > other.max.y) return false;
  46.         else return true;
  47.     }
  48. };
  49.  
  50. class World;
  51.  
  52. class Body {
  53. public:
  54.     typedef void*  user_data_t;
  55.  
  56.     Body(World&  world);
  57.     ~Body();
  58.  
  59.     Body(const Body&  other) = delete;
  60.     Body& operator=(const Body&  other) = delete;
  61.  
  62.     inline const World&  world() const { return m_world; }
  63.     inline World&  world() { return m_world; }
  64.  
  65.     inline const Vec2&  pos() const { return m_pos; }
  66.     inline Vec2&  pos() { return m_pos; }
  67.  
  68.     inline const AABB&  aabb() const { return m_aabb; }
  69.     inline AABB&  aabb() { return m_aabb; }
  70.  
  71.     inline const Vec2&  velocity() const { return m_velocity; }
  72.     inline Vec2&  velocity() { return m_velocity; }
  73.  
  74.     inline const user_data_t&  user_data() const { return m_user_data; }
  75.     inline user_data_t&  user_data() { return m_user_data; }
  76. private:
  77.     World&  m_world;
  78.  
  79.     Vec2  m_pos;
  80.     AABB  m_aabb;
  81.  
  82.     Vec2  m_velocity;
  83.    
  84.     user_data_t  m_user_data;
  85. };
  86.  
  87.  
  88. class World {
  89. public:
  90.     World();
  91.     virtual ~World();
  92.    
  93.     void  step(const unit_t&  seconds);
  94.    
  95.     std::shared_ptr<Body>  create_body();
  96.     void  free_body(const Body*  body);
  97.  
  98.     const std::list< std::shared_ptr<Body> >&  get_bodies() const { return m_bodies; }
  99. private:
  100.     std::list< std::shared_ptr<Body> >  m_bodies;
  101. };
  102.  
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment