2014-11-13 2 views
-1

두 개체와 벽 사이에 광선 추적 교차점을 사용하여 충돌을 감지하려고하지만 어떤 이유로 경계 상자가 감지되지 않습니다. 기존 광선 인 vector3 & 상자 클래스를 사용하고 있습니다. 광선 추적 상자 교차점 (C++)

광선 클래스 :

class Ray { 
    public: 
    Ray() { } 
    Ray(Vector3 o, Vector3 d) { 
     origin = o; 
     direction = d; 
     inv_direction = Vector3(1/d.x(), 1/d.y(), 1/d.z()); 
     sign[0] = (inv_direction.x() < 0); 
     sign[1] = (inv_direction.y() < 0); 
     sign[2] = (inv_direction.z() < 0); 
    } 
    Ray(const Ray &r) { 
     origin = r.origin; 
     direction = r.direction; 
     inv_direction = r.inv_direction; 
     sign[0] = r.sign[0]; sign[1] = r.sign[1]; sign[2] = r.sign[2]; 
    } 

    Vector3 origin; 
    Vector3 direction; 
    Vector3 inv_direction; 
    int sign[3]; 
}; 

상자 클래스 : 나는 '

bool Area::CheckForWalls(Vector3 point1, Vector3 point2) { 
    //point1 is entity #1 @ Vector(246.98, 0.45, -487.84) 
    //point2 is entity #2 @ Vector(236.60, 0.32, -483.84) 
    Vector3 direction = point1.crossVector3D(point2); 
    Ray* ray = new Ray(point1, direction); 

    float distance = sqrt((point1.x() - point2.x()) * (point1.x() - point2.x()) + 
     (point1.y() - point2.y()) * (point1.y() - point2.y()) + 
     (point1.z() - point2.z()) * (point1.z() - point2.z())); 

    // box corner 1: (243.03, 1.00, -480.04) and corner 2: (240.02, -2.00, -491.46) 
    if (bounding_box->intersect(*ray, -2, distance)) { 
     return true; 
    } 


    return false; 
} 

: 광선이 상자를 통과하면

bool Box::intersect(const Ray &r, float t0, float t1) const { 
    float tmin, tmax, tymin, tymax, tzmin, tzmax; 

    tmin = (parameters[r.sign[0]].x() - r.origin.x()) * r.inv_direction.x(); 
    tmax = (parameters[1-r.sign[0]].x() - r.origin.x()) * r.inv_direction.x(); 
    tymin = (parameters[r.sign[1]].y() - r.origin.y()) * r.inv_direction.y(); 
    tymax = (parameters[1-r.sign[1]].y() - r.origin.y()) * r.inv_direction.y(); 
    if ((tmin > tymax) || (tymin > tmax)) 
    return false; 
    if (tymin > tmin) 
    tmin = tymin; 
    if (tymax < tmax) 
    tmax = tymax; 
    tzmin = (parameters[r.sign[2]].z() - r.origin.z()) * r.inv_direction.z(); 
    tzmax = (parameters[1-r.sign[2]].z() - r.origin.z()) * r.inv_direction.z(); 
    if ((tmin > tzmax) || (tzmin > tmax)) 
    return false; 
    if (tzmin > tmin) 
    tmin = tzmin; 
    if (tzmax < tmax) 
    tmax = tzmax; 
    return ((tmin < t1) && (tmax > t0)); 
} 

여기보고 확인하는 내 방법입니다 문제가 거리 또는 레이의 방향과 관련이 있다면 어떤 도움을 주시면 감사하겠습니다.

+0

복사 생성자가 불필요합니다. 코드에서 메모리가 누수됩니다. 레이는 새로운 것이 필요하지 않습니다. –

답변