雷交叉口在XNA 3D四边形? [英] Ray intersection with 3D quads in XNA?

查看:154
本文介绍了雷交叉口在XNA 3D四边形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我已经成功地做​​了一个射线代表未投影到世界的鼠标,现在我需要检查,如果该线可与四物相交,这里是我使用来获取射线的代码:

So I have successfully made a ray the represents the mouse unprojected into the world, and now I need to check if that ray can intersect with a quad object, here is the code I use to get the ray:

public Ray GetMouseRay()
    {
        Vector2 mousePosition = new Vector2(cursor.getX(), cursor.getY());

        Vector3 nearPoint = new Vector3(mousePosition, 0);
        Vector3 farPoint = new Vector3(mousePosition, 1);

        nearPoint = viewport.Unproject(nearPoint, projectionMatrix, viewMatrix, Matrix.Identity);
        farPoint = viewport.Unproject(farPoint, projectionMatrix, viewMatrix, Matrix.Identity);

        Vector3 direction = farPoint - nearPoint;
        direction.Normalize();

        return new Ray(nearPoint, direction);
    }

和类似的,这是我四结构,我用它来画一个魔方世界在三维空间。
公共结构四
{
公众的Vector3来源;
公众的Vector3 UpperLeft;
公众的Vector3 LowerLeft;
公众的Vector3 UpperRight;
公众的Vector3 LowerRight;
公众的Vector3正常;
公众的Vector3向上;
公众的Vector3左;

And similarly, this is my quad struct, which I use to draw a "cube world" in 3d space. public struct Quad { public Vector3 Origin; public Vector3 UpperLeft; public Vector3 LowerLeft; public Vector3 UpperRight; public Vector3 LowerRight; public Vector3 Normal; public Vector3 Up; public Vector3 Left;

    public VertexPositionNormalTexture[] Vertices;
      public int[] Indexes;
    public short[] Indexes;


    public Quad( Vector3 origin, Vector3 normal, Vector3 up, 
        float width, float height )
    {
        Vertices = new VertexPositionNormalTexture[4];
        Indexes = new short[6];
        Origin = origin;
        Normal = normal;
        Up = up;

        // Calculate the quad corners
        Left = Vector3.Cross( normal, Up );
        Vector3 uppercenter = (Up * height / 2) + origin;
        UpperLeft = uppercenter + (Left * width / 2);
        UpperRight = uppercenter - (Left * width / 2);
        LowerLeft = UpperLeft - (Up * height);
        LowerRight = UpperRight - (Up * height);

        FillVertices();
    }

    private void FillVertices()
    {
        // Fill in texture coordinates to display full texture
        // on quad
        Vector2 textureUpperLeft = new Vector2( 0.0f, 0.0f );
        Vector2 textureUpperRight = new Vector2( 1.0f, 0.0f );
        Vector2 textureLowerLeft = new Vector2( 0.0f, 1.0f );
        Vector2 textureLowerRight = new Vector2( 1.0f, 1.0f );

        // Provide a normal for each vertex
        for (int i = 0; i < Vertices.Length; i++)
        {
            Vertices[i].Normal = Normal;
        }

        // Set the position and texture coordinate for each
        // vertex
        Vertices[0].Position = LowerLeft;
        Vertices[0].TextureCoordinate = textureLowerLeft;
        Vertices[1].Position = UpperLeft;
        Vertices[1].TextureCoordinate = textureUpperLeft;
        Vertices[2].Position = LowerRight;
        Vertices[2].TextureCoordinate = textureLowerRight;
        Vertices[3].Position = UpperRight;
        Vertices[3].TextureCoordinate = textureUpperRight;

        // Set the index buffer for each vertex, using
        // clockwise winding
        Indexes[0] = 0;
        Indexes[1] = 1;
        Indexes[2] = 2;
        Indexes[3] = 2;
        Indexes[4] = 1;
        Indexes[5] = 3;
    }
}



我发现射线类有一个方法相交( ),这需要的平面的结构类型作为参数,与平面结构需要一个正常和距离产地在构造函数中,但我的飞机有一个位置和正常的,而不是从原点只是距离,这样我就可以'T将它们转换。 ?我怎样才能检测是否我的射线相交我四结构

I found that the ray class has a method intersects() which takes a plane struct as a parameter, and the plane struct takes a normal and distance from origin in the constructor, but my planes have a position and normal instead of just distance from origin, so I can't convert them. How can I detect if my ray is intersecting my quad struct?

编辑:
我知道,我不能在飞机上使用结构,因为它是不的有限尺寸,并且不包括角部,作为我的四一样。我现在需要找到一种方式,如果这种射线相交我创建...

I realize that I can't use the plane struct, as it is not of finite size and does not include corners, as my quad does. I need to find a way now to detect if this ray is intersecting the quad I have created...

感谢您的阅读四察觉,我意识到这个问题是一个有点冗长,并在此先感谢。

Thanks for reading, I realize this question is a bit lengthy, and thanks in advance.

推荐答案

我不知道到底是什么,他们的意思是沿着从原点的正常距离,但我会假设它仅仅意味着从原点的距离。你可以从length属性上的的Vector3

I'm not sure exactly what they mean by "distance along the normal from the origin", but I would assume it just mean the distance from the origin. You can get that from the length property on a Vector3.

如果不工作,也有一个构造器的飞机的该飞机上有三个点:

If that doesn't work, there is also a constructor for plane which takes three points on the plane:

public Plane (
     Vector3 point1,
     Vector3 point2,
     Vector3 point3
)

您可以与任何使用此3点的飞机上,比如你四边形的角落,营造一个平面上。

You can use this with any 3 points on the plane, such as the corners of your quad, to create a plane.

这篇关于雷交叉口在XNA 3D四边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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