Skip to content

Commit aff0c7c

Browse files
committed
Particle movement
1 parent 8d258d0 commit aff0c7c

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

include/Actor.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,15 @@ float Actor::getMass() {
2929
float Actor::getRadius() {
3030
return radius;
3131
}
32+
33+
void Actor::setPos(std::array<float, 2> newPos) {
34+
pos = newPos;
35+
}
36+
37+
void Actor::setVelocity(std::array<float, 2> newVelocity) {
38+
velocity = newVelocity;
39+
}
40+
41+
void Actor::setDesiredVelocity(std::array<float, 2> newDesiredVelocity) {
42+
desiredVelocity = newDesiredVelocity;
43+
}

include/Actor.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ class Actor {
1313
float radius;
1414
public:
1515
Actor(std::array<float, 2> pPos, std::array<float, 2> pVelocity, std::array<float, 2> pDesiredVelocity, float pMass, float pRadius);
16+
1617
std::array<float, 2> getPos();
1718
std::array<float, 2> getVelocity();
1819
std::array<float, 2> getDesiredVelocity();
1920
float getMass();
2021
float getRadius();
22+
23+
void setPos(std::array<float, 2> newPos);
24+
void setVelocity(std::array<float, 2> newVelocity);
25+
void setDesiredVelocity(std::array<float, 2> newDesiredVelocity);
2126
};
2227

2328
#endif

src/main.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
const int WIDTH = 8; // metres
77
const int HEIGHT = 6; // metres
88
const int SCALE = 100;
9+
const int DELAY = 10000;
910

1011
void init(SDL_Window* &win, SDL_Renderer* &render, std::vector<Actor> &actors) {
1112
SDL_Init(SDL_INIT_VIDEO);
1213
win = SDL_CreateWindow("SYCL Crowd Simulation", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH * SCALE, HEIGHT * SCALE, SDL_WINDOW_SHOWN);
1314
render = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
1415

15-
actors.push_back(Actor{{1,2}, {3,4}, {5,6}, 50, 0.05});
16+
actors.push_back(Actor{{1, 2}, {0.01, 0}, {0.02, 0.02}, 50, 0.05});
1617
}
1718

1819
void drawCircle(SDL_Renderer* &render, SDL_Point center, int radius, SDL_Color color) {
@@ -28,6 +29,13 @@ void drawCircle(SDL_Renderer* &render, SDL_Point center, int radius, SDL_Color c
2829
}
2930
}
3031

32+
void update(std::vector<Actor> &actors) {
33+
for (auto &actor : actors) {
34+
actor.setPos({actor.getPos()[0] + actor.getVelocity()[0],
35+
actor.getPos()[1] + actor.getVelocity()[1]});
36+
}
37+
}
38+
3139
void draw(SDL_Renderer* &render, std::vector<Actor> actors) {
3240
SDL_SetRenderDrawColor(render, 255, 255, 255, 255);
3341
SDL_RenderClear(render);
@@ -57,6 +65,7 @@ int main() {
5765

5866
draw(render, actors);
5967

68+
int delayCounter = 0;
6069
bool isQuit = false;
6170
SDL_Event event;
6271

@@ -66,8 +75,14 @@ int main() {
6675
isQuit = true;
6776
}
6877
}
69-
70-
//draw(render, actors);
78+
if (delayCounter >= DELAY) {
79+
delayCounter = 0;
80+
update(actors);
81+
draw(render, actors);
82+
}
83+
else {
84+
delayCounter++;
85+
}
7186
}
7287

7388
close(win);

0 commit comments

Comments
 (0)