/// <summary> /// Returns True if any of this BoundingPart's BoundingSpheres collide with the given BoundingPart's. /// </summary> /// <param name="boundingPart"></param> /// <returns></returns> public bool Intersects(BoundingPart boundingPart) { //For optimization we will only bother checking for collisions if the proximitySphere shows a collision. if (this.ProximitySphereTransformed.Intersects(boundingPart.ProximitySphereTransformed)) { //the actual collision spheres may not have been transformed yet. if (!_spheresTransformed) { TransformSpheres(); } if (!boundingPart.SpheresTransformed) { boundingPart.TransformSpheres(); } foreach (BoundingSphere thisBS in _boundingSpheresTransformed) { foreach (BoundingSphere otherBS in boundingPart.BoundingSpheresTransformed) { if (thisBS.Intersects(otherBS)) { return(true); } } } return(false); } else { return(false); } }
public void CreateMeshInfo(XnaModel model) { _modelBoundingParts = new BoundingPartList(); //Matrix[] modelBoneTransforms = new Matrix[model.Bones.Count]; modelBoneTransforms = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(modelBoneTransforms); BoundingSphere[] pieces = new BoundingSphere[1]; foreach (ModelMesh mesh in model.Meshes) { BoundingPart boundingPart = new BoundingPart(mesh.BoundingSphere, pieces, modelBoneTransforms[mesh.ParentBone.Index], mesh.Name, Color.White); _modelBoundingParts.Add(boundingPart); } }
private void Load(XnaModel model, string contentName) { _modelBoundingParts = new BoundingPartList(); Matrix[] modelBoneTransforms = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(modelBoneTransforms); XmlDocument meshInfo = new XmlDocument(); if (File.Exists(string.Format(@".\MeshInfo\{0}.xml", contentName))) { meshInfo.Load(string.Format(@".\MeshInfo\{0}.xml", contentName)); } if ((!meshInfo.DocumentElement.HasAttributes) || (meshInfo.DocumentElement.Attributes["scaleModifier"] == null)) { _scaleModifier = 1f; } else { _scaleModifier = float.Parse(meshInfo.DocumentElement.Attributes["scaleModifier"].Value); } int meshIndex = 0; foreach (ModelMesh mesh in model.Meshes) { string xpath = string.Format("/MeshInfo/MeshPart[@id='{0}']", mesh.Name); XmlNode meshInfoPart = meshInfo.SelectSingleNode(xpath); XmlNodeList meshInfoBoundingParts = null; if (meshInfoPart != null) { xpath = "BoundingParts/BoundingPart"; meshInfoBoundingParts = meshInfoPart.SelectNodes(xpath); } if ((meshInfoBoundingParts != null) && (meshInfoBoundingParts.Count > 0)) { BoundingSphere[] pieces = new BoundingSphere[meshInfoBoundingParts.Count]; int pieceIndex = 0; Color boundingPartColour = new Color(byte.Parse(meshInfoPart.Attributes["color.bounds.r"].Value), byte.Parse(meshInfoPart.Attributes["color.bounds.g"].Value), byte.Parse(meshInfoPart.Attributes["color.bounds.b"].Value)); foreach (XmlNode subdivisionNode in meshInfoBoundingParts) { float x0 = float.Parse(subdivisionNode.Attributes["x"].Value); float y0 = float.Parse(subdivisionNode.Attributes["y"].Value); float z0 = float.Parse(subdivisionNode.Attributes["z"].Value); float w0 = float.Parse(subdivisionNode.Attributes["w"].Value); Vector4 subdivision = new Vector4(x0, y0, z0, w0); //Determine the new BoundingSphere's Radius float radius = subdivision.W * mesh.BoundingSphere.Radius; //Determine the new BoundingSphere's Center by interpolating. //The subdivision's X, Y, Z represent percentages in each axis. //They will used across the full diameter of XNA's "default" BoundingSphere. float x = MathHelper.Lerp(mesh.BoundingSphere.Center.X - mesh.BoundingSphere.Radius, mesh.BoundingSphere.Center.X + mesh.BoundingSphere.Radius, subdivision.X); float y = MathHelper.Lerp(mesh.BoundingSphere.Center.Y - mesh.BoundingSphere.Radius, mesh.BoundingSphere.Center.Y + mesh.BoundingSphere.Radius, subdivision.Y); float z = MathHelper.Lerp(mesh.BoundingSphere.Center.Z - mesh.BoundingSphere.Radius, mesh.BoundingSphere.Center.Z + mesh.BoundingSphere.Radius, subdivision.Z); Vector3 center = new Vector3(x, y, z); pieces[pieceIndex] = new BoundingSphere(center, radius); pieceIndex++; } BoundingPart boundingPart = new BoundingPart(mesh.BoundingSphere, pieces, modelBoneTransforms[mesh.ParentBone.Index], mesh.Name, boundingPartColour); _modelBoundingParts.Add(boundingPart); } meshIndex++; } }
public BoundingPart Clone() { BoundingPart clone = new BoundingPart(_proximitySphere, _boundingSpheres.ToArray(), _boneTransform, _name, _color); return(clone); }
public bool DetectCollision(BoundingPart boundingPart) { return(_modelBoundingParts.Intersects(boundingPart)); }