如何计算目标光标在xna中的位置和方向? [英] How to calculate the position and direction of the target cursor in xna?

查看:204
本文介绍了如何计算目标光标在xna中的位置和方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个像minecraft这样的小游戏。并且我卡住了几天计算光标的方向。
让我解释一下。
我想要定位一个多维数据集,我的光标在屏幕的中间,使它消失,点击。但我没有看到任何如何恢复这些信息..
有人可以指点我的样本/教程或解释吗?
非常感谢。

I developped a small games like "minecraft". and I'm stuck for a few days on the calculation of the direction of the cursor. Let me explain. I would like to target a cube with my cursor in the middle of the screen and make it disappear with a click. But I do not see any how to recover this information .. Can someone point me to a "sample / tutorial" or explanation please? Thank you very much.

我的萤幕画面游戏:

My screenShoot picture game :

推荐答案

我在我的3D XNA游戏,它的工作。评论是相当解释。基本上它从你的游标位置直接投射 Ray ,并检查它是否 Intersects()任何地图的边界对象(在这种情况下为 Unit

I take no credit for most of this--it's what I used in my 3D XNA game and it worked. The comments are fairly explanative. Basically it projects a Ray straight from your cursor position and checks to see if it Intersects() the boundingsphere of any map objects (Units in this case)

    /// <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;
    }

这篇关于如何计算目标光标在xna中的位置和方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