Advertisement
stjernan

Untitled

Feb 1st, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. // Timestep Example Source Code
  2. // Copyright (c) Glenn Fiedler 2004
  3. // http://www.gaffer.org/articles
  4.  
  5. bool openDisplay(int width, int height);
  6. void updateDisplay();
  7. void closeDisplay();
  8. void onQuit();
  9. float time();
  10.  
  11. #include "Apple.h"
  12. #include "Windows.h"
  13.  
  14. bool quit = false;
  15.  
  16. void onQuit()
  17. {
  18.     quit = true;
  19. }
  20.  
  21. struct State
  22. {
  23.     float x;
  24.     float v;
  25. };
  26.  
  27. struct Derivative
  28. {
  29.     float dx;
  30.     float dv;
  31. };
  32.  
  33. State interpolate(const State &previous, const State &current, float alpha)
  34. {
  35.     State state;
  36.     state.x = current.x*alpha + previous.x*(1-alpha);
  37.     state.v = current.v*alpha + previous.v*(1-alpha);
  38.     return state;
  39. }
  40.  
  41. float acceleration(const State &state, float t)
  42. {
  43.     const float k = 10;
  44.     const float b = 1;
  45.     return - k*state.x - b*state.v;
  46. }
  47.  
  48. Derivative evaluate(const State &initial, float t)
  49. {
  50.     Derivative output;
  51.     output.dx = initial.v;
  52.     output.dv = acceleration(initial, t);
  53.     return output;
  54. }
  55.  
  56. Derivative evaluate(const State &initial, float t, float dt, const Derivative &d)
  57. {
  58.     State state;
  59.     state.x = initial.x + d.dx*dt;
  60.     state.v = initial.v + d.dv*dt;
  61.     Derivative output;
  62.     output.dx = state.v;
  63.     output.dv = acceleration(state, t+dt);
  64.     return output;
  65. }
  66.  
  67. void integrate(State &state, float t, float dt)
  68. {
  69.     Derivative a = evaluate(state, t);
  70.     Derivative b = evaluate(state, t, dt*0.5f, a);
  71.     Derivative c = evaluate(state, t, dt*0.5f, b);
  72.     Derivative d = evaluate(state, t, dt, c);
  73.    
  74.     const float dxdt = 1.0f/6.0f * (a.dx + 2.0f*(b.dx + c.dx) + d.dx);
  75.     const float dvdt = 1.0f/6.0f * (a.dv + 2.0f*(b.dv + c.dv) + d.dv);
  76.    
  77.     state.x = state.x + dxdt*dt;
  78.     state.v = state.v + dvdt*dt;
  79. }
  80.  
  81. int main()
  82. {
  83.     const int width = 640;
  84.     const int height = 480;
  85.    
  86.     if (!openDisplay("Timestep", width, height))
  87.         return 1;
  88.  
  89.     glMatrixMode(GL_PROJECTION);
  90.     glLoadIdentity();
  91.     gluPerspective(45.0, 4.0/3.0, 1, 1000);
  92.  
  93.     glMatrixMode(GL_MODELVIEW);
  94.     glLoadIdentity();
  95.     gluLookAt(0,0,150, 0,0,0, 0,1,0);
  96.    
  97.     glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
  98.     glEnable(GL_POINT_SMOOTH);
  99.     glPointSize(4);
  100.    
  101.     glClearColor(0.3f, 0.3f, 0.3f, 1);
  102.  
  103.     State current;
  104.     current.x = 100;
  105.     current.v = 0;
  106.    
  107.     State previous = current;
  108.  
  109.     float t = 0.0f;
  110.     float dt = 0.1f;
  111.    
  112.     float currentTime = 0.0f;
  113.     float accumulator = 0.0f;
  114.    
  115.     while (!quit)
  116.     {          
  117.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  118.        
  119.         const float newTime = time();
  120.         float deltaTime = newTime - currentTime;
  121.         currentTime = newTime;
  122.        
  123.         if (deltaTime>0.25f)
  124.             deltaTime = 0.25f;
  125.        
  126.         accumulator += deltaTime;
  127.        
  128.         while (accumulator>=dt)
  129.         {
  130.             accumulator -= dt;
  131.             previous = current;
  132.             integrate(current, t, dt);
  133.             t += dt;
  134.         }
  135.        
  136.         State state = interpolate(previous, current, accumulator/dt);
  137.        
  138.         glBegin(GL_POINTS);
  139.         glColor3f(1,1,1);
  140.         glVertex3f(state.x, 0, 0);
  141.         glEnd();
  142.        
  143.         updateDisplay();
  144.     }
  145.    
  146.     closeDisplay();
  147.    
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement