|
|
我想给自己学习写的小引擎搞些数学部分的函数,按照惯例,先找源代码COPY`````
关于圆和平面是否相交,找到Ogre里相关的这个函数, 一下子没看懂, 于是在纸上画了下它的思路. 感觉不对啊. 差了十万八千里了, 点乘后应该减去平面方程中的 D 然后再取绝对值 和 球半径判断吧.
函数在 OgreMath.cpp 里, 判断圆是否和平面相交的函数,这个函数应该使用的频率很高吧?
OGRE出了这么久了,甚至商业游戏也用到了,出了这么久了竟然没有用户反映吗,怪哦```呵`
bool Math::intersects(const Sphere& sphere, const Plane& plane)
{
return (
Math::Abs(plane.normal.dotProduct(sphere.getCenter()))
<= sphere.getRadius() );
}
应改为
bool Math::intersects(const Sphere& sphere, const Plane& plane)
{
return (
Math::Abs(plane.normal.dotProduct(sphere.getCenter()) - plane.D )
<= sphere.getRadius() );
}
也可能是我太菜, 想错了, 呵````
[em10]
另外 IrrLicht line3d.h 里面的一个数学函数也可能有问题,问题要小一些
bool getIntersectionWithSphere(vector3d<T> sorigin, T sradius, f64& outdistance) const
{
vector3d<T> q = sorigin - start;
f64 c = q.getLength();
f64 v = q.dotProduct(getVector().normalize());
f64 d = sradius * sradius - (c*c - v*v);
if (d < 0.0)
return false;
outdistance = v - sqrt(d);
return true;
}
应将最后的 2句 改为( 因为 irrlicht 将 line 明确的定义为有起点和终点的线段 )
f64 distanceToStart = v - sqrt( d );
if ( distanceToStart < 0 || distanceToStart > getVector().getLength() )
return false;
else
{
outdistance = distanceToStart;
return true;
}
嗯```两个很棒的开源项目,有点小瑕疵在所难免
这两个开源代码给我,我想也给了我们的大家太多太多的帮助, 公开感谢 Ogre 和 IrrLicht, 虽然他们看不懂中文, 呵~ ^^[em13] |
|