Guest User

phys.cpp

a guest
Feb 12th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include "phys.hpp"
  4.  
  5. namespace rg {
  6. namespace phys {
  7.  
  8. Body::Body(World&  world) : m_world(world), m_pos(), m_aabb() {}
  9. Body::~Body() {
  10.     m_world.free_body(this);
  11. }
  12.  
  13.  
  14. World::World() {}
  15. World::~World() {}
  16.    
  17. void  World::step(const unit_t&  seconds) {
  18.     for (auto&  body : m_bodies) {
  19.         body->pos().x += body->velocity().x * seconds;
  20.         body->pos().y += body->velocity().y * seconds;
  21.         std::cout << "Body #" << static_cast<void*>(body.get()) << " x:" << body->pos().x << " y:" << body->pos().y << std::endl;
  22.     }
  23. }
  24.  
  25.  
  26. std::shared_ptr<Body>  World::create_body() {
  27.     // auto  body = std::make_shared<Body>(*this);
  28.     std::shared_ptr<Body>  body (new Body(*this));
  29.     m_bodies.push_back(body);
  30.     std::cout << "Body created at " << body.get() << std::endl;
  31.     for (auto  b : m_bodies) {
  32.         std::cout << "\tNow there are " << b.get() << std::endl;
  33.     }
  34.     return body;
  35. }
  36.  
  37.  
  38. void  World::free_body(const Body*  body) {
  39.     std::cout << "Free body at " << body << std::endl;
  40.     m_bodies.remove_if([body](const std::shared_ptr<Body>&  ptr) { return ptr.get() == body; });
  41. }
  42.  
  43. }
  44. }
Add Comment
Please, Sign In to add comment