游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2491|回复: 5

OGRE最新的源代码里数学部分有一个基本函数竟然有一个完

[复制链接]

14

主题

156

帖子

158

积分

注册会员

Rank: 2

积分
158
QQ
发表于 2007-6-13 15:06:00 | 显示全部楼层 |阅读模式
我想给自己学习写的小引擎搞些数学部分的函数,按照惯例,先找源代码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]

8

主题

390

帖子

390

积分

中级会员

Rank: 3Rank: 3

积分
390
发表于 2007-6-13 16:29:00 | 显示全部楼层

Re:OGRE最新的源代码里数学部分有一个基本函数竟然有一

仔细看了下,果然有这个bug。不过应该该为-d,而不是+d,ogre这个d带符号的,请参考其计算点面距离的函数。

ogre有个函数,计算点面距离,加了d,显然球和面相交计算,就是球心到平面小于半径,一定要加d
Real Plane::getDistance (const Vector3& rkPoint) const
    {
        return normal.dotProduct(rkPoint) + d;
    }

8

主题

390

帖子

390

积分

中级会员

Rank: 3Rank: 3

积分
390
发表于 2007-6-13 16:32:00 | 显示全部楼层

Re:OGRE最新的源代码里数学部分有一个基本函数竟然有一

请参考。ogre的d反了符号
下面是其构造函数
//-----------------------------------------------------------------------
    Plane:lane (const Plane& rhs)
    {
        normal = rhs.normal;
        d = rhs.d;
    }
    //-----------------------------------------------------------------------
    Plane::Plane (const Vector3& rkNormal, Real fConstant)
    {
        normal = rkNormal;
        d = -fConstant;
    }
//-----------------------------------------------------------------------
    Plane::Plane (const Vector3& rkNormal, const Vector3& rkPoint)
    {
        normal = rkNormal;
        d = -rkNormal.dotProduct(rkPoint);
    }

//
//另外:ogre难道就这个样子,对不起观众啊。。。。。

14

主题

156

帖子

158

积分

注册会员

Rank: 2

积分
158
QQ
 楼主| 发表于 2007-6-13 17:03:00 | 显示全部楼层

Re:OGRE最新的源代码里数学部分有一个基本函数竟然有一

嗯```干嘛要定义成负的距离? 有啥玄机在里面吗

8

主题

390

帖子

390

积分

中级会员

Rank: 3Rank: 3

积分
390
发表于 2007-6-13 17:58:00 | 显示全部楼层

Re:OGRE最新的源代码里数学部分有一个基本函数竟然有一

ax+by+cz+d=0;
ax+by+cz=d;
两种平面方程写法,导致d可能符号相反,其实无所谓的。。。。

18

主题

971

帖子

982

积分

高级会员

Rank: 4

积分
982
发表于 2007-6-13 18:02:00 | 显示全部楼层

Re:OGRE最新的源代码里数学部分有一个基本函数竟然有一

没发现什么玄机…
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-26 03:08

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表