-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathWall.cpp
39 lines (30 loc) · 1.24 KB
/
Wall.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "Wall.h"
Wall::Wall() {
wall.start.set(0.0, 0.0, 0.0);
wall.end.set(0.0, 0.0, 0.0);
}
Wall::Wall(float x1, float y1, float x2, float y2) {
wall.start.set(x1, y1, 0.0);
wall.end.set(x2, y2, 0.0);
}
Point3f Wall::getNearestPoint(Point3f position_i) {
Vector3f relativeEnd, relativePos, relativeEndScal, relativePosScal;
float dotProduct;
Point3f nearestPoint;
// Create Vector Relative to Wall's 'start'
relativeEnd = wall.end - wall.start; // Vector from wall's 'start' to 'end'
relativePos = position_i - wall.start; // Vector from wall's 'start' to agent i 'position'
// Scale Both Vectors by the Length of the Wall
relativeEndScal = relativeEnd;
relativeEndScal.normalize();
relativePosScal = relativePos * (1.0F / relativeEnd.length());
// Compute Dot Product of Scaled Vectors
dotProduct = relativeEndScal.dot(relativePosScal);
if (dotProduct < 0.0) // Position of Agent i located before wall's 'start'
nearestPoint = wall.start;
else if (dotProduct > 1.0) // Position of Agent i located after wall's 'end'
nearestPoint = wall.end;
else // Position of Agent i located between wall's 'start' and 'end'
nearestPoint = (relativeEnd * dotProduct) + wall.start;
return nearestPoint;
}