• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Jan 12th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <SFML/Graphics.hpp>
2. #include <iostream>
3. #include <unistd.h>
4. #include <cstdlib>
5. #include <math.h>
6.
7. using namespace std;
8. using namespace sf;
9.
10. #define Height 1000
11. #define Width 1500
12.
13. #define dt 0.01
14. #define G 9.8
15.
16. #define MAX_VY 15
17. #define u_water_0 (1.79*0.001)
18. #define u_water_25 (0.89*0.001)
19. #define u_water_100 (0.28*0.001)
20. #define u_glycerin (934*0.001)
21. #define u_mercury (1.554*0.001)
22. #define u_tar 10000000
23. #define u 0.00119
24.
25.
26. struct Circle {
27.     double R = rand()%26;
28.     Vector2f pos;
29.     Vector2f vel;
30.     double ro = (rand()%101 + 50)/(double)100;
31.     double V = (double)4/3*M_PI*R*R*R;
32.     double m = V*ro;
33.     bool sy = true, sx = true;
34.     int r= rand()%255;
35.     int g= rand()%255;
36.     int b= rand()%255;
37. };
38.
39.
40.
41. void bouncing(Circle &circle) {
42.     Vector2f F(0, circle.m * G);
43.
44.     Rect<float> fluid0(0, 0, Width/2 - circle.R, Height/2 - circle.R);
45.
46.     if(fluid0.contains(circle.pos))
47.         F += circle.vel*(float)-6*(float)M_PI*(float)circle.R*(float)u_glycerin*(float)100;
48.
49.     Vector2f a = F/(float)circle.m;
50.
51.     // double ax = (F)/circle.m;
52.
53.     // circle.vy = circle.vy + (ay)*dt;
54.     // circle.vx = circle.vx + (ax)*dt;
55.     circle.vel += a*(float)dt;
56.     circle.pos += circle.vel*(float)dt;
57.
58.     //circle.x = circle.x + circle.vx*dt;
59.     //circle.y = circle.y + circle.vy*dt;
60.
61. /*    if(circle.x<0 || circle.x > Width/2){
62.         double Dx = -6*M_PI*circle.R*circle.vx*u_glycerin*100;
63.         double Dy = -6*M_PI*circle.R*circle.vy*u_glycerin*100;
64.         double ay = (Dy+F)/circle.m;
65.         double ax = (Dx+F)/circle.m;
66.         circle.vy = circle.vy + (ay)*dt;
67.         circle.vx = circle.vx + (ax)*dt;
68.
69.         circle.x = circle.x + circle.vx*dt;
70.         circle.y = circle.y + circle.vy*dt;
71.     }*/
72.     CircleShape shape(circle.R);
73.     shape.setPosition(circle.pos);
74.
75.     // Rect<float> window( 0, 0, Width - circle.R, Height - circle.R);
76.     // if(!window.contains(circle.pos)) {
77.     //     circle.vel = -circle.vel;
78.     //     circle.sy = false;
79.     // } else circle.sy = true;
80.
81.     if(circle.pos.y<0 || circle.pos.y > Height-2*circle.R) {
82.         if(circle.sy){
83.             circle.vel.y *= -1;
84.             circle.sy = false;
85.         }
86.     } else circle.sy = true;
87.
88.     if(circle.pos.x<0 || circle.pos.x > Width-2*circle.R) {
89.         if(circle.sx){
90.             circle.vel.x *= -1;
91.             circle.sx = false;
92.         }
93.     } else circle.sx = true;
94. }
95.
96.
97.
98. int main()
99. {
100.     sf::RenderWindow window(sf::VideoMode(Width, Height), "Bouncing balls");
101.
102.     srand(time(NULL));
103.     int N = 50; //amount of balls
104.     Circle balls[N];
105.
106.     RectangleShape fluid0_box(Vector2f(Width/2,Height/2));
107.     fluid0_box.setPosition(0,0);
108.     fluid0_box.setFillColor(Color(rand()%256, rand()%256, rand()%256));
109.
110.     // Vector2f pos(Width/2-R, Height/2-R);
111.     //pos.x = Width/2-R;
112.     // pos.y = Height/2-R;
113.
114.     for(int i=0; i<N; i++) {
115.         // balls[i].x=rand()%(int)(Width-2*balls[i].R);
116.         // balls[i].y=rand()%(int)(Height-2*balls[i].R);
117.         balls[i].pos = Vector2f(rand()%(int)(Width-2*balls[i].R), rand()%(int)(Height-2*balls[i].R));
118.         balls[i].vel = Vector2f(rand()%11-5, 0);
119.     }
120.
121.
122.     while (window.isOpen()){
123.
124.         sf::Event event;
125.         while (window.pollEvent(event)){
126.             if (event.type == sf::Event::Closed)
127.                 window.close();
128.
129.             if( event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape )
130.                 window.close();
131.         }
132.
133.         window.clear();
134.         window.draw(fluid0_box);
135.         window.draw(fluid1_box);
136.         window.draw(fluid2_box);
137.         for(int i=0; i<N; i++) {
138.             bouncing(balls[i]);
139.             CircleShape shape(balls[i].R);
140.             shape.setFillColor(Color(balls[i].r, balls[i].g, balls[i].b));
141.             // shape.setPosition(balls[i].x, balls[i].y);
142.             shape.setPosition(balls[i].pos);
143.             window.draw(shape);
144.         }
145.
146.         window.display();
147.         usleep(500);
148.     }
149.
150.     return 0;
151. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?