Skip to content
Snippets Groups Projects
Commit 0e939b76 authored by Aron Nikku's avatar Aron Nikku
Browse files

Working collisions simulator 1.0

parent 84ab3b98
No related branches found
No related tags found
No related merge requests found
{
"files.associations": {
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"map": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp"
}
}
\ No newline at end of file
#include <math.h> #include <math.h>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <iostream> #include <iostream>
#include <set>
#include <vector> #include <vector>
int SIZE = 500; int SIZE = 500;
...@@ -11,15 +13,10 @@ double pyt(double x, double y) { ...@@ -11,15 +13,10 @@ double pyt(double x, double y) {
} }
class Vector2 { class Vector2 {
public: public:
double x; double x;
double y; double y;
Vector2() {
x = 0;
y = 0;
}
Vector2 add(Vector2 other) { Vector2 add(Vector2 other) {
Vector2 newVector; Vector2 newVector;
newVector.x = x + other.x; newVector.x = x + other.x;
...@@ -124,10 +121,10 @@ class sphere { ...@@ -124,10 +121,10 @@ class sphere {
newSpeed.x = partOne * partTwoX; newSpeed.x = partOne * partTwoX;
newSpeed.y = partOne * partTwoY; newSpeed.y = partOne * partTwoY;
*/ */
// ALT 2 // ALT 2
double part1 = 2 * other.mass / (mass + other.mass); double part1 = 2 * other.mass / (mass + other.mass);
double part2 = ( (speed.sub(other.speed)).dot(pos.sub(other.pos)) ) / pow((pos.sub(other.pos)).abs(), 2); double part2 = ((speed.sub(other.speed)).dot(pos.sub(other.pos))) / pow((pos.sub(other.pos)).abs(), 2);
Vector2 r1 = pos.sub(other.pos).mult(part1 * part2); Vector2 r1 = pos.sub(other.pos).mult(part1 * part2);
newSpeed = speed.sub(r1); newSpeed = speed.sub(r1);
...@@ -143,13 +140,18 @@ int main() { ...@@ -143,13 +140,18 @@ int main() {
sf::RenderWindow window(sf::VideoMode(SIZE, SIZE), "Physics"); sf::RenderWindow window(sf::VideoMode(SIZE, SIZE), "Physics");
window.setFramerateLimit(60); window.setFramerateLimit(60);
sphere s1(50, 50, 30); std::vector<sphere> sphereList;
sphere s2(350, 200, 30);
s1.speed.x = 1.5; for (int i = 50; i < 500; i += 50) {
s1.speed.y = 0.5; for (int j = 50; j < 500; j += 50) {
s2.speed.x = 0; sphere s(i, j, 20);
s2.speed.y = 0; sphereList.push_back(s);
}
}
sphereList[0].speed.x = 10;
sphereList[0].speed.y = 10;
sphereList[0].mass = 500;
while (window.isOpen()) { while (window.isOpen()) {
sf::Event event; sf::Event event;
...@@ -160,21 +162,33 @@ int main() { ...@@ -160,21 +162,33 @@ int main() {
} }
// MOVE // MOVE
s1.move(); for (int i = 0; i < sphereList.size(); i++) {
s2.move(); sphereList[i].move();
}
// Collide // Collide
Vector2 speed1 = s1.sphereCollision(s2); for (int i = 0; i < sphereList.size(); i++) {
Vector2 speed2 = s2.sphereCollision(s1); for (int j = i; j < sphereList.size(); j++) {
if (i != j) {
s1.setSpeed(speed1); Vector2 speed1 = sphereList[i].sphereCollision(sphereList[j]);
s2.setSpeed(speed2); Vector2 speed2 = sphereList[j].sphereCollision(sphereList[i]);
sphereList[i].setSpeed(speed1);
sphereList[j].setSpeed(speed2);
//std::cout << speed2.x << " " << speed2.y << std::endl;
//std::cout << i << " " << j << std::endl;
//std::cout << sphereList[1].pos.x << " " << sphereList[1].pos.y << std::endl;
}
}
}
window.clear(); window.clear();
// DRAW // DRAW
s1.draw(window); for (int i = 0; i < sphereList.size(); i++) {
s2.draw(window); sphereList[i].draw(window);
}
window.display(); window.display();
} }
......
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment