-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball_collision.cpp
176 lines (150 loc) · 5.67 KB
/
ball_collision.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <SFML/Graphics.hpp>
#include <math.h>
#include <iostream>
#include "Vector2.h"
const int speed = 400;
using namespace sf;
class MaterialPoint{
public:
vec::Vector2 GetImpulse() const{
return vec::Vector2(velocity.x * mass, velocity.y * mass);
}
vec::Vector2 velocity;
vec::Vector2 acceleration;
vec::Vector2 initialPosition;
float mass;
float timeOfCreation;
};
class Ball: public MaterialPoint{
public:
float radius;
int type;
CircleShape circle;
};
void setBall(Ball &ball, RenderWindow &window, Time &time){
CircleShape circle(ball.mass*10);
circle.setOrigin(circle.getRadius(), circle.getRadius());
ball.circle = circle;
ball.circle.setPosition(Mouse::getPosition(window).x, Mouse::getPosition(window).y);
ball.initialPosition.setPosition(Mouse::getPosition(window).x, Mouse::getPosition(window).y);
ball.circle.setFillColor(Color::Red);
ball.velocity = vec::Vector2(speed, speed*(-1));
ball.timeOfCreation = time.asSeconds();
}
bool isInsideOfBorder(Ball ball, RenderWindow &window, int sideOfBorder){ // 1
switch (sideOfBorder){ // 4 2
case 0:{ // 3
if(isInsideOfBorder(ball, window, 1) && isInsideOfBorder(ball, window, 2) && isInsideOfBorder(ball, window, 3) && isInsideOfBorder(ball, window, 4))
return true;
else
return false;
}
case 1:{
if(ball.circle.getPosition().y - ball.circle.getRadius() > 0)
return true;
else
return false;
}
case 2:{
if(ball.circle.getPosition().x + ball.circle.getRadius() < window.getSize().x)
return true;
else
return false;
}
case 3:{
if(ball.circle.getPosition().y + ball.circle.getRadius() < window.getSize().y)
return true;
else
return false;
}
case 4:{
if(ball.circle.getPosition().x - ball.circle.getRadius() > 0)
return true;
else
return false;
}
default:
return false;
}
}
void reflectWall(Ball &ball, RenderWindow &window, Time time){
if(!isInsideOfBorder(ball, window, 1) || !isInsideOfBorder(ball, window, 3)) {
ball.velocity.y *= -1;
ball.initialPosition = vec::Vector2(ball.circle.getPosition().x, ball.circle.getPosition().y);
ball.timeOfCreation = time.asSeconds();
}
if(!isInsideOfBorder(ball, window, 2) || !isInsideOfBorder(ball, window, 4)){
ball.velocity.x *= -1;
ball.initialPosition = vec::Vector2(ball.circle.getPosition().x, ball.circle.getPosition().y);
ball.timeOfCreation = time.asSeconds();
}
}
vec::Vector2 getdP(Ball ball_1, Ball ball_2) {
vec::Vector2 d = vec::Vector2(ball_1.circle.getPosition().x-ball_2.circle.getPosition().x,
ball_1.circle.getPosition().y-ball_2.circle.getPosition().y);
if((d.length() <= ball_1.circle.getRadius() + ball_2.circle.getRadius()) && ((ball_1.velocity - ball_2.velocity)*d <= 0))
return (2 * (ball_1.velocity-ball_2.velocity) / (1/ball_1.mass+1/ball_2.mass) * d.norm()) * d.norm();
else
return vec::Vector2(0,0);
}
void reflectBall(Ball &ball_1, Ball &ball_2, Time time){
vec::Vector2 dp = getdP(ball_1, ball_2);
ball_2.velocity += dp/ball_2.mass;
ball_1.velocity -= dp/ball_1.mass;
ball_1.initialPosition = vec::Vector2(ball_1.circle.getPosition().x, ball_1.circle.getPosition().y);
ball_2.initialPosition = vec::Vector2(ball_2.circle.getPosition().x, ball_2.circle.getPosition().y);
ball_1.timeOfCreation = time.asSeconds();
ball_2.timeOfCreation = time.asSeconds();
}
int main()
{
int ballCount = 0;
RenderWindow window(VideoMode(1200, 1000), "SFML Window"/*, sf::Style::Fullscreen*/);
//window.setFramerateLimit(120);
Event event;
Clock clock;
Clock clock1;
std::vector<Ball> balls;
float start = 0;
while(window.isOpen()){
Time time = clock.getElapsedTime();
Time time1 = clock1.getElapsedTime();
Ball ball;
while(window.pollEvent(event)){
if(event.type == Event::Closed)
window.close();
if(event.type == event.MouseButtonPressed && event.mouseButton.button == Mouse::Left)
start = time1.asSeconds();
if(event.type == event.MouseButtonReleased && event.mouseButton.button == Mouse::Left){
ball.mass = time1.asSeconds() - start;
setBall(ball, window, time);
balls.push_back(ball);
ballCount++;
}
}
if(Mouse::isButtonPressed(Mouse::Left)){
ball.mass = time1.asSeconds() - start;
setBall(ball, window, time);
}
for(auto &i: balls) {
i.circle.setPosition(i.initialPosition.x + i.velocity.x * (time.asSeconds() - i.timeOfCreation),
i.initialPosition.y + i.velocity.y * (time.asSeconds() - i.timeOfCreation));
reflectWall(i, window, time);
}
for(int i = 0; i < ballCount; i++) {
for(int j = i + 1; j < ballCount; j++)
reflectBall(balls[i], balls[j], time);
}
window.clear();
for(auto &i : balls)
window.draw(i.circle);
window.draw(ball.circle);
window.display();
}
return 0;
}
// Launching this prm:
//
//g++ -c main.cpp
//g++ main.o -o sfml-app -lsfml-graphics -lsfml-window -lsfml-system
//./sfml-app