在XNA中拖放一个Model 3D [英] Drag and Drop one Model 3D in XNA

查看:83
本文介绍了在XNA中拖放一个Model 3D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在XNA 4.0 3D中.我想拖放一个3D模型.因此,我是否必须在模型中检查鼠标.我的问题是我不知道将模型从3D更改为2D的位置和等级.它与相机的相关矩阵视图和矩阵投影有关吗?
这是我的代码:
我的代码

In XNA 4.0 3D. I want to Drag and Drop one model 3D.So,I must check mouse in a Model or not. My problem is I don''t know change position and rate this Model from 3D to 2D. It''s related Matrix View and Matrix Projection of camera??
This is my code:
my code

推荐答案

http://msdn.microsoft.com/en-us/library/bb203905.aspx [ MSFT示例 [
http://msdn.microsoft.com/en-us/library/bb203905.aspx[^]

There is a MSFT Sample[^] to do this.


在此代码示例中:
取得3D光线:
In this code example:
Get a Ray in 3D:
Ray GetPickRay()
        {
            MouseState mouseState = Mouse.GetState();

            int mouseX = mouseState.X;
            int mouseY = mouseState.Y;

            Vector3 nearsource = new Vector3((float)mouseX, (float)mouseY, 0f);
            Vector3 farsource = new Vector3((float)mouseX, (float)mouseY, 1f);

            Matrix world = Matrix.CreateTranslation(0, 0, 0);

            Vector3 nearPoint = GraphicsDevice.Viewport.Unproject(nearsource,
                camera.Projection, camera.View, world);

            Vector3 farPoint = GraphicsDevice.Viewport.Unproject(farsource,
                camera.Projection, camera.View, world);

            // Create a ray from the near clip plane to the far clip plane.
            Vector3 direction = farPoint - nearPoint;
            direction.Normalize();
            Ray pickRay = new Ray(nearPoint, direction);

            return pickRay;
        }


并使用该射线检查碰撞:


And use that Ray to check collision:

public void CheckMouse()
       {
           MouseState mouseState = Mouse.GetState();
           float deltaX = (float)mouseState.X - (float)lastMouseState2.X;
           float deltaY = -(float)mouseState.Y + (float)lastMouseState2.Y;
           if (mouseState.LeftButton==ButtonState.Pressed)
           {
               Ray ray = GetPickRay();
               float selectedDistance = float.MaxValue;

                   Nullable<float> result = ray.Intersects(models[0].BoudingSphere);
                   if (result.HasValue==true)
                   {
                       if (result.Value < selectedDistance)
                       {

                          models[0].Positon += new Vector3(deltaX, deltaY, 0);
                       }
                   }

           }
           lastMouseState2 = mouseState;

       }</pre>


您可以用鼠标拖放一个模型.


And you can drag and drop one model by mouse.


这篇关于在XNA中拖放一个Model 3D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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