我开发了一个像“我的世界”这样的小游戏。我在光标方向的计算上停留了几天。让我解释。我想用我的光标在屏幕中间定位一个立方体,然后单击它使其消失。但我没有看到任何如何恢复这些信息..有人可以指点我一个“示例/教程”或解释吗?非常感谢你。
我的截屏图片游戏:
我开发了一个像“我的世界”这样的小游戏。我在光标方向的计算上停留了几天。让我解释。我想用我的光标在屏幕中间定位一个立方体,然后单击它使其消失。但我没有看到任何如何恢复这些信息..有人可以指点我一个“示例/教程”或解释吗?非常感谢你。
我的截屏图片游戏:
当然,这在很大程度上取决于您如何组织数据。但是,如果您将多边形和网格存储在某处(我不是在谈论顶点缓冲区!!),就像在八叉树中一样,您可以执行以下操作:
您从您的相机设置中创建一条射线并通过您的场景“发射”它。通常可以扩展八叉树或类似结构以轻松进行射线测试以返回命中元素。当然,您可以暴力迭代所有四边形,但这很快就会成为性能瓶颈。从那里您可以将结果用于您想要的任何东西。
这整个过程通常被称为“挑选”,这是一个非常好的教程。
我对其中的大部分内容不以为然——这是我在我的 3D XNA 游戏中使用的,并且它有效。这些评论是相当解释性的。基本上,它Ray
从您的光标位置投射一条直线并检查它是否Intersects()
是任何地图对象的边界球(Unit
在这种情况下为 s )
/// <summary>
/// Creates a Ray translating screen cursor position into screen position
/// </summary>
/// <param name="projectionMatrix"></param>
/// <param name="viewMatrix"></param>
/// <returns></returns>
public Ray CalculateCursorRay(Matrix projectionMatrix, Matrix viewMatrix)
{
// create 2 positions in screenspace using the cursor position. 0 is as
// close as possible to the camera, 1 is as far away as possible.
Vector3 nearSource = new Vector3(mousePosition, 0f);
Vector3 farSource = new Vector3(mousePosition, 1f);
// use Viewport.Unproject to tell what those two screen space positions
// would be in world space. we'll need the projection matrix and view
// matrix, which we have saved as member variables. We also need a world
// matrix, which can just be identity.
Vector3 nearPoint = GraphicsDevice.Viewport.Unproject(nearSource,
projectionMatrix, viewMatrix, Matrix.Identity);
Vector3 farPoint = GraphicsDevice.Viewport.Unproject(farSource,
projectionMatrix, viewMatrix, Matrix.Identity);
// find the direction vector that goes from the nearPoint to the farPoint
// and normalize it....
Vector3 direction = farPoint - nearPoint;
direction.Normalize();
// and then create a new ray using nearPoint as the source.
return new Ray(nearPoint, direction);
}
private Vector3 cursorRayToCoords(Ray ray, Vector3 cameraPos)
{
Nullable<float> distance = ray.Intersects(new Plane(Vector3.Up, 0.0f));
if (distance == null)
return Vector3.Zero;
else
{
return cameraPos + ray.Direction * (float)distance;
}
}
public override void Update(GameTime gameTime)
{
cursorRay = CalculateCursorRay(camera.Projection, camera.View);
cursorOnMapPos = cursorRayToCoords(cursorRay, cursorRay.Position);
checkInput(gameTime);
base.Update(gameTime);
}
/// <summary>
/// Returns the nearest unit to the cursor
/// </summary>
/// <returns>The unit nearest to the cursor</returns>
private Unit getCursorUnit()
{
Unit closestUnit = null;
float? nearestDistance = null;
float? checkIntersect = null;
foreach (Unit unit in map.Units)//checks to see if unit selection
{
checkIntersect = cursorRay.Intersects(unit.BoundingSphere);
if (checkIntersect != null)//if intersection detected
{
if (nearestDistance == null) //first unit found
{
closestUnit = unit;
nearestDistance = (float)checkIntersect;
}
else if ((float)checkIntersect < (float)nearestDistance)//for any others, only find the nearest
{
closestUnit = unit;
nearestDistance = (float)checkIntersect;
}
}
}
return closestUnit;
}