Advertisement
Guest User

code thingie, use to replace fluidsim2.cpp

a guest
Feb 5th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. #include "base.h"
  2.  
  3. /*#include <fstream>
  4. #include <SFML\Graphics.hpp>
  5. #include <string>
  6. #include <vector>
  7. #include <math.h>
  8.  
  9. const double e=2.71828183;*/
  10.  
  11. const int w=64,h=48; //width and height of the simulation in cells
  12. const int px_per_cell_w=4,px_per_cell_h=4; //width and height of a single cell in pixels
  13. const float change_weight=2.0; //rate of change towards average value of neighbours per step, 1=100% change 0=no change
  14. float grid[w][h]={0};
  15. float grid_new[w][h]={0};
  16. float power=0.005;
  17. sf::Vector2i mouse;
  18.  
  19. float positive(float x)
  20. {
  21. if(x<0) return -x;
  22. return x;
  23. }
  24. float sign(float x) //for use in "to_channel" function
  25. {
  26. if(x>0) return 1;
  27. if(x<0) return -1;
  28. return 0;
  29. }
  30.  
  31. float cast_0to1sharp(float x)
  32. {
  33. return 1.0-1.0/(1.0+sqrt(abs(x))); //useful for mapping a logarithmically large but positive (including 0) range of values to a value from 0 to 1
  34. }
  35. float cast_0to1soft(float x) //useful for mapping a logarithmically large but positive (including 0) range of values to a value from 0 to 1
  36. {
  37. return 1.0-1.0/(1.0+abs(x));
  38. }
  39. float to_channel(float channel) //for use of pixel display
  40. {
  41. return 128.0*sign(channel)*cast_0to1soft(channel)+128.0; //useful for mapping a logarithmically large (but not necessarily positive) range of values from 0 to 1
  42. }
  43.  
  44. float get(int x,int y)
  45. {
  46. int cx=x,cy=y;
  47. if(x<0) cx=w-1;
  48. if(y<0) cy=h-1;
  49. if(x>=w) cx=0;
  50. if(y>=h) cy=0;
  51. return grid[cx][cy];
  52. }
  53.  
  54. int main()
  55. {
  56. sf::Clock clock;
  57. sf::ContextSettings set;
  58. set.antialiasingLevel=8;
  59. sf::RenderWindow window(sf::VideoMode(w*px_per_cell_w,h*px_per_cell_h),"fluid sim");
  60. while(window.isOpen())
  61. {
  62. clock.restart();
  63. sf::Event event;
  64. while (window.pollEvent(event)) {if (event.type == sf::Event::Closed) window.close();}
  65. window.clear(sf::Color::Black);
  66.  
  67. //[do physics step and render]
  68.  
  69. //user interaction
  70. mouse.x=sf::Mouse::getPosition(window).x-1000;//I only add 1000 so that I can evade this weird strangely x-axis-specific glitch
  71. mouse.y=sf::Mouse::getPosition(window).y;
  72. float temp=grid[(mouse.x/px_per_cell_w)%w][(mouse.y/px_per_cell_h)%h];
  73. if(mouse.x>=0 && mouse.x<w*px_per_cell_w && mouse.y>=0 && mouse.y<h*px_per_cell_h)
  74. {
  75. if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
  76. {
  77. grid[(mouse.x/px_per_cell_w)%w][(mouse.y/px_per_cell_h)%h]+=32.0;//+abs(temp)*power;
  78. }
  79. if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Right))
  80. {
  81. grid[(mouse.x/px_per_cell_w)%w][(mouse.y/px_per_cell_h)%h]-=32.0;//+abs(temp)*power;
  82. }
  83. }
  84.  
  85. //simulation and rendering
  86. for(int cy=0;cy<h;cy++)
  87. {
  88. for(int cx=0;cx<w;cx++)
  89. {
  90. //for convenience of reference
  91. float curcell=grid[cx][cy];
  92. float newcell=grid_new[cx][cy];
  93.  
  94. //what is our neighbourhood? (I've organised it so that it is visually obvious what neighbour each term corresponds to)
  95. float neighbour[8]=
  96. {get(cx-1,cy-1),get(cx,cy-1),get(cx+1,cy-1),
  97. get(cx-1,cy), get(cx+1,cy),
  98. get(cx-1,cy+1),get(cx,cy+1),get(cx+1,cy+1)};
  99.  
  100. //calculate the average
  101. float average=0;
  102. for(int i=0;i<8;i++)
  103. {
  104. average+=neighbour[i]/8.0;
  105. }
  106.  
  107. newcell+=(average-curcell)/change_weight; //find the difference between the average and the current value, and divide by change_weight, then change by that
  108. float difference=curcell-newcell;
  109. grid_new[cx][cy]=newcell;
  110. sf::RectangleShape square(sf::Vector2f(px_per_cell_w,px_per_cell_h));
  111.  
  112. float b=to_channel(difference*10.0); //float b=255*(1.0-1.0/(1+curcell));
  113. float g=255.0/(1.0+pow(e,-curcell)); //float g=255*(1.0-1.0/(1+sqrt(curcell*2.0)));
  114. float r=255.0/(1.0+pow(e,curcell)); //float r=255*(1.0-1.0/(1+sqrt(sqrt(curcell*4.0))));
  115. square.setFillColor(sf::Color(r,g,b));
  116. square.setPosition(sf::Vector2f(cx*px_per_cell_w,cy*px_per_cell_h));
  117. window.draw(square);
  118. }
  119. }
  120. //copy new array of grid cells to old one
  121. for(int cy=0;cy<h;cy++)
  122. {
  123. for(int cx=0;cx<w;cx++)
  124. {
  125. grid[cx][cy]=grid_new[cx][cy];
  126. }
  127. }
  128. //window.draw(pwrdisp);
  129. window.display();
  130. //end step
  131. //attempting to sleep a negative amount of time will probably either crash it, cause the universe to stop existing or make a time travel machine...
  132. sf::Time sleep_time=sf::seconds(1.0/60.0)-clock.getElapsedTime(),no_time=sf::seconds(0.0);
  133. if(sleep_time>no_time) sf::sleep(sleep_time);
  134. }
  135. return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement