所以,我已经成功地取得了光线的代表未投影到世界的鼠标,现在我需要检查,如果该线可以用四物相交,这里是我使用来获取射线代码:Ray在XNA中与3D四边形相交?
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);
}
同样,这是我的四元结构,我用它在三维空间中绘制“立方体世界”。 public struct Quad { public Vector3 Origin; public Vector3 UpperLeft; public Vector3 LowerLeft; public Vector3 UpperRight; public Vector3 LowerRight; public Vector3正常; 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;
}
}
我发现射线类有一个方法相交(),这需要平面结构作为参数,飞机结构需要一个正常的距离起源于构造,但我的飞机有位置和法线,而不是距离原点的距离,所以我不能转换它们。我如何检测我的射线是否与四元结构相交?
编辑: 我意识到我不能使用平面结构,因为它不是有限的大小,也不包括角落,就像我的四边形一样。我需要找到一种方法来检测这条射线是否与我创建的四边形相交...
感谢阅读,我意识到这个问题有点冗长,并且在此先感谢。
沿着平面的法向矢量的距离意味着,如果平面处于相同的方向,但被翻译为它也包含点(0,0,0),它需要移动多远以达到其实际位置。或者它可能是这个价值的负面影响,我总是混合最后一点:) –
好吧,但不是距离原点到飞机最近的距离?对我来说,这似乎措辞不佳。 –
是的,但它依赖于一个平面可以完全由该正常单位矢量和距离定义的想法。 –