Skip to content

Commit

Permalink
async velocity updates + fixed dangling references in the vector class
Browse files Browse the repository at this point in the history
  • Loading branch information
Feeeeddmmmeee committed May 21, 2022
1 parent 41c6636 commit c7d501e
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 28 deletions.
20 changes: 14 additions & 6 deletions GravitySim/src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ Game::Game(const char* title, int xpos, int ypos, int width, int height, bool fu
}//*/

// Orbiting planets
/*this->universe.addPlanet(new Planet(100000, Vector(width/2, height/2), Vector(0 + 2, -0.3), this->renderer, this->planet));
this->universe.addPlanet(new Planet(1000, Vector(width/2, height/2 - height/4), Vector(7 + 2, -0.3), this->renderer, this->planet));
this->universe.addPlanet(new Planet(1000, Vector(width/2, height/2 + height/4), Vector(-7 + 2, -0.3), this->renderer, this->planet));//*/
/*this->universe.addPlanet(new Planet(100000, Vector(width/2, height/2), Vector(0 , 0), this->renderer, this->planet));
this->universe.addPlanet(new Planet(1000, Vector(width/2, height/2 - height/4), Vector(7 , 0), this->renderer, this->planet));
this->universe.addPlanet(new Planet(1000, Vector(width/2, height/2 + height/4), Vector(-7 , 0), this->renderer, this->planet));//*/

this->ui = new UIManager();
this->ui->buttons.push_back(new UIElement(Vector(width - 34 - 5, 10), this->renderer, "res/gfx/gui_exit.png", ID::EXIT));
Expand All @@ -87,7 +87,7 @@ void Game::update()
SDL_GetMouseState(&x, &y);
Vector mouseVec = Vector(x, y);

this->universe.move(mouseVec - this->previousMousePos);
this->universe.move(mouseVec - this->previousMousePos, this->universe.getMutexPtr());
this->previousMousePos = mouseVec;
}

Expand Down Expand Up @@ -178,13 +178,21 @@ void Game::handleEvents()
break;

case SDLK_RIGHT:
this->universe.move(this->universe.getPlanetPosition(this->index) * -1 + Vector(400, 400));
if (index >= this->universe.size()) index = 0;
else if (index < 0) index = this->universe.size() - 1;

this->universe.move(this->universe.getPlanetPosition(this->index) * -1, this->universe.getMutexPtr());
this->universe.move(Vector(400, 400), this->universe.getMutexPtr());
this->index++;

break;

case SDLK_LEFT:
this->universe.move(this->universe.getPlanetPosition(this->index) * -1 + Vector(400, 400));
if (index < 0) index = this->universe.size() - 1;
else if (index >= this->universe.size()) index = 0;

this->universe.move(this->universe.getPlanetPosition(this->index) * -1, this->universe.getMutexPtr());
this->universe.move(Vector(400, 400), this->universe.getMutexPtr());
this->index--;

break;
Expand Down
25 changes: 18 additions & 7 deletions GravitySim/src/Planet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Planet::Planet(double mass, Vector position, Vector velocity, SDL_Renderer* renderer, SDL_Texture* texture)
{
this->radius = std::cbrt(4 * mass / DENSITY / PI / 3);
this->acceleration = Vector();
this->mass = mass;
this->position = position;
this->velocity = velocity;
Expand All @@ -16,24 +15,30 @@ void Planet::destroyTexture()
SDL_DestroyTexture(this->texture);
}

void Planet::updateVelocity(std::vector<Planet*>& others)
void Planet::updateVelocity(std::vector<Planet*>* vec, std::mutex* m)
{
this->acceleration = Vector();
auto& others = *vec;
Vector acceleration;

for (auto& other : others)
{
if (other == this) continue;

if (sqrt(pow((this->position.x + this->radius - other->position.x - other->radius), 2) + pow((this->position.y + this->radius - other->position.y - other->radius), 2)) < this->radius + other->radius)
{

std::lock_guard<std::mutex> lock(*m);
if (this->mass > other->mass)
{
this->mass += other->mass;
this->velocity += other->velocity * other->mass / this->mass;

Planet* p = other;
delete p;

others.erase(std::remove(others.begin(), others.end(), other), others.end());
this->radius = std::cbrt(4 * this->mass / DENSITY / PI / 3);

return;
}
else
Expand All @@ -43,25 +48,31 @@ void Planet::updateVelocity(std::vector<Planet*>& others)

Planet* p = this;
delete p;

others.erase(std::remove(others.begin(), others.end(), this), others.end());
other->radius = std::cbrt(4 * other->mass / DENSITY / PI / 3);

return;
}
}

Vector posV = other->position - this->position;
double distance = posV.Lenght();
if (!distance) return;
Vector mag = posV / distance;

double force = this->mass * other->mass * G / pow(distance, 2);
this->acceleration += mag * force / this->mass;
this->radius = std::cbrt(4 * this->mass / DENSITY / PI / 3);

acceleration += mag * force / this->mass;
}

this->velocity += this->acceleration;
std::lock_guard<std::mutex> lock(*m);
this->velocity += acceleration;
}

