Advertisement
Redee

Moving object c++

Apr 1st, 2015
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1. //ы
  2.  
  3. //#include <windows.h>
  4. //#include <iostream>
  5. //using namespace std;
  6. #include "SFML/Graphics.hpp"
  7. using namespace sf;
  8.  
  9.  
  10. // Interpolation
  11. // trying to do like here >> http://gafferongames.com/game-physics/fix-your-timestep/
  12. //
  13. // Problem - motion a bit laggy with flickering 1px sprite border
  14. // also a bit slowed and(or) accelerated
  15.  
  16.  
  17. class timeLogic
  18. {
  19.     Clock cl;
  20.     bool start;
  21.     Int64 prevTme, currTme, accum;
  22.    
  23.     public:
  24.         const Int64 tmeStep;
  25.         Int64 frTme;
  26.         float alpha;
  27.         unsigned short stepCount;
  28.  
  29.         // 100 fps fixed step
  30.         timeLogic() : tmeStep(10000)
  31.         {
  32.             start = true;
  33.             currTme = 0;
  34.             accum = 0;
  35.         }
  36.        
  37.         void startTme()
  38.         {
  39.             if(start)
  40.             {
  41.                 cl.restart();
  42.                 start = false;
  43.             }
  44.         }
  45.  
  46.         void newFrame()
  47.         {
  48.             prevTme = currTme;
  49.             currTme = cl.getElapsedTime().asMicroseconds();
  50.             frTme = currTme - prevTme;
  51.             if(frTme > 40000)
  52.                 frTme = 40000;
  53.  
  54.             accum += frTme;
  55.             // wipe value, interpolated in previous frame if setted
  56.             stepCount = 0;
  57.             while(accum >= tmeStep)
  58.             {
  59.                 // set count to sprite interpolation logic
  60.                 stepCount++;
  61.                 accum -= tmeStep;
  62.             }
  63.             // interpolation alpha
  64.             alpha = (float)accum / tmeStep;
  65.         }
  66. }
  67. tmeL;
  68.  
  69.  
  70. class moveSpriteLogic
  71. {
  72.     Vector2f prevPos;
  73.     Vector2f currPos;
  74.     Vector2f interpPos;
  75.     Vector2f realPos;
  76.  
  77.     float velX, velY;
  78.  
  79.  
  80.     public:
  81.         Texture tx;
  82.         Sprite sp;
  83.  
  84.     moveSpriteLogic(char* file, float velocityX, float velocityY)
  85.     {
  86.         tx.loadFromFile(file);
  87.         sp.setTexture(tx);
  88.  
  89.         // velocity for 1 microsecond
  90.         velX = velocityX;
  91.         velY = velocityY;
  92.  
  93.         currPos.x = sp.getPosition().x;
  94.         currPos.y = sp.getPosition().y;
  95.        
  96.         prevPos = currPos;
  97.     }
  98.  
  99.     // main Interpolation method
  100.     void move()
  101.     {
  102.         if(tmeL.stepCount)
  103.         {
  104.             for(unsigned short n = 0; n < tmeL.stepCount; n++)
  105.             {
  106.                 prevPos = currPos;
  107.                 currPos.x += velX * tmeL.tmeStep;
  108.                 currPos.y += velY * tmeL.tmeStep;
  109.             }
  110.            
  111.         }
  112.  
  113.         // Interpolation
  114.         interpPos.x = currPos.x * tmeL.alpha + prevPos.x * (1 - tmeL.alpha);
  115.         interpPos.y = currPos.y * tmeL.alpha + prevPos.y * (1 - tmeL.alpha);
  116.  
  117.         realPos.x = floor(interpPos.x + 0.5f);
  118.         realPos.y = floor(interpPos.y + 0.5f);
  119.  
  120.         sp.setPosition(realPos.x, realPos.y);
  121.     }
  122.  
  123.     void respawn()
  124.     {
  125.         if(realPos.x > 540)
  126.         {
  127.             sp.setPosition(0, 0);
  128.             currPos.x = 0;
  129.             currPos.y = 0;
  130.             prevPos = currPos;
  131.         }
  132.     }
  133. };
  134.  
  135.  
  136. void main()
  137. {
  138.     RenderWindow wIn;
  139.     wIn.create(VideoMode(600, 500), "My12345", Style::Default);
  140.  
  141.     moveSpriteLogic mspL("tree.png", 0.00015f, 0);
  142.     Sprite &sp = mspL.sp;
  143.  
  144.     Event ev;
  145.     tmeL.startTme();
  146.     while (wIn.isOpen())
  147.     {
  148.         tmeL.newFrame();
  149.  
  150.         while (wIn.pollEvent(ev))
  151.         {
  152.             // Close window : exit
  153.             if (ev.type == Event::Closed)
  154.                 wIn.close();
  155.             if (ev.type == Event::KeyPressed)
  156.             {
  157.                 if (ev.key.code == Keyboard::Escape)
  158.                     wIn.close();
  159.                 else if (ev.key.control && ev.key.code == Keyboard::C)
  160.                     wIn.close();
  161.             }
  162.         }
  163.         wIn.clear();
  164.        
  165.         // interpolation move sprite
  166.         mspL.move();
  167.         mspL.respawn();
  168.        
  169.         wIn.draw(sp);
  170.         wIn.display();
  171.        
  172.         // reduce CPU charge
  173.         // value must be in milliseconds
  174.         // 1 millsec = 1000 microsec
  175.         sleep(milliseconds(1));
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement