Ray intersection with 3D quads in XNA?

后端 未结 3 1306
野趣味
野趣味 2021-01-20 09:19

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

相关标签:
3条回答
  • 2021-01-20 10:04

    Since you are using cubes, you can use the Ray.Intersects(Plane) method. You just need a few more planes.

    Once you determine the first intersection point, then you can construct more planes for each perpendicular face of your cube. Then you can use the Plane.Distance(Vector3) (I'm pretty sure this or something like it exists) to make sure your point is between each pair of perpendicular planes. If it is not between them, then you can ignore the intersection.

    0 讨论(0)
  • 2021-01-20 10:09

    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
    )
    

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

    0 讨论(0)
  • 2021-01-20 10:21

    Not exactly a straight answer to your question, but perhaps you find this relevant.

    Have you considered constructing a BoundingBox object and use the Intersects(Ray)? http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.boundingbox.intersects.aspx

    Edit 1: It's how I usually do it. Depending on your data structure, it might also be easier to optimize (if you construct larger bounding boxes to bore into for example).

    Edit 2: As per Niko's suggestion: I don't want to post the relevant code (because it's a couple of pages long in total to make the context make sense) from the samples, but I can point you to the parts that you'll be interested in.

    1. Download the sample from http://xbox.create.msdn.com/en-US/education/catalog/sample/picking_triangle
    2. The bottom of the Cursor.cs file contains what you already know, calculating the Ray
    3. Go to the Game.cs file. There you'll find UpdatePicking() which calls RayIntersectsModel(...) which calls RayIntersectsTriangle(...)

    The transform of the Ray is this part:

    Matrix inverseTransform = Matrix.Invert( modelTransform );
    
    ray.Position = Vector3.Transform( ray.Position, inverseTransform );
    ray.Direction = Vector3.TransformNormal( ray.Direction, inverseTransform );
    

    How deep you want to go (how accurate your picking) is up to your needs of course, but there you have it.

    0 讨论(0)
提交回复
热议问题