Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <cmath>
- class Vector2d
- {
- protected:
- float x,y;
- public:
- Vector2d():x(0),y(0){}
- Vector2d(float _x, float _y):x(_x),y(_y){}
- Vector2d operator + (const Vector2d& v){return Vector2d(x+v.x, y+v.y);}
- void operator += (const Vector2d& v){x+=v.x; y+=v.y;}
- Vector2d operator - (const Vector2d& v){return Vector2d(x-v.x, y-v.y);}
- void operator -= (const Vector2d& v){x-=v.x; y-= v.y;}
- Vector2d operator * (const float& s){return Vector2d(x*s, y*s);}
- void operator *= (const float& v){x*=v; y*=v;}
- float operator * (const Vector2d& v){return x*v.x + y*v.y;}
- void operator = (const Vector2d& rhs){x=rhs.x, y=rhs.y;}
- float getx(){return x;}
- float gety(){return y;}
- void setx(float _x){x = _x;}
- void sety(float _y){y = _y;}
- float magnitude(){return sqrt(x*x+y*y);}
- void normalise(){if(!magnitude()) return; x /= magnitude(); y /= magnitude();}
- void clear(){x=y=0.f;}
- };
- class Particle : public sf::Sprite //inherit particle from Sprite... bit cheeky but will do the job!
- {
- protected:
- Vector2d pos, vel, acc;
- float damping, inverseMass;
- public:
- Particle():pos(),vel(),acc(0,20.0), damping(0.9){} //sets values for damping and gravity on creation
- Vector2d& getPos(){return pos;} //get and set position
- void setPos(float x, float y){pos = Vector2d(x,y);}
- void setPos(Vector2d v){pos = v;}
- Vector2d& getVel(){return vel;} //get and set velocity
- void setVel(float x, float y){vel = Vector2d(x,y);}
- void setVel(Vector2d v){vel = v;}
- void setMass(float mass, bool infinite = false){if(infinite) inverseMass = 0; else inverseMass = 1.f/mass;}
- float getInverseMass(){return inverseMass;} //get and set mass/inverse mass
- void integrate(float time); //the integrator
- };
- void Particle::integrate(float time)
- {
- if(inverseMass == 0) //don’t integrate if mass is infinite
- return;
- Vector2d tempAcc = acc; //apply gravity
- //work out acceleration and store in tempAcc (we’ll do this next week)
- pos += (vel*time); //update position
- vel += (tempAcc*time); //update velocity
- vel *= (float) pow(damping,time); //apply damping
- this->setPosition(pos.getx()*100, pos.gety()*100); //Sneaky hack to make Sprite's onscreen position realistic
- //Scales distances to 100 pixels per metre
- }
- int main()
- {
- const float PHYS_TIMER = 0.002; //2ms between physics updates
- sf::RenderWindow window(sf::VideoMode(800, 600), "Gravity with Cartman");
- sf::Texture tex;
- tex.loadFromFile("cartman.png");
- tex.setSmooth(true);
- Particle cartPart;
- cartPart.setTexture(tex);
- cartPart.scale(0.05,0.05);
- cartPart.setPos(1,0);
- sf::Clock c; //timer for physics updates
- while (window.isOpen())
- {
- if(c.getElapsedTime().asMilliseconds()>PHYS_TIMER*1000) //has physics timer elapsed?
- {
- cartPart.integrate(PHYS_TIMER); //integrate
- c.restart(); //restart physics timer
- }
- window.clear();
- window.draw(cartPart);
- window.display();
- sf::Event event;
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- window.close();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement