Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include <unistd.h>
- #include <cstdlib>
- #include <math.h>
- using namespace std;
- using namespace sf;
- #define Height 1000
- #define Width 1500
- #define dt 0.01
- #define G 9.8
- #define MAX_VY 15
- #define u_water_0 (1.79*0.001)
- #define u_water_25 (0.89*0.001)
- #define u_water_100 (0.28*0.001)
- #define u_glycerin (934*0.001)
- #define u_mercury (1.554*0.001)
- #define u_tar 10000000
- #define u 0.00119
- struct Circle {
- double R = rand()%26;
- Vector2f pos;
- Vector2f vel;
- double ro = (rand()%101 + 50)/(double)100;
- double V = (double)4/3*M_PI*R*R*R;
- double m = V*ro;
- bool sy = true, sx = true;
- int r= rand()%255;
- int g= rand()%255;
- int b= rand()%255;
- };
- void bouncing(Circle &circle) {
- Vector2f F(0, circle.m * G);
- Rect<float> fluid0(0, 0, Width/2 - circle.R, Height/2 - circle.R);
- if(fluid0.contains(circle.pos))
- F += circle.vel*(float)-6*(float)M_PI*(float)circle.R*(float)u_glycerin*(float)100;
- Vector2f a = F/(float)circle.m;
- // double ax = (F)/circle.m;
- // circle.vy = circle.vy + (ay)*dt;
- // circle.vx = circle.vx + (ax)*dt;
- circle.vel += a*(float)dt;
- circle.pos += circle.vel*(float)dt;
- //circle.x = circle.x + circle.vx*dt;
- //circle.y = circle.y + circle.vy*dt;
- /* if(circle.x<0 || circle.x > Width/2){
- double Dx = -6*M_PI*circle.R*circle.vx*u_glycerin*100;
- double Dy = -6*M_PI*circle.R*circle.vy*u_glycerin*100;
- double ay = (Dy+F)/circle.m;
- double ax = (Dx+F)/circle.m;
- circle.vy = circle.vy + (ay)*dt;
- circle.vx = circle.vx + (ax)*dt;
- circle.x = circle.x + circle.vx*dt;
- circle.y = circle.y + circle.vy*dt;
- }*/
- CircleShape shape(circle.R);
- shape.setPosition(circle.pos);
- // Rect<float> window( 0, 0, Width - circle.R, Height - circle.R);
- // if(!window.contains(circle.pos)) {
- // circle.vel = -circle.vel;
- // circle.sy = false;
- // } else circle.sy = true;
- if(circle.pos.y<0 || circle.pos.y > Height-2*circle.R) {
- if(circle.sy){
- circle.vel.y *= -1;
- circle.sy = false;
- }
- } else circle.sy = true;
- if(circle.pos.x<0 || circle.pos.x > Width-2*circle.R) {
- if(circle.sx){
- circle.vel.x *= -1;
- circle.sx = false;
- }
- } else circle.sx = true;
- }
- int main()
- {
- sf::RenderWindow window(sf::VideoMode(Width, Height), "Bouncing balls");
- srand(time(NULL));
- int N = 50; //amount of balls
- Circle balls[N];
- RectangleShape fluid0_box(Vector2f(Width/2,Height/2));
- fluid0_box.setPosition(0,0);
- fluid0_box.setFillColor(Color(rand()%256, rand()%256, rand()%256));
- // Vector2f pos(Width/2-R, Height/2-R);
- //pos.x = Width/2-R;
- // pos.y = Height/2-R;
- for(int i=0; i<N; i++) {
- // balls[i].x=rand()%(int)(Width-2*balls[i].R);
- // balls[i].y=rand()%(int)(Height-2*balls[i].R);
- balls[i].pos = Vector2f(rand()%(int)(Width-2*balls[i].R), rand()%(int)(Height-2*balls[i].R));
- balls[i].vel = Vector2f(rand()%11-5, 0);
- }
- while (window.isOpen()){
- sf::Event event;
- while (window.pollEvent(event)){
- if (event.type == sf::Event::Closed)
- window.close();
- if( event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape )
- window.close();
- }
- window.clear();
- window.draw(fluid0_box);
- window.draw(fluid1_box);
- window.draw(fluid2_box);
- for(int i=0; i<N; i++) {
- bouncing(balls[i]);
- CircleShape shape(balls[i].R);
- shape.setFillColor(Color(balls[i].r, balls[i].g, balls[i].b));
- // shape.setPosition(balls[i].x, balls[i].y);
- shape.setPosition(balls[i].pos);
- window.draw(shape);
- }
- window.display();
- usleep(500);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement