diff -Nauwr source/Irrlicht/CSceneCollisionManager.cpp source/Irrlicht/CSceneCollisionManager.cpp --- source/Irrlicht/CSceneCollisionManager.cpp 2005-08-24 18:00:28.000000000 +0200 +++ source/Irrlicht/CSceneCollisionManager.cpp 2005-09-23 11:23:56.000000000 +0200 @@ -140,6 +140,89 @@ } +//! Finds the near and far point where a line intersects a box +bool CSceneCollisionManager::getCollisionPoints( const core::aabbox3df& box, const core::line3d& ray, + core::vector3df& nearIntersection, core::vector3df& farIntersection ) +{ + bool rtnValue = false; + + if( box.intersectsWithLine( ray ) ) + { + core::vector3df linevect = ray.getVector().normalize(); + + core::vector3df edges[8]; + box.getEdges( edges ); + + core::plane3df planes[6]; + planes[0].setPlane( edges[5], edges[0], edges[4] ); // far plane + planes[1].setPlane( edges[2], edges[7], edges[6] ); // near plane + planes[2].setPlane( edges[0], edges[3], edges[2] ); // right plane + planes[3].setPlane( edges[7], edges[4], edges[6] ); // left plane + planes[4].setPlane( edges[6], edges[0], edges[2] ); // top plane + planes[5].setPlane( edges[7], edges[1], edges[5] ); // bottom plane + + core::array< core::vector3df > intersections; + core::vector3df intersection; + + if( planes[0].getIntersectionWithLine( ray.start, linevect, intersection ) && + box.isPointInside( intersection ) ) + { + intersections.push_back( intersection ); + } + if( planes[1].getIntersectionWithLine( ray.start, linevect, intersection ) && + box.isPointInside( intersection ) ) + { + intersections.push_back( intersection ); + } + if( planes[2].getIntersectionWithLine( ray.start, linevect, intersection ) && + box.isPointInside( intersection ) ) + { + intersections.push_back( intersection ); + } + if( planes[3].getIntersectionWithLine( ray.start, linevect, intersection ) && + box.isPointInside( intersection ) ) + { + intersections.push_back( intersection ); + } + if( planes[4].getIntersectionWithLine( ray.start, linevect, intersection ) && + box.isPointInside( intersection ) ) + { + intersections.push_back( intersection ); + } + if( planes[5].getIntersectionWithLine( ray.start, linevect, intersection ) && + box.isPointInside( intersection ) ) + { + intersections.push_back( intersection ); + } + + farIntersection = ray.start; + f64 farDistance = 0.0; + nearIntersection = ray.end; + f64 nearDistance = 9999999999.9; + f64 distance = 0.0; + + for( u32 i = 0; i < intersections.size(); ++i ) + { + distance = intersections[i].getDistanceFromSQ( ray.start ); + + if( distance > farDistance ) + { + farDistance = distance; + farIntersection = intersections[i]; + } + if( distance < nearDistance ) + { + nearDistance = distance; + nearIntersection = intersections[i]; + } + } + + rtnValue = true; + } + + return rtnValue; +} + //! Finds the collision point of a line and lots of triangles, if there is one. bool CSceneCollisionManager::getCollisionPoint(const core::line3d& ray, @@ -159,22 +242,21 @@ s32 cnt = 0; selector->getTriangles(Triangles.pointer(), totalcnt, cnt, ray); - core::vector3df linevect = ray.getVector(); + core::vector3df linevect = ray.getVector().normalize(); core::vector3df intersection; f32 nearest = 9999999999999.0f; bool found = false; - f32 tmp, tmp2; - f32 raylength = (f32)ray.getLengthSQ(); + f32 tmp; for (s32 i=0; i& ray, + core::vector3df& nearIntersection, core::vector3df& farIntersection ); + //! Finds the collision point of a line and lots of triangles, if there is one. virtual bool getCollisionPoint(const core::line3d& ray, ITriangleSelector* selector, core::vector3df& outCollisionPoint, diff -Nauwr include/ISceneCollisionManager.h include/ISceneCollisionManager.h --- include/ISceneCollisionManager.h 2005-08-20 22:17:46.000000000 +0200 +++ include/ISceneCollisionManager.h 2005-09-22 15:33:42.000000000 +0200 @@ -29,6 +29,10 @@ //! destructor virtual ~ISceneCollisionManager() {}; + //! Finds the near and far point where a line intersects a box + virtual bool getCollisionPoints( const core::aabbox3df& box, const core::line3d& ray, + core::vector3df& nearIntersection, core::vector3df& farIntersection ) = 0; + //! Finds the collision point of a line and lots of triangles, if there is one. //! \param ray: Line with witch collisions are tested. //! \param selector: TriangleSelector containing the triangles. It can