RayTrace / Visibility Check / BSP P... [ EXTERNAL ]
Hi (:
Missing so long that i don't post here, i was searching about BSP parcing (wrong spell ya?) and ray trace and this things and how to do them...
I found a code up on internet:
But i really can't find a way to use it since the Ray::Trace part doesn't make any sense for me, can someone explain me on how to RayTrace, or something Like BSP parsing, Would be great any tutorial/source/anything that can make me learn how to do it externaly (:
Thx (;
Adri.
Missing so long that i don't post here, i was searching about BSP parcing (wrong spell ya?) and ray trace and this things and how to do them...
I found a code up on internet:
Code:
Ray.h:
#ifndef RAY_H
#define RAY_H
#include "Engine.h" // Includes the Vector3d structure
class Ray {
public:
Vector3d origin, direction, directionInverse;
Ray(Vector3d _origin, Vector3d _direction);
bool Trace(Vector3d leftBottom, Vector3d rightTop, float &distance);
};
#endif
Ray.cpp:
#include "Ray.h"
Ray::Ray(Vector3d _origin, Vector3d _direction) {
origin = _origin;
direction = _direction;
directionInverse.x = 1.0f / direction.x;
directionInverse.y = 1.0f / direction.y;
directionInverse.z = 1.0f / direction.z;
}
// http://gamedev.stackexchange.com/questions/18436/most-efficient-aabb-vs-ray-collision-algorithms
bool Ray::Trace(Vector3d leftBottom, Vector3d rightTop, float &distance) {
// If line is parallel and outsite the box it is not possible to intersect
if (direction.x == 0.0f && (origin.x < min(leftBottom.x, rightTop.x) || origin.x > max(leftBottom.x, rightTop.x)))
return false;
if (direction.y == 0.0f && (origin.y < min(leftBottom.y, rightTop.y) || origin.y > max(leftBottom.y, rightTop.y)))
return false;
if (direction.z == 0.0f && (origin.z < min(leftBottom.z, rightTop.z) || origin.z > max(leftBottom.z, rightTop.z)))
return false;
float t1 = (leftBottom.x - origin.x) * directionInverse.x;
float t2 = (rightTop.x - origin.x) * directionInverse.x;
float t3 = (leftBottom.y - origin.y) * directionInverse.y;
float t4 = (rightTop.y - origin.y) * directionInverse.y;
float t5 = (leftBottom.z - origin.z) * directionInverse.z;
float t6 = (rightTop.z - origin.z) * directionInverse.z;
float tmin = max(max(min(t1, t2), min(t3, t4)), min(t5, t6));
float tmax = min(min(max(t1, t2), max(t3, t4)), max(t5, t6));
if (tmax < 0) {
distance = tmax;
return false;
}
if (tmin > tmax) {
distance = tmax;
return false;
}
distance = tmin;
return true;
}
Thx (;
Adri.