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 <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();
}
......
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