-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollision.cpp
More file actions
62 lines (59 loc) · 2.02 KB
/
collision.cpp
File metadata and controls
62 lines (59 loc) · 2.02 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* \author Marie DARRIGOL & Anthony LEONARD & Ophélie PELLOUX-PRAYER & Olivier SOLDANO
* \project Ray-Tracing
* \class collision.cpp
* \brief Algorithm naif of collision
*/
#include "collision.h"
#include <limits>
pair<Vec3, SceneObject*> collide(Ray &ray, vector<SceneObject*>& sceneObjects){
// init the best impact point to the ray position
Vec3 best_impact_point = ray.getOrigin();
// get the origin of the ray
Vec3 ray_origin = ray.getOrigin();
// declare the best scene object;
SceneObject* best_sceneObject = nullptr;
// declare the best distance
float best_dist = numeric_limits<float>::infinity();
// declare the current distance
float dist = 0.0;
// declare the pair for the intersect function
pair<bool, Vec3> pair_intersect;
// declare impact point
Vec3 impact_point;
//for each object of the scene
for (vector<SceneObject*>::iterator it = sceneObjects.begin(); it != sceneObjects.end(); it++){
// determine if the ray intersect the object
pair_intersect = (*it)->intersect(ray);
if (pair_intersect.first){
// compute the distance between the ray and the impact point
impact_point = pair_intersect.second - ray_origin;
dist = impact_point.length();
// if it's the best
if (best_dist > dist){
best_dist = dist;
best_sceneObject = *it;
best_impact_point = pair_intersect.second;
}
}
}
return pair<Vec3, SceneObject*>(best_impact_point, best_sceneObject);
}
pair<Vec3, SceneObject*> collide(Ray &ray, Scene &scene){
// get the vector of scene object of the scene
vector<SceneObject*>* sceneObjects = scene.getSceneObjects();
return collide(ray, *sceneObjects);
}
bool is_collisionned(Ray &ray, vector<SceneObject*>& sceneObjects){
// declare the pair for the intersect function
pair<bool, Vec3> pair_intersect;
//for each object of the scene
for (vector<SceneObject*>::iterator it = sceneObjects.begin(); it != sceneObjects.end(); it++){
// determine if the ray intersect the object
pair_intersect = (*it)->intersect(ray);
if (pair_intersect.first){
return true;
}
}
return false;
}