我正在尝试在多个 MeshGeometryVisual3D 对象之间移动的 Modelvisual3D 对象之间实现碰撞检查,并且无法在 media3d 和 sharpdx 之间完全正确地进行矩阵/矢量/点转换,感谢任何帮助。 或者是否有内置命中测试方法的 using 命名空间?如果是这样,如何使用它们? 说它主要是代码所以我需要放入更多文本所以这里有更多文本要放入所以它会让我发布我的问题抱歉不要介意这只是填充请忽略这里和后面的文本直到代码块开始非常感谢祝你今天愉快,保重,希望你所在的地方天气好,你的家人安好,生活美好,阳光明媚,如果不是很抱歉,我想明天会好运吗?
private void CheckForCollisions(Visual3D selectedVisual2)
{
// Get the position of the selected object
var position = selectedVisual.Transform.Value;
// Create a ray from the position of the selected object
// and pointing downwards (in the negative Y direction)
var ray = new Ray(new Vector3D(selectedVisual.Transform.Value.OffsetX, position.OffsetY, position.OffsetZ), new Vector3(0, -1, 0));
// Loop through all the descendants of the HelixViewport3D and check for collisions with the selected object
foreach (var visual in MainVprt.Children)
{
// Check if the visual is a MeshGeometryVisual3D object from the meshGeoList collection
if (visual is MeshGeometryVisual3D mesh && meshGeoList.Contains(mesh))
{
// Get the model transform of the mesh
var meshTransform = mesh.Transform;
if (meshTransform != null)
{
// Get the position and orientation of the mesh
var meshPosition = meshTransform.Value.Position;
var meshOrientation = meshTransform.Value;
// Check if the ray intersects with the mesh
var hitPoint = GetRayHit2(ray, mesh);
// If there is a collision, prevent the selected object from moving downwards
if (hitPoint.HasValue && position.OffsetY - hitPoint.Value.Y < 10)
{
// Set the position of the selected object so that it is just above the mesh
var newPosition = position;
newPosition.OffsetY = hitPoint.Value.Y + 10;
selectedVisual.Transform = new MatrixTransform3D(newPosition);
}
}
}
}
}
public Point3D? GetRayHit(Ray ray, Visual3D model)
{
// Transform the ray to the coordinate space of the model
var inverseTransform = model.Transform.Value;
inverseTransform.Invert();
var transformedRay = new Ray(ray.Position * inverseTransform, ray.Direction * inverseTransform);
// Check if the transformed ray intersects with any of the mesh geometries in the model
var closestHitDistance = double.MaxValue;
Point3D? closestHitPoint = null;
foreach (var m in MainVprt.Children)
{
if (m is MeshGeometryVisual3D mesh && meshGeoList.Contains(mesh))
{
var meshGeometry = mesh.Geometry as MeshGeometry3D;
if (meshGeometry != null)
{
// Transform the mesh to the coordinate space of the model
var meshTransform = mesh.Transform.Value;
var meshOrientation = meshTransform.Value;
// Check if the transformed ray intersects with the mesh's bounds
var meshBounds = meshGeometry.Bounds;
var rayHitResult = meshBounds.Intersect(transformedRay);
// If there is an intersection, transform the hit point back to world space and
// check if it is the closest hit point found so far
if (rayHitResult.HasValue)
{
var hitPoint = transformedRay.GetPoint(rayHitResult.Value);
hitPoint *= meshTransform.Value;
var hitDistance = (hitPoint - ray.Position).LengthSquared;
if (hitDistance < closestHitDistance)
{
closestHitPoint = hitPoint;
closestHitDistance = hitDistance;
}
}
}
}
}
return closestHitPoint;
}