void Planet::updatePosition()
void Planet::updatePosition(std::mutex* m)
{
std::lock_guard<std::mutex> lock(*m);
this->position += this->velocity;
}

Expand Down
9 changes: 6 additions & 3 deletions GravitySim/src/Planet.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#ifndef PLANET_H
#define PLANET_H

#define ASYNC 1

#include <vector>
#include <mutex>

#include "SDL_image.h"

Expand All @@ -14,7 +17,7 @@ constexpr auto G = 0.1;
struct Planet
{
double radius, mass;
Vector position, velocity, acceleration;
Vector position, velocity;

SDL_Rect destRect;
SDL_Texture* texture;
Expand All @@ -23,8 +26,8 @@ struct Planet

void destroyTexture();

void updateVelocity(std::vector<Planet*>& others);
void updatePosition();
void updateVelocity(std::vector<Planet*>* others, std::mutex* m);
void updatePosition(std::mutex* m);

void render(SDL_Renderer* renderer, float zoom);
};
Expand Down
51 changes: 48 additions & 3 deletions GravitySim/src/Universe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,42 @@ void Universe::update()
{
for (auto& planet : this->planets)
{
planet->updateVelocity(this->planets);
#if ASYNC
this->futures.push_back(std::async(std::launch::async, [planet, this]() -> void
{
planet->updateVelocity(&this->planets, &this->mutex);
}
));

#else
planet->updateVelocity(&this->planets, &this->mutex);

#endif
}
#if ASYNC
this->futures.clear();

#endif

for (auto& planet : this->planets)
{
planet->updatePosition();
#if ASYNC
this->futures.push_back(std::async(std::launch::async, [planet, this]() -> void
{
planet->updatePosition(&this->mutex);
}
));

#else
planet->updatePosition(&this->mutex);

#endif
}

#if ASYNC
this->futures.clear();

#endif
}

void Universe::render(SDL_Renderer* renderer, float zoom)
Expand All @@ -36,12 +65,28 @@ void Universe::restart()
this->planets.clear();
}

void Universe::move(Vector deltaPos)
void Universe::move(Vector deltaPos, std::mutex* m)
{
for (auto& planet : planets)
{
#if ASYNC
this->futures.push_back(std::async(std::launch::async, [planet, deltaPos, m]() -> void
{
std::lock_guard<std::mutex> lock(*m);
planet->position += deltaPos;
}
));

#else
planet->position += deltaPos;

#endif
}

#if ASYNC
this->futures.clear();

#endif
}

Vector Universe::getPlanetPosition(int& index)
Expand Down
9 changes: 8 additions & 1 deletion GravitySim/src/Universe.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef UNIVERSE_H
#define UNIVERSE_H

#include <future>

#include "Planet.h"

class Universe
Expand All @@ -10,14 +12,19 @@ class Universe
void update();
void render(SDL_Renderer* renderer, float zoom);
void restart();
void move(Vector deltaPos);
void move(Vector deltaPos, std::mutex* m);

size_t size() { return this->planets.size(); }

Vector getPlanetPosition(int& index);
std::mutex* getMutexPtr() { return &this->mutex; }

private:
std::vector<Planet*> planets = {};

std::vector<std::future<void>> futures;
std::mutex mutex;

};

#endif
8 changes: 4 additions & 4 deletions GravitySim/src/Vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,22 @@ Vector& Vector::Divide(const Vector& vec)
return *this;
}

Vector& operator+(Vector vec1, const Vector vec2)
Vector operator+(Vector vec1, const Vector vec2)
{
return vec1.Add(vec2);
}

Vector& operator-(Vector vec1, const Vector vec2)
Vector operator-(Vector vec1, const Vector vec2)
{
return vec1.Substract(vec2);
}

Vector& operator*(Vector vec1, const Vector vec2)
Vector operator*(Vector vec1, const Vector vec2)
{
return vec1.Multiply(vec2);
}

Vector& operator/(Vector vec1, const Vector vec2)
Vector operator/(Vector vec1, const Vector vec2)
{
return vec1.Divide(vec2);
}
Expand Down
8 changes: 4 additions & 4 deletions GravitySim/src/Vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ struct Vector
Vector& Multiply(const Vector& vec);
Vector& Divide(const Vector& vec);

friend Vector& operator+(Vector vec1, const Vector vec2);
friend Vector& operator-(Vector vec1, const Vector vec2);
friend Vector& operator*(Vector vec1, const Vector vec2);
friend Vector& operator/(Vector vec1, const Vector vec2);
friend Vector operator+(Vector vec1, const Vector vec2);
friend Vector operator-(Vector vec1, const Vector vec2);
friend Vector operator*(Vector vec1, const Vector vec2);
friend Vector operator/(Vector vec1, const Vector vec2);

Vector& operator+=(const Vector vec);
Vector& operator-=(const Vector vec);
Expand Down
5 changes: 5 additions & 0 deletions GravitySim/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ int main(int argc, char* argv[])

//ShowWindow(GetConsoleWindow(), SW_HIDE);

#if ASYNC
std::cout << "Async updates enabled!" << std::endl;

#endif

while (game->running())
{

Expand Down

0 comments on commit c7d501e

Please sign in to comment.