Skip to content

Commit 230c8a3

Browse files
committed
Working model, commit before making improvements
1 parent a281573 commit 230c8a3

File tree

6 files changed

+133
-75
lines changed

6 files changed

+133
-75
lines changed

external/Actor.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
#include <array>
44
#include <sycl/sycl.hpp>
55

6-
Actor::Actor(vecType pPos, vecType pVelocity, vecType pDesiredVelocity, vecType pDestination, float pMass, float pRadius) {
6+
Actor::Actor(vecType pPos, vecType pVelocity, vecType pDesiredVelocity, vecType pDestination, float pMass, float pRadius, bool pAtDestination, std::array<int, 3> pColor) {
77
pos = pPos;
88
velocity = pVelocity;
99
desiredVelocity = pDesiredVelocity;
1010
destination = pDestination;
1111
mass = pMass;
1212
radius = pRadius;
13+
atDestination = pAtDestination;
14+
color = pColor;
1315
}
1416

1517
SYCL_EXTERNAL vecType Actor::getPos() {
@@ -36,6 +38,14 @@ SYCL_EXTERNAL float Actor::getRadius() {
3638
return radius;
3739
}
3840

41+
SYCL_EXTERNAL bool Actor::getAtDestination() {
42+
return atDestination;
43+
}
44+
45+
SYCL_EXTERNAL std::array<int, 3> Actor::getColor() {
46+
return color;
47+
}
48+
3949
SYCL_EXTERNAL void Actor::setPos(vecType newPos) {
4050
pos = newPos;
4151
}
@@ -51,3 +61,7 @@ SYCL_EXTERNAL void Actor::setDesiredVelocity(vecType newDesiredVelocity) {
5161
SYCL_EXTERNAL void Actor::setDestination(vecType newDestination) {
5262
destination = newDestination;
5363
}
64+
65+
SYCL_EXTERNAL void Actor::setAtDestination(bool param) {
66+
atDestination = param;
67+
}

external/Actor.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,26 @@ class Actor {
1616
vecType destination;
1717
float mass;
1818
float radius;
19+
bool atDestination;
20+
std::array<int, 3> color;
21+
1922
public:
20-
Actor(vecType pPos, vecType pVelocity, vecType pDesiredVelocity, vecType pDestination, float pMass, float pRadius);
23+
Actor(vecType pPos, vecType pVelocity, vecType pDesiredVelocity, vecType pDestination, float pMass, float pRadius, bool pAtDestination, std::array<int, 3> pColor);
2124

2225
SYCL_EXTERNAL vecType getPos();
2326
SYCL_EXTERNAL vecType getVelocity();
2427
SYCL_EXTERNAL vecType getDesiredVelocity();
2528
SYCL_EXTERNAL vecType getDestination();
2629
SYCL_EXTERNAL float getMass();
2730
SYCL_EXTERNAL float getRadius();
31+
SYCL_EXTERNAL bool getAtDestination();
32+
SYCL_EXTERNAL std::array<int, 3> getColor();
2833

2934
SYCL_EXTERNAL void setPos(vecType newPos);
3035
SYCL_EXTERNAL void setVelocity(vecType newVelocity);
3136
SYCL_EXTERNAL void setDesiredVelocity(vecType newDesiredVelocity);
3237
SYCL_EXTERNAL void setDestination(vecType newDestination);
38+
SYCL_EXTERNAL void setAtDestination(bool param);
3339
};
3440

3541
#endif

external/DifferentialEq.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,24 @@
99

1010
SYCL_EXTERNAL void differentialEq(int z, sycl::accessor<Actor, 1, sycl::access::mode::read_write> actors, sycl::accessor<std::array<vecType, 2>, 1, sycl::access::mode::read> walls, sycl::stream out) {
1111
Actor* i = &actors[z];
12+
13+
auto pos = i->getPos();
1214

1315
auto mi = i->getMass();
14-
auto v0i = 1;
16+
auto v0i = 2;
1517
auto e0i = normalize(getDirectionVector(i->getPos(), i->getDestination()));
1618
auto vi = i->getVelocity();
17-
out << "Personal impulse parts: mi = " << mi << ", v0i = " << v0i << ", e0i = (" << e0i[0] << ", " << e0i[1] << "), vi = (" << vi[0] << ", " << vi[1] << ")" << sycl::endl;
1819

1920
auto personalImpulse = mi * (((v0i * e0i) - vi) / Ti);
20-
out << "Personal Impulse: (" << personalImpulse[0] << ", " << personalImpulse[1] << ") " << z << sycl::endl << sycl::endl;
2121

2222
vecType peopleForces = {0, 0};
2323
for (int x = 0; x < actors.size(); x++) {
2424
auto j = actors[x];
25-
if (z != x) {
25+
if (z != x && !j.getAtDestination()) {
2626
auto rij = j.getRadius() + i->getRadius();
2727
auto dij = magnitude(i->getPos() - j.getPos());
2828
auto nij = (i->getPos() - j.getPos()) / dij;
29-
auto tij = nij;
30-
tij = {-nij[1], nij[0]};
29+
vecType tij = {-nij[1], nij[0]};
3130
auto g = dij > rij ? 0 : rij - dij;
3231
auto deltavtij = dotProduct((j.getVelocity() - i->getVelocity()), tij);
3332

@@ -39,26 +38,27 @@ SYCL_EXTERNAL void differentialEq(int z, sycl::accessor<Actor, 1, sycl::access::
3938
for (int x = 0; x < walls.size(); x++) {
4039
auto w = walls[x];
4140
auto ri = i->getRadius();
42-
auto diw = distanceToWall(i->getPos(), w);
41+
auto diw = distanceToWall(pos, w);
4342
auto g = diw > ri ? 0 : ri - diw;
44-
auto niw = getniw(i->getPos(), w);
45-
auto tiw = niw;
46-
tiw = {-niw[1], niw[0]};
43+
auto niw = getniw(pos, w);
44+
vecType tiw = {-niw[1], niw[0]};
4745

4846
wallForces += (Ai * exp((ri - diw) / Bi) + K1 * g) * niw - (K2 * g * dotProduct(vi, tiw) * tiw);
4947
}
5048

51-
vecType forceSum = {0, 0};
52-
forceSum += personalImpulse;
53-
forceSum += peopleForces;
54-
forceSum += wallForces;
49+
vecType forceSum = personalImpulse + peopleForces + wallForces;
5550
auto acceleration = forceSum / mi;
5651

57-
out << "People Forces: (" << peopleForces[0] << ", " << peopleForces[1] << ") " << z << sycl::endl;
58-
out << "Wall Forces: (" << wallForces[0] << ", " << wallForces[1] << ") " << z << sycl::endl;
59-
out << "Acceleration: (" << acceleration[0] << ", " << acceleration[1] << ") " << z << sycl::endl;
60-
out << "-----------------------" << sycl::endl;
52+
//out << "People Forces: (" << peopleForces[0] << ", " << peopleForces[1] << ") " << z << sycl::endl;
53+
//out << "Wall Forces: (" << wallForces[0] << ", " << wallForces[1] << ") " << z << sycl::endl;
54+
//out << "Acceleration: (" << acceleration[0] << ", " << acceleration[1] << ") " << z << sycl::endl;
55+
//out << "-----------------------" << sycl::endl;
56+
57+
i->setVelocity(i->getVelocity() + forceSum * 0.001);
58+
i->setPos(i->getPos() + i->getVelocity() * 0.001);
6159

62-
i->setVelocity(i->getVelocity() + forceSum * 0.01);
63-
i->setPos(i->getPos() + i->getVelocity() * 0.01);
60+
if (i->getPos()[0] <= i->getDestination()[0] + 0.01 && i->getPos()[0] >= i->getDestination()[0] - 0.01
61+
&& i->getPos()[1] <= i->getDestination()[1] + 0.01 && i->getPos()[1] >= i->getDestination()[1] - 0.01) {
62+
i->setAtDestination(true);
63+
}
6464
}

external/DifferentialEq.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "MathHelper.hpp"
1111
#include "VectorMaths.hpp"
1212

13-
constexpr float Ai = 2000;
13+
constexpr float Ai = 200;
1414
constexpr float Bi = 0.08;
1515
constexpr float K1 = 125000;
1616
constexpr float K2 = 240000;

external/MathHelper.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,41 @@ SYCL_EXTERNAL float distance(vecType from, vecType to) {
3232
}
3333

3434
SYCL_EXTERNAL float distanceToWall(vecType point, std::array<vecType, 2> wall) {
35-
float lSquared = pow(magnitude(wall[1] - wall[0]), 2);
35+
float lSquared = pow(magnitude(wall[1] - wall[0]), 2) +0.01f;
3636
if (lSquared == 0.0) {
3737
return distance(point, wall[0]);
3838
}
3939
float t = std::max(float(0.0), std::min(float(1.0), dotProduct(point - wall[0], wall[1] - wall[0]) / lSquared));
4040
auto projection = wall[0] + t * (wall[1] - wall[0]);
41-
return distance(point, projection);
41+
return distance(point, projection);
4242
}
4343

4444
SYCL_EXTERNAL vecType normalize(vecType inp) {
4545
return inp / magnitude(inp);
4646
}
4747

4848
SYCL_EXTERNAL vecType getniw(vecType point, std::array<vecType, 2> wall) {
49-
float lSquared = pow(magnitude(wall[1] - wall[0]), 2);
50-
if (lSquared == 0.0) {
51-
return {0, 0};
49+
vecType AB = wall[1] - wall[0];
50+
vecType BP = point - wall[1];
51+
vecType AP = point - wall[0];
52+
53+
float ABdotBP = dotProduct(AB, BP);
54+
float ABdotAP = dotProduct(AB, AP);
55+
56+
if (ABdotBP >= 0) {
57+
return normalize(point - wall[1]);
58+
}
59+
else if (ABdotAP < 0) {
60+
return normalize(point - wall[0]);
61+
}
62+
else {
63+
float lSquared = pow(magnitude(wall[1] - wall[0]), 2);
64+
if (lSquared == 0.0) {
65+
return {0, 0};
66+
}
67+
float t = std::max(float(0.0), std::min(float(1.0), dotProduct(point - wall[0], wall[1] - wall[0]) / lSquared));
68+
auto projection = t * AB;
69+
70+
return normalize(AP - projection);
5271
}
53-
float t = std::max(float(0.0), std::min(float(1.0), dotProduct(point - wall[0], wall[1] - wall[0]) / lSquared));
54-
auto projection = wall[0] + t * (wall[1] - wall[0]);
55-
return normalize(projection);
5672
}

src/main.cpp

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
#include "DifferentialEq.hpp"
99
#include "VectorMaths.hpp"
1010

11-
constexpr int WIDTH = 8; // metres
12-
constexpr int HEIGHT = 6; // metres
11+
constexpr int WIDTH = 9; // metres
12+
constexpr int HEIGHT = 9; // metres
1313
constexpr int SCALE = 100;
14-
constexpr int DELAY = 5000;
14+
constexpr int DELAY = 0;
1515

1616
using vecType = std::array<float, 2>;
1717

@@ -20,20 +20,45 @@ void init(SDL_Window* &win, SDL_Renderer* &render, std::vector<Actor> &actors) {
2020
win = SDL_CreateWindow("SYCL Crowd Simulation", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH * SCALE, HEIGHT * SCALE, SDL_WINDOW_SHOWN);
2121
render = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
2222

23-
actors.push_back(Actor{{4, 2},
24-
{0.01, 0.01},
25-
{0.02, 0.02},
26-
{8, 2},
27-
50, 0.05});
28-
actors.push_back(Actor{{4.5, 2},
29-
{-0.02, -0.02},
30-
{-0.03, -0.03},
31-
{1, 2},
32-
60, 0.08});
33-
34-
// Make actor move towards destination
35-
//actors[0].setVelocity(velToPoint(0.01, actors[0].getPos(), actors[0].getDestination()));
36-
//actors[1].setVelocity(velToPoint(0.008, actors[1].getPos(), actors[1].getDestination()));
23+
for (int i = 0; i < 5; i++) {
24+
for (int j = 0; j < 5; j++) {
25+
actors.push_back(Actor{{float(0.5 + (i * 0.5)), float(0.5 + (j * 0.5))},
26+
{0.01, 0.01},
27+
{0.02, 0.02},
28+
{float(6.5 + (i * 0.5)), float(6.5 + (j * 0.5))},
29+
50, 0.05, false, {255, 0, 0}});
30+
}
31+
}
32+
33+
for (int i = 0; i < 5; i++) {
34+
for (int j = 0; j < 5; j++) {
35+
actors.push_back(Actor{{float(6.5 + (i * 0.5)), float(0.5 + (j * 0.5))},
36+
{0.01, 0.01},
37+
{0.02, 0.02},
38+
{float(0.5 + (i * 0.5)), float(6.5 + (j * 0.5))},
39+
50, 0.05, false, {0, 255, 0}});
40+
}
41+
}
42+
43+
for (int i = 0; i < 5; i++) {
44+
for (int j = 0; j < 5; j++) {
45+
actors.push_back(Actor{{float(6.5 + (i * 0.5)), float(6.5 + (j * 0.5))},
46+
{0.01, 0.01},
47+
{0.02, 0.02},
48+
{float(0.5 + (i * 0.5)), float(0.5 + (j * 0.5))},
49+
50, 0.05, false, {0, 0, 255}});
50+
}
51+
}
52+
53+
for (int i = 0; i < 5; i++) {
54+
for (int j = 0; j < 5; j++) {
55+
actors.push_back(Actor{{float(0.5 + (i * 0.5)), float(6.5 + (j * 0.5))},
56+
{0.01, 0.01},
57+
{0.02, 0.02},
58+
{float(6.5 + (i * 0.5)), float(0.5 + (j * 0.5))},
59+
50, 0.05, false, {150, 150, 150}});
60+
}
61+
}
3762
}
3863

3964
void drawCircle(SDL_Renderer* &render, SDL_Point center, int radius, SDL_Color color) {
@@ -63,28 +88,21 @@ void update(sycl::queue myQueue, std::vector<Actor> &actors, Room room) {
6388
auto out = sycl::stream{1024, 768, cgh};
6489

6590
cgh.parallel_for(sycl::range<1>{actors.size()}, [=](sycl::id<1> index) {
66-
differentialEq(index, actorAcc, wallsAcc, out);
91+
if (!actorAcc[index].getAtDestination()) {
92+
differentialEq(index, actorAcc, wallsAcc, out);
93+
}
6794
});
6895
}).wait();
69-
70-
// myQueue.submit([&](sycl::handler& cgh) {
71-
// auto actorAcc = actorBuf.get_access<sycl::access::mode::read_write>(cgh);
72-
73-
// cgh.parallel_for(sycl::range<1>{actors.size()}, [=](sycl::id<1> index) {
74-
// Actor current = actorAcc[index];
75-
// actorAcc[index].setPos(current.getPos() + current.getVelocity());
76-
// });
77-
// }).wait();
7896
}
7997

8098
void draw(SDL_Renderer* &render, std::vector<Actor> actors, Room room) {
8199
SDL_SetRenderDrawColor(render, 255, 255, 255, 255);
82100
SDL_RenderClear(render);
83101

84-
SDL_Color red = {255, 0, 0, 255};
85102
for (Actor actor : actors) {
86103
SDL_Point pos = {int(actor.getPos()[0] * SCALE), int(actor.getPos()[1] * SCALE)};
87-
drawCircle(render, pos, actor.getRadius() * SCALE, red);
104+
SDL_Color actorColor = {Uint8(actor.getColor()[0]), Uint8(actor.getColor()[1]), Uint8(actor.getColor()[2]), 255};
105+
drawCircle(render, pos, actor.getRadius() * SCALE, actorColor);
88106
}
89107

90108
SDL_SetRenderDrawColor(render, 0, 0, 0, 255);
@@ -95,9 +113,7 @@ void draw(SDL_Renderer* &render, std::vector<Actor> actors, Room room) {
95113

96114
SDL_SetRenderDrawColor(render, 0, 255, 0, 255);
97115
SDL_Rect r;
98-
r.x = 690; r.y = 190; r.w = 20; r.h = 20;
99-
SDL_RenderDrawRect(render, &r);
100-
r.x = 90; r.y = 190; r.w = 20; r.h = 20;
116+
r.x = 35; r.y = 195; r.w = 10; r.h = 10;
101117
SDL_RenderDrawRect(render, &r);
102118

103119
SDL_RenderPresent(render);
@@ -114,12 +130,25 @@ int main() {
114130
SDL_Renderer* render = NULL;
115131

116132
std::vector<Actor> actors;
117-
Room room = Room({{vecType{0.5, 0.5}, vecType{0.5, 1.5}},
118-
{vecType{0.5, 2.5}, vecType{0.5, 5.5}},
119-
{vecType{0.5, 5.5}, vecType{7.5, 5.5}},
120-
{vecType{7.5, 5.5}, vecType{7.5, 0.5}},
121-
{vecType{7.5, 0.5}, vecType{0.5, 0.5}}
133+
Room room = Room({{vecType{3, 3}, vecType{4.25, 3}},
134+
{vecType{4.25, 3}, vecType{4.25, 4.25}},
135+
{vecType{4.25, 4.25}, vecType{3, 4.25}},
136+
{vecType{3, 4.25}, vecType{3, 3}},
137+
{vecType{4.75, 3}, vecType{6, 3}},
138+
{vecType{6, 3}, vecType{6, 4.25}},
139+
{vecType{6, 4.25}, vecType{4.75, 4.25}},
140+
{vecType{4.75, 4.25}, vecType{4.75, 3}},
141+
{vecType{3, 4.75}, vecType{4.25, 4.75}},
142+
{vecType{4.25, 4.75}, vecType{4.25, 6}},
143+
{vecType{4.25, 6}, vecType{3, 6}},
144+
{vecType{3, 6}, vecType{3, 4.75}},
145+
{vecType{4.75, 4.75}, vecType{6, 4.75}},
146+
{vecType{6, 4.75}, vecType{6, 6}},
147+
{vecType{6, 6}, vecType{4.75, 6}},
148+
{vecType{4.75, 6}, vecType{4.75, 4.75}},
122149
});
150+
// Room room = Room({{vecType{2, 3.5}, vecType{6, 3.5}}});
151+
// Room room = Room({{vecType{3.5, 0.5}, vecType{4.5, 5.5}}});
123152

124153
sycl::queue myQueue{sycl::gpu_selector()};
125154

@@ -131,13 +160,6 @@ int main() {
131160
bool isQuit = false;
132161
SDL_Event event;
133162

134-
//std::cout << distanceToWall(GeometricVector({-3, 1}), {GeometricVector({1, 2}), GeometricVector({4, 0})});
135-
136-
vecType s = {0.01, 5.2};
137-
vecType r = {8.7, 9.9};
138-
vecType opp = s / 0.5;
139-
std::cout << opp[0] << ", " << opp[1] << std::endl << std::endl;
140-
141163
while(!isQuit) {
142164
if (SDL_PollEvent(&event)) {
143165
if (event.type == SDL_QUIT) {

0 commit comments

Comments
 (0)