Advertisement
DrMikeMorgan

Week 18: Gravity with Cartman

Oct 2nd, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <cmath>
  3.  
  4.  
  5. class Vector2d
  6. {
  7. protected:
  8.     float x,y;
  9. public:
  10.     Vector2d():x(0),y(0){}
  11.     Vector2d(float _x, float _y):x(_x),y(_y){}
  12.     Vector2d operator + (const Vector2d& v){return Vector2d(x+v.x, y+v.y);}
  13.     void operator += (const Vector2d& v){x+=v.x; y+=v.y;}
  14.     Vector2d operator - (const Vector2d& v){return Vector2d(x-v.x, y-v.y);}
  15.     void operator -= (const Vector2d& v){x-=v.x; y-= v.y;}
  16.     Vector2d operator * (const float& s){return Vector2d(x*s, y*s);}
  17.     void operator *= (const float& v){x*=v; y*=v;}
  18.     float operator * (const Vector2d& v){return x*v.x + y*v.y;}
  19.     void operator = (const Vector2d& rhs){x=rhs.x, y=rhs.y;}
  20.     float getx(){return x;}
  21.     float gety(){return y;}
  22.     void setx(float _x){x = _x;}
  23.     void sety(float _y){y = _y;}
  24.     float magnitude(){return sqrt(x*x+y*y);}
  25.     void normalise(){if(!magnitude()) return; x /= magnitude(); y /= magnitude();}
  26.     void clear(){x=y=0.f;}
  27. };
  28.  
  29. class Particle : public sf::Sprite      //inherit particle from Sprite... bit cheeky but will do the job!
  30. {
  31. protected:
  32.     Vector2d pos, vel, acc;
  33.     float damping, inverseMass;
  34. public:
  35.     Particle():pos(),vel(),acc(0,20.0), damping(0.9){}  //sets values for damping and gravity on creation
  36.  
  37.     Vector2d& getPos(){return pos;}          //get and set position
  38.     void setPos(float x, float y){pos = Vector2d(x,y);}
  39.     void setPos(Vector2d v){pos = v;}
  40.  
  41.     Vector2d& getVel(){return vel;}          //get and set velocity
  42.     void setVel(float x, float y){vel = Vector2d(x,y);}
  43.     void setVel(Vector2d v){vel = v;}
  44.  
  45.     void setMass(float mass, bool infinite = false){if(infinite) inverseMass = 0; else inverseMass = 1.f/mass;}
  46.     float getInverseMass(){return inverseMass;}  //get and set mass/inverse mass
  47.  
  48.     void integrate(float time);             //the integrator
  49. };
  50.  
  51. void Particle::integrate(float time)
  52. {
  53.     if(inverseMass == 0)    //don’t integrate if mass is infinite
  54.         return;
  55.  
  56.     Vector2d tempAcc = acc; //apply gravity
  57.  
  58.     //work out acceleration and store in tempAcc (we’ll do this next week)
  59.  
  60.     pos += (vel*time);      //update position
  61.     vel += (tempAcc*time);  //update velocity
  62.  
  63.     vel *= (float) pow(damping,time);   //apply damping
  64.  
  65.     this->setPosition(pos.getx()*100, pos.gety()*100);  //Sneaky hack to make Sprite's onscreen position realistic
  66.                                 //Scales distances to 100 pixels per metre
  67. }
  68.  
  69.  
  70. int main()
  71. {
  72.     const float PHYS_TIMER = 0.002;           //2ms between physics updates
  73.     sf::RenderWindow window(sf::VideoMode(800, 600), "Gravity with Cartman");
  74.  
  75.     sf::Texture tex;
  76.     tex.loadFromFile("cartman.png");
  77.     tex.setSmooth(true);
  78.  
  79.     Particle cartPart;
  80.     cartPart.setTexture(tex);
  81.     cartPart.scale(0.05,0.05);
  82.     cartPart.setPos(1,0);
  83.  
  84.     sf::Clock c;    //timer for physics updates
  85.  
  86.     while (window.isOpen())
  87.     {
  88.         if(c.getElapsedTime().asMilliseconds()>PHYS_TIMER*1000) //has physics timer elapsed?
  89.         {
  90.             cartPart.integrate(PHYS_TIMER);                 //integrate
  91.             c.restart();                                    //restart physics timer
  92.         }
  93.  
  94.         window.clear();
  95.         window.draw(cartPart);
  96.         window.display();
  97.  
  98.         sf::Event event;
  99.         while (window.pollEvent(event))
  100.         {
  101.             if (event.type == sf::Event::Closed)
  102.                 window.close();
  103.         }
  104.     }
  105.  
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement