diff -Nauwr Irrlicht/include/triangle3d.h Irrlicht/include/triangle3d.h --- Irrlicht/include/triangle3d.h 2005-08-20 22:17:44.000000000 +0200 +++ Irrlicht/include/triangle3d.h 2005-09-16 13:15:50.000000000 +0200 @@ -98,7 +98,7 @@ vector3d bminusa = b - a; vector3d cp1 = bminusa.crossProduct(p1 - a); vector3d cp2 = bminusa.crossProduct(p2 - a); - return (cp1.dotProduct(cp2) > 0.0f); + return (cp1.dotProduct(cp2) >= 0.0f); } @@ -129,12 +129,13 @@ const vector3d& lineVect, vector3d& outIntersection) const { if (getIntersectionOfPlaneWithLine(linePoint, lineVect, outIntersection)) + { return isPointInside(outIntersection); + } return false; } - //! Calculates the intersection between a 3d line and //! the plane the triangle is on. //! \param lineVect: Vector of the line to intersect with. @@ -144,7 +145,7 @@ bool getIntersectionOfPlaneWithLine(const vector3d& linePoint, const vector3d& lineVect, vector3d& outIntersection) const { - vector3d normal = getNormal(); + vector3d normal = getNormal().normalize(); T t2 = normal.dotProduct(lineVect); if (t2 == 0.0f) diff -Nauwr Irrlicht/include/aabbox3d.h Irrlicht/include/aabbox3d.h --- Irrlicht/include/aabbox3d.h 2005-08-20 22:17:44.000000000 +0200 +++ Irrlicht/include/aabbox3d.h 2005-09-22 16:38:32.000000000 +0200 @@ -133,8 +133,8 @@ const vector3d& linevect, T halflength) const { - const vector3d e = (MaxEdge - MinEdge) * (T)0.5; - const vector3d t = (MinEdge + e) - linemiddle; + const vector3d e = getExtent(); + const vector3d t = getCenter() - linemiddle; float r; if ((fabs(t.X) > e.X + halflength * fabs(linevect.X)) || @@ -212,20 +212,21 @@ //! \param edges: Pointer to array of 8 edges void getEdges(vector3d *edges) const { - core::vector3df middle = (MinEdge + MaxEdge) / 2; + core::vector3df middle = getCenter(); core::vector3df diag = middle - MaxEdge; /* Edges are stored in this way: Hey, am I an ascii artist, or what? :) niko. - /1--------/3 + /4--------/0 / | / | / | / | - 5---------7 | - | 0- - -| -2 + 6---------2 | + | 5- - -| -1 | / | / |/ | / - 4---------6/ + 7---------3/ + */ edges[0].set(middle.X + diag.X, middle.Y + diag.Y, middle.Z + diag.Z); diff -Nauwr Irrlicht/CSceneCollisionManager.cpp Irrlicht/CSceneCollisionManager.cpp --- Irrlicht/CSceneCollisionManager.cpp 2005-08-24 18:00:28.000000000 +0200 +++ Irrlicht/CSceneCollisionManager.cpp 2005-09-23 11:23:56.000000000 +0200 @@ -141,6 +141,89 @@ } +//! Finds the near and far point where a line intersects a box +bool CSceneCollisionManager::getCollisionPoints( const core::aabbox3df& box, const core::line3df& 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 raylenght = (f32)ray.getLengthSQ(); + f32 tmp; for (s32 i=0; i& ray, ITriangleSelector* selector, core::vector3df& outCollisionPoint, diff -Nauwr Irrlicht/include/ISceneCollisionManager.h Irrlicht/include/ISceneCollisionManager.h --- Irrlicht/include/ISceneCollisionManager.h 2005-08-20 22:17:46.000000000 +0200 +++ Irrlicht/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::line3df& 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 diff -Nauwr Irrlicht/include/line3d.h Irrlicht/include/line3d.h --- Irrlicht/include/line3d.h 2005-08-20 22:17:44.000000000 +0200 +++ Irrlicht/include/line3d.h 2005-09-22 22:59:54.000000000 +0200 @@ -107,11 +107,14 @@ } // member variables - vector3d start; vector3d end; }; + //! Typedef for a f32 3d bounding box. + typedef line3d line3df; + //! Typedef for an integer 3d bounding box. + typedef line3d line3di; } // end namespace core } // end namespace irr