XNA中的拾取与碰撞检测
来源:互联网 发布:php 7 编程实战 pdf 编辑:程序博客网 时间:2024/06/05 02:38
一、 拾取与碰撞检测
1、 碰撞检测
XNA中的碰撞检测是通过测试两个物体的包围盒或者包围球是否相交来实现的。XNA为模型的每个Meshe建立一个包围盒和包围球。
(1)、获取包围球
BoundingSphere c1BoundingSphere = model1.Meshes[i].BoundingSphere;
BoundingSphere c2BoundingSphere = model2.Meshes[j].BoundingSphere;
(2)、判断两个包围球是否相交
if (c1BoundingSphere.Intersects(c2BoundingSphere))
返回值为true表示两模型相交,返回值为false表示两模型没有相交
2、 拾取
拾取是指通过鼠标来选中某个模型。
(1)、获取当前鼠标状态
MouseState mouseState = Mouse.GetState();
(2)、获取鼠标的位置信息
int mouseX = mouseState.X;
int mouseY = mouseState.Y;
(3)、构造摄像机坐标系下的两个点,分别代表了近点和远点
Vector3 nearsource = new Vector3((float)mouseX, (float)mouseY,
Vector3 farsource = new Vector3((float)mouseX, (float)mouseY,
(4)、将两个点通过逆投影矩阵转换为世界坐标系下的两个点
Matrix world = Matrix.CreateTranslation(0, 0, 0);
Vector3 nearPoint = graphics.GraphicsDevice.Viewport.Unproject(nearsource,
proj, view, world);
Vector3 farPoint = graphics.GraphicsDevice.Viewport.Unproject(farsource,
proj, view, world);
(5)、构造拾取射线
Vector3 direction = farPoint - nearPoint;
direction.Normalize();
Ray pickRay = new Ray(nearPoint, direction);
(6)、判断射线是否与模型Mesh的包围盒相交
Nullable<float> result = pickRay.Intersects(sphere);
如果result.HasValue 为 true并且result.Value < selectedDistance则认为物体被拾取。当射线与多个物体相交时可通过result.Value来判断谁在前面,值小的更靠近屏幕。在初始阶段我们将selectedDistance设置为最大值(float.MaxValue;)。
- XNA中的拾取与碰撞检测
- XNA中的拾取与碰撞检
- xna像素的碰撞检测
- opengles拾取,碰撞检测,相交检测
- 三维拾取Ray-AABB碰撞检测算法
- 碰撞检测 包围盒 包围球 XNA
- Ogre中的碰撞检测
- Ogre中的碰撞检测
- Ogre中的碰撞检测
- 游戏中的碰撞检测
- flash中的碰撞检测
- rokon中的碰撞检测
- 游戏中的碰撞检测
- 游戏中的碰撞检测
- 游戏中的碰撞检测
- Unity3d中的碰撞检测
- 游戏中的碰撞检测
- unity中的碰撞检测
- JavaScript将成Silverlight的最大对手
- .NET中的虚函数
- XNA中的显示
- XNA中的摄像机
- 微软借奥运与Adobe争夺富互联网应用统治地位
- XNA中的拾取与碰撞检测
- 我们一起快乐,一起流泪以后......
- 解决Ubuntu 8.0在vmware中不能上网问题
- 一个高效率算法
- Tweening过度缓动效果
- mvc设计模式
- 软件测试新手误区-追求最新版本的测试工具
- Hitwise 调查显示:微软的现金返还策略已见成效
- 这辈子忘吥了伱