-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRaycastExplosion.cpp
More file actions
47 lines (41 loc) · 1.28 KB
/
RaycastExplosion.cpp
File metadata and controls
47 lines (41 loc) · 1.28 KB
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
40
41
42
43
44
45
46
47
#include "CPhysics/RaycastExplosion.h"
#include <cmath>
#include <stdexcept>
RaycastExplosion::RaycastExplosion(const Vectors2D& epicentre, unsigned noOfRays, real distance,
const std::vector<Body*>& worldBodies)
{
rayScatter = Rayscatter(epicentre, noOfRays);
rayScatter.castRays(distance);
update(worldBodies);
}
void RaycastExplosion::changeEpicentre(const Vectors2D& v)
{
rayScatter.changeEpicentre(v);
}
void RaycastExplosion::update(const std::vector<Body*>& worldBodies)
{
raysInContact.clear();
rayScatter.updateRays(worldBodies);
for (Ray ray : rayScatter.getRays()) {
RayInformation rayInfo = ray.getRayInformation();
if (rayInfo.getB() != nullptr) {
raysInContact.push_back(rayInfo);
}
}
}
void RaycastExplosion::applyBlastImpulse(real blastPower)
{
if (!std::isfinite(blastPower)) {
throw std::invalid_argument("Raycast explosion blast power must be finite.");
}
for (RayInformation ray : raysInContact) {
Vectors2D blastDir = ray.getCoord() - rayScatter.getEpicentre();
real distance = blastDir.len();
if (distance <= EPSILON) continue;
real invDistance = 1.0f / distance;
Vectors2D impulseMag = blastDir.normalizeVec() * (blastPower * invDistance);
Body* b = ray.getB();
b->wake();
b->applyLinearImpulse(impulseMag, ray.getCoord() - b->position);
}
}