diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..26f8186354b74dc59bc5f37cc290d70b65981cd0 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,50 @@ +{ + "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 diff --git a/collision3.cpp b/Collisions/collision3.cpp similarity index 72% rename from collision3.cpp rename to Collisions/collision3.cpp index 4e11c6f23da0b8ae128756fac00c58472e92f516..abd00bd4a1b8daf3f6f414196d898af749139e3b 100644 --- a/collision3.cpp +++ b/Collisions/collision3.cpp @@ -1,6 +1,8 @@ #include <math.h> + #include <SFML/Graphics.hpp> #include <iostream> +#include <set> #include <vector> int SIZE = 500; @@ -11,15 +13,10 @@ double pyt(double x, double y) { } class Vector2 { - public: + public: double x; double y; - Vector2() { - x = 0; - y = 0; - } - Vector2 add(Vector2 other) { Vector2 newVector; newVector.x = x + other.x; @@ -124,10 +121,10 @@ class sphere { newSpeed.x = partOne * partTwoX; newSpeed.y = partOne * partTwoY; */ - + // ALT 2 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); newSpeed = speed.sub(r1); @@ -143,13 +140,18 @@ int main() { sf::RenderWindow window(sf::VideoMode(SIZE, SIZE), "Physics"); window.setFramerateLimit(60); - sphere s1(50, 50, 30); - sphere s2(350, 200, 30); + std::vector<sphere> sphereList; - s1.speed.x = 1.5; - s1.speed.y = 0.5; - s2.speed.x = 0; - s2.speed.y = 0; + for (int i = 50; i < 500; i += 50) { + for (int j = 50; j < 500; j += 50) { + sphere s(i, j, 20); + sphereList.push_back(s); + } + } + + sphereList[0].speed.x = 10; + sphereList[0].speed.y = 10; + sphereList[0].mass = 500; while (window.isOpen()) { sf::Event event; @@ -160,21 +162,33 @@ int main() { } // MOVE - s1.move(); - s2.move(); + for (int i = 0; i < sphereList.size(); i++) { + sphereList[i].move(); + } // Collide - Vector2 speed1 = s1.sphereCollision(s2); - Vector2 speed2 = s2.sphereCollision(s1); - - s1.setSpeed(speed1); - s2.setSpeed(speed2); + for (int i = 0; i < sphereList.size(); i++) { + for (int j = i; j < sphereList.size(); j++) { + if (i != j) { + Vector2 speed1 = sphereList[i].sphereCollision(sphereList[j]); + 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(); // DRAW - s1.draw(window); - s2.draw(window); + for (int i = 0; i < sphereList.size(); i++) { + sphereList[i].draw(window); + } window.display(); } diff --git a/a.out b/a.out index 4199d47210215ee1b29a6d47325e267108fb5795..da8303c34545ba10a3bfededaed0bd323ae6a0db 100755 Binary files a/a.out and b/a.out differ