/// <summary> /// Check whether or not a sphere /// </summary> /// <param name="Sphere">The sphere to test</param> /// <returns>True if the sphere intersects, false otherwise.</returns> public bool IsBoundingSphereIntersecting(BoundingSphere Sphere, out Vector3 triangleNormal) { triangleNormal = Vector3.Zero; Matrix world = GetModelMatrix(); for (int i = 0; i < _trianglesPositions.Count; i++) { Triangle triangleToTest = _trianglesPositions[i].NewByMatrix(world); if (TriangleTest.Intersects(ref Sphere, ref triangleToTest)) { triangleNormal = _trianglesNormal[i]; return(true); } } return(false); }
public static float?CheckRayIntersectsModel(Ray ray, out int modelId, float damage = -1, float range = 0f) { Dictionary <int, float> modelsClicked = new Dictionary <int, float>(); for (int i = 0; i < modelsList.Count; i++) { Matrix modelWorld = modelsList[i].GetModelMatrix(); foreach (Triangle tri in modelsList[i]._trianglesPositions) { Triangle triangle = tri.NewByMatrix(modelWorld); float? distance = TriangleTest.Intersects(ref ray, ref triangle); if (distance != null) { modelsClicked.Add(i, (float)distance); break; } } } if (modelsClicked.Count == 0) { modelId = -1; return(null); } var closest = (from pair in modelsClicked orderby pair.Value ascending select pair).First(); modelId = closest.Key; if (range != 0 && closest.Value > range) { return(null); } if (modelsList[modelId]._Explodable && damage > -1) { modelsList[modelId].SetDamageToModel(damage); } return(closest.Value); }
public int?RayIntersectsAxis(Ray ray, string gizmo) { if (gizmo == "pos") { GenerateBoundingBoxes(); for (int i = 0; i < 3; i++) { if (ray.Intersects(boxesPos[i]) != null) { return(i); } } } else if (gizmo == "rot") { foreach (Triangle tri in rotGizmo.GetRealTriangles()) { Triangle testedTriangle = tri; if (TriangleTest.Intersects(ref ray, ref testedTriangle) != null) { if (tri.TriName == "R") { return(0); } else if (tri.TriName == "G") { return(1); } else { return(2); } } } } else if (gizmo == "scale") { foreach (Triangle tri in scaleGizmo.GetRealTriangles()) { Triangle testedTriangle = tri; if (TriangleTest.Intersects(ref ray, ref testedTriangle) != null) { if (tri.TriName == "R") { return(0); } else if (tri.TriName == "G") { return(1); } else if (tri.TriName == "B") { return(2); } else { return(3); } } } } return(null); }