游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2197|回复: 2

关于射线场景查询的问题。请赐教。

[复制链接]

22

主题

63

帖子

63

积分

注册会员

Rank: 2

积分
63
发表于 2009-3-27 10:36:00 | 显示全部楼层 |阅读模式
这里有一段关于地形碰撞检测,射线场景查询的代码,引自Ogre官方中级教程2。
源码地址:http://www.ogre3d.org/wiki/index.php/IntermediateTutorial2Source
教程地址:http://www.ogre3d.cn/index.php?title=%E6%96%87%E6%A1%A3:%E6%95%99%E7%A8%8B:%E4%B8%AD%E7%BA%A7%E6%95%99%E7%A8%8B:%E4%B8%AD%E7%BA%A7%E6%95%99%E7%A8%8B%E4%BA%8C#.E4.BB.8B.E7.BB.8D

代码:       
        bool frameStarted(const FrameEvent &evt)
        {
                // Process the base frame listener code.  Since we are going to be
                // manipulating the translate vector, we need this to happen first.
                if (!ExampleFrameListener::frameStarted(evt))
                        return false;
                // Setup the scene query
                Vector3 camPos = mCamera->getPosition();
                Ray cameraRay(Vector3(camPos.x, 5000.0f, camPos.z), Vector3::NEGATIVE_UNIT_Y);//定义一条射线
                mRaySceneQuery->setRay(cameraRay);//设置射线场景查询的“射线”
                // Perform the scene query
                RaySceneQueryResult &result = mRaySceneQuery->execute();//执行查询,返回查询结果
                RaySceneQueryResult::iterator itr = result.begin();
                // Get the results, set the camera height
                if (itr != result.end() && itr->worldFragment)
                {
                        Real terrainHeight = itr->worldFragment->singleIntersection.y;
                        if ((terrainHeight + 10.0f) > camPos.y)
                                mCamera->setPosition( camPos.x, terrainHeight + 10.0f, camPos.z );
                }

                return true;

        }
问题:
   1、在上面代码RaySceneQueryResult &result里存放的是些什么内容?也就是执行完射线场景查询函数后返回的是什么?
   2、&result值为何需要使用一个迭代器指向它?有很多内容么?在代码中也没见到迭代器遍历操作。场景查询的execute()过程大致是怎么样的?
请赐教。

30

主题

227

帖子

232

积分

中级会员

Rank: 3Rank: 3

积分
232
发表于 2009-3-30 11:32:00 | 显示全部楼层

Re:关于射线场景查询的问题。请赐教。

1、std::vector<RaySceneQueryResultEntry>
2、一般都是遍历的,但是因为这个例子没有按距离排序,所以begin的值获取的就是在最后面的地理信息。
查OGRE API吧

22

主题

63

帖子

63

积分

注册会员

Rank: 2

积分
63
 楼主| 发表于 2009-4-5 21:56:00 | 显示全部楼层

Re:关于射线场景查询的问题。请赐教。

多谢赐教,经过阁下指点,我的理解是:
代码:
RaySceneQueryResult &result = mRaySceneQuery->execute();//执行查询,返回查询结果
理解:
result为一个羊肉串,因为在射线查询可能会查询到很多场景对象,将其存放在一个std::vector<RaySceneQueryResultEntry>对象中。

代码:
RaySceneQueryResult::iterator itr = result.begin();
Real terrainHeight = itr->worldFragment->singleIntersection.y;
mCamera->setPosition( camPos.x, terrainHeight + 10.0f, camPos.z );
理解:
将摄像机位置Y坐标设为result.begin()所获取地理信息的Y值+10。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-12-20 15:46

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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