Skip to content

Commit aac8ec8

Browse files
committed
Started vector math helper functions
1 parent b791fb4 commit aac8ec8

File tree

8 files changed

+66
-16
lines changed

8 files changed

+66
-16
lines changed

include/Actor.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
#include <iostream>
33
#include <array>
44

5-
Actor::Actor(std::array<float, 2> pPos, std::array<float, 2> pVelocity, std::array<float, 2> pDesiredVelocity, float pMass, float pRadius) {
5+
Actor::Actor(std::array<float, 2> pPos, std::array<float, 2> pVelocity, std::array<float, 2> pDesiredVelocity, std::array<float, 2> pDestination, float pMass, float pRadius) {
66
pos = pPos;
77
velocity = pVelocity;
88
desiredVelocity = pDesiredVelocity;
9+
destination = pDestination;
910
mass = pMass;
1011
radius = pRadius;
1112
}
@@ -22,6 +23,10 @@ std::array<float, 2> Actor::getDesiredVelocity() {
2223
return desiredVelocity;
2324
}
2425

26+
std::array<float, 2> Actor::getDestination() {
27+
return destination;
28+
}
29+
2530
float Actor::getMass() {
2631
return mass;
2732
}
@@ -40,4 +45,8 @@ void Actor::setVelocity(std::array<float, 2> newVelocity) {
4045

4146
void Actor::setDesiredVelocity(std::array<float, 2> newDesiredVelocity) {
4247
desiredVelocity = newDesiredVelocity;
48+
}
49+
50+
void Actor::setDestination(std::array<float, 2> newDestination) {
51+
destination = newDestination;
4352
}

include/Actor.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
#ifndef Actor_hpp
22
#define Actor_hpp
33

4-
#include<iostream>
4+
#include <iostream>
55
#include <array>
66

77
class Actor {
88
private:
99
std::array<float, 2> pos;
1010
std::array<float, 2> velocity;
1111
std::array<float, 2> desiredVelocity;
12+
std::array<float, 2> destination;
1213
float mass;
1314
float radius;
1415
public:
15-
Actor(std::array<float, 2> pPos, std::array<float, 2> pVelocity, std::array<float, 2> pDesiredVelocity, float pMass, float pRadius);
16+
Actor(std::array<float, 2> pPos, std::array<float, 2> pVelocity, std::array<float, 2> pDesiredVelocity, std::array<float, 2> pDestination, float pMass, float pRadius);
1617

1718
std::array<float, 2> getPos();
1819
std::array<float, 2> getVelocity();
1920
std::array<float, 2> getDesiredVelocity();
21+
std::array<float, 2> getDestination();
2022
float getMass();
2123
float getRadius();
2224

2325
void setPos(std::array<float, 2> newPos);
2426
void setVelocity(std::array<float, 2> newVelocity);
2527
void setDesiredVelocity(std::array<float, 2> newDesiredVelocity);
28+
void setDestination(std::array<float, 2> newDestination);
2629
};
2730

2831
#endif

include/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
add_library(include Actor.cpp Room.cpp)
1+
add_library(include Actor.cpp Room.cpp MathHelper.cpp)

include/MathHelper.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "MathHelper.hpp"
2+
#include <iostream>
3+
#include <vector>
4+
#include <array>
5+
#include <cmath>
6+
7+
std::array<float, 2> getDirectionVector(std::array<float, 2> a, std::array<float, 2> b) {
8+
return {float(b[0] - a[0]), float(b[1] - a[1])};
9+
}
10+
11+
std::array<float, 2> velFromSpeedAndDir(float s, std::array<float, 2> d) {
12+
return {(s / magnitude(d)) * d[0], (s / magnitude(d)) * d[1]};
13+
}
14+
15+
std::array<float, 2> velToPoint(float s, std::array<float, 2> pos, std::array<float, 2> destination) {
16+
return velFromSpeedAndDir(s, getDirectionVector(pos, destination));
17+
}
18+
19+
float magnitude(std::array<float, 2> inp) {
20+
return std::sqrt((inp[0] * inp[0]) + (inp[1] * inp[1]));
21+
}

include/MathHelper.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef MathHelper_hpp
2+
#define MathHelper_hpp
3+
4+
#include <iostream>
5+
#include <vector>
6+
#include <array>
7+
8+
std::array<float, 2> getDirectionVector(std::array<float, 2> a, std::array<float, 2> b);
9+
10+
std::array<float, 2> velFromSpeedAndDir(float s, std::array<float, 2> d);
11+
12+
std::array<float, 2> velToPoint(float s, std::array<float, 2> pos, std::array<float, 2> destination);
13+
14+
float magnitude(std::array<float, 2> inp);
15+
16+
#endif

include/Room.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@
33
#include <vector>
44
#include <array>
55

6-
Room::Room(std::vector<std::array<float, 4>> pWalls, std::vector<std::array<float, 2>> pDestinations) {
6+
Room::Room(std::vector<std::array<float, 4>> pWalls) {
77
walls = pWalls;
8-
destinations = pDestinations;
98
}
109

1110
std::vector<std::array<float, 4>> Room::getWalls() {
1211
return walls;
13-
}
14-
15-
std::vector<std::array<float, 2>> Room::getDestinations() {
16-
return destinations;
1712
}

include/Room.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
#ifndef Room_hpp
22
#define Room_hpp
33

4-
#include<iostream>
4+
#include <iostream>
55
#include <vector>
66
#include <array>
77

88
class Room {
99
private:
1010
std::vector<std::array<float, 4>> walls;
11-
std::vector<std::array<float, 2>> destinations;
1211
public:
13-
Room(std::vector<std::array<float, 4>> pWalls, std::vector<std::array<float, 2>> pDestinations);
12+
Room(std::vector<std::array<float, 4>> pWalls);
1413

1514
std::vector<std::array<float, 4>> getWalls();
16-
std::vector<std::array<float, 2>> getDestinations();
1715
};
1816

1917
#endif

src/main.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "Actor.hpp"
66
#include "Room.hpp"
7+
#include "MathHelper.hpp"
78

89
const int WIDTH = 8; // metres
910
const int HEIGHT = 6; // metres
@@ -15,7 +16,8 @@ void init(SDL_Window* &win, SDL_Renderer* &render, std::vector<Actor> &actors) {
1516
win = SDL_CreateWindow("SYCL Crowd Simulation", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH * SCALE, HEIGHT * SCALE, SDL_WINDOW_SHOWN);
1617
render = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
1718

18-
actors.push_back(Actor{{1, 2}, {0.01, 0.01}, {0.02, 0.02}, 50, 0.05});
19+
actors.push_back(Actor{{4, 0}, {0.01, 0.01}, {0.02, 0.02}, {1, 2}, 50, 0.05});
20+
actors.push_back(Actor{{8, 6}, {-0.02, -0.02}, {-0.03, -0.03}, {1, 2}, 60, 0.08});
1921
}
2022

2123
void drawCircle(SDL_Renderer* &render, SDL_Point center, int radius, SDL_Color color) {
@@ -53,6 +55,9 @@ void draw(SDL_Renderer* &render, std::vector<Actor> actors, Room room) {
5355
SDL_RenderDrawLine(render, wall[0] * SCALE, wall[1] * SCALE, wall[2] * SCALE, wall[3] * SCALE);
5456
}
5557

58+
SDL_SetRenderDrawColor(render, 0, 255, 0, 255);
59+
SDL_RenderDrawPoint(render, 100, 200);
60+
5661
SDL_RenderPresent(render);
5762
}
5863

@@ -67,10 +72,13 @@ int main() {
6772
SDL_Renderer* render = NULL;
6873

6974
std::vector<Actor> actors;
70-
Room room = Room({{0.5, 0.5, 0.5, 1.5}, {0.5, 2.5, 0.5, 5.5}, {0.5, 5.5, 7.5, 5.5}, {7.5, 5.5, 7.5, 0.5}, {7.5, 0.5, 0.5, 0.5}}, {});
75+
Room room = Room({{0.5, 0.5, 0.5, 1.5}, {0.5, 2.5, 0.5, 5.5}, {0.5, 5.5, 7.5, 5.5}, {7.5, 5.5, 7.5, 0.5}, {7.5, 0.5, 0.5, 0.5}});
7176

7277
init(win, render, actors);
7378

79+
// Make actor move towards destination
80+
actors[0].setVelocity(velToPoint(0.01, actors[0].getPos(), actors[0].getDestination()));
81+
7482
draw(render, actors, room);
7583

7684
int delayCounter = 0;

0 commit comments

Comments
 (0)