/// <summary> /// It tests for collision among the collision elements which /// have been registered to the collision layer and returns the result. /// </summary> /// <param name="collide">Source collsion element</param> /// <param name="idLayer">Destination collison layer ID number</param> /// <param name="resultType">type of result</param> /// <returns>A result report</returns> public CollisionResult HitTest(CollideElement collide, int idLayer, ResultType resultType) { // Get the collide layer CollisionLayer layer = GetLayer(idLayer); return(HitTest(collide, ref layer, resultType)); }
public void Clear() { this.distance = 0.0f; this.collideCount = 0; this.detectedCollide = null; this.intersect = null; this.normal = null; }
/// <summary> /// Add a collsion element. /// </summary> public void AddCollide(CollideElement collide) { if (collideContainer.Contains(collide)) { throw new InvalidOperationException("Already entry the collide"); } collideContainer.Add(collide); collide.ParentLayer = this; }
/// <summary> /// Finds a contaning quad node. /// </summary> public QuadNode GetNodeContaining(ref CollideElement bounds) { if (rootNode != null) { if (rootNode.Contains(ref bounds)) { return(rootNode.GetNodeContaining(ref bounds)); } } return(null); }
/// <summary> /// Finds a contaning quad node. /// </summary> public QuadNode GetNodeContaining(ref CollideElement bounds) { if (this.IsLeafNode == false) { // checks with upper left node. if (UpperLeftNode != null) { if (UpperLeftNode.Contains(ref bounds)) { return(UpperLeftNode.GetNodeContaining(ref bounds)); } } // checks with upper right node. if (UpperRightNode != null) { if (UpperRightNode.Contains(ref bounds)) { return(UpperRightNode.GetNodeContaining(ref bounds)); } } // checks with lower left node. if (LowerLeftNode != null) { if (LowerLeftNode.Contains(ref bounds)) { return(LowerLeftNode.GetNodeContaining(ref bounds)); } } // checks with lower right node. if (LowerLeftNode != null) { if (LowerLeftNode.Contains(ref bounds)) { return(LowerLeftNode.GetNodeContaining(ref bounds)); } } } // checks with this node. if (this.Contains(ref bounds)) { return(this); } return(null); }
/// <summary> /// Find a collision element using the name. /// </summary> public CollideElement FindCollide(string name) { // Finding collision by name for (int i = 0; i < collideContainer.Count; i++) { CollideElement collide = collideContainer[i]; if (collide.Name == name) { return(collide); } } return(null); }
/// <summary> /// checks contaning. /// </summary> public bool Contains(ref CollideElement bounds) { if (bounds is CollideBox) { CollideBox target = bounds as CollideBox; return(containBox.Intersects(target.BoundingBox)); } else if (bounds is CollideSphere) { CollideSphere target = bounds as CollideSphere; return(containBox.Intersects(target.BoundingSphere)); } else if (bounds is CollideRay) { CollideRay target = bounds as CollideRay; return(containBox.Intersects(target.Ray) != null); } return(false); }
public void SetCollide(CollideElement collide) { modelCollide = collide; modelCollide.Name = Name + "_Collide"; modelCollide.Owner = (object)this; }
protected bool TestUsingQuadTree(CollideElement sourceCollide, QuadNode quadNode, out Vector3 intersect, out Vector3 normal, out float distance) { bool result = false; float tempDistance = 0.0f; Vector3 tempIntersect = Vector3.Zero; Vector3 tempNormal = Vector3.Zero; float closestDistance = float.MaxValue; Vector3 closestIntersection = Vector3.Zero; Vector3 closestNormal = Vector3.Zero; distance = 0.0f; intersect = Vector3.Zero; normal = Vector3.Zero; // checks upper left node. if (quadNode.UpperLeftNode != null) { if (TestUsingQuadTree(sourceCollide, quadNode.UpperLeftNode, out tempIntersect, out tempNormal, out tempDistance)) { result = true; // checks closest if (closestDistance > tempDistance) { closestDistance = tempDistance; closestIntersection = tempIntersect; closestNormal = tempNormal; } } } // checks upper right node. if (quadNode.UpperRightNode != null) { if (TestUsingQuadTree(sourceCollide, quadNode.UpperRightNode, out tempIntersect, out tempNormal, out tempDistance)) { result = true; // checks closest if (closestDistance > tempDistance) { closestDistance = tempDistance; closestIntersection = tempIntersect; closestNormal = tempNormal; } } } // checks lower left node. if (quadNode.LowerLeftNode != null) { if (TestUsingQuadTree(sourceCollide, quadNode.LowerLeftNode, out tempIntersect, out tempNormal, out tempDistance)) { result = true; // checks closest if (closestDistance > tempDistance) { closestDistance = tempDistance; closestIntersection = tempIntersect; closestNormal = tempNormal; } } } // checks lower right node. if (quadNode.LowerRightNode != null) { if (TestUsingQuadTree(sourceCollide, quadNode.LowerRightNode, out tempIntersect, out tempNormal, out tempDistance)) { result = true; // checks closest if (closestDistance > tempDistance) { closestDistance = tempDistance; closestIntersection = tempIntersect; closestNormal = tempNormal; } } } // checks vertices in quad node. if (quadNode.Contains(ref sourceCollide)) { // checks vertices with bounding sphere. if (sourceCollide is CollideSphere) { CollideSphere collide = sourceCollide as CollideSphere; // Hit test sphere with the model BoundingSphere sphere = collide.BoundingSphere; if (quadNode.Vertices != null) { if (TestSphereintersectModel(sphere, quadNode.Vertices, Matrix.Identity, out tempIntersect, out tempNormal, out tempDistance)) { result = true; // checks closest if (closestDistance > tempDistance) { closestDistance = tempDistance; closestIntersection = tempIntersect; closestNormal = tempNormal; } } } } // checks vertices with ray. else if (sourceCollide is CollideRay) { CollideRay collide = sourceCollide as CollideRay; if (quadNode.Vertices != null) { if (TestRayintersectModel(collide.Ray, quadNode.Vertices, Matrix.Identity, out tempIntersect, out tempNormal, out tempDistance)) { result = true; // checks closest if (closestDistance > tempDistance) { closestDistance = tempDistance; closestIntersection = tempIntersect; closestNormal = tempNormal; } } } } } // resolve final result. if (result) { distance = closestDistance; intersect = closestIntersection; normal = closestNormal; } return result; }
/// <summary> /// checks contaning. /// </summary> public bool Contains(ref CollideElement bounds) { if (bounds is CollideBox) { CollideBox target = bounds as CollideBox; return containBox.Intersects(target.BoundingBox); } else if (bounds is CollideSphere) { CollideSphere target = bounds as CollideSphere; return containBox.Intersects(target.BoundingSphere); } else if (bounds is CollideRay) { CollideRay target = bounds as CollideRay; return (containBox.Intersects(target.Ray) != null); } return false; }
/// <summary> /// Remove the collision element. /// </summary> public bool RemoveCollide(CollideElement collide) { return collideContainer.Remove(collide); }
/// <summary> /// it checks whether the collision element which has been included. /// </summary> public bool IsContain(CollideElement collide) { return (collideContainer.IndexOf(collide) == -1 ? false : true); }
/// <summary> /// Add a collsion element. /// </summary> public void AddCollide(CollideElement collide) { if (collideContainer.Contains(collide)) throw new InvalidOperationException("Already entry the collide"); collideContainer.Add(collide); collide.ParentLayer = this; }
/// <summary> /// Finds a contaning quad node. /// </summary> public QuadNode GetNodeContaining(ref CollideElement bounds) { if (rootNode != null) { if (rootNode.Contains(ref bounds) ) return rootNode.GetNodeContaining(ref bounds); } return null; }
/// <summary> /// it checks whether the collision element which has been included. /// </summary> public bool IsContain(CollideElement collide) { return(collideContainer.IndexOf(collide) == -1 ? false : true); }
/// <summary> /// Remove the collision element. /// </summary> public bool RemoveCollide(CollideElement collide) { return(collideContainer.Remove(collide)); }
/// <summary> /// It tests for collision among the collision elements which /// have been registered to the collision layer and returns the result. /// </summary> /// <param name="collide">Source collsion element</param> /// <param name="idLayer">Destination collison layer ID number</param> /// <param name="resultType">type of result</param> /// <returns>A result report</returns> public CollisionResult HitTest(CollideElement collide, int idLayer, ResultType resultType) { // Get the collide layer CollisionLayer layer = GetLayer(idLayer); return HitTest(collide, ref layer, resultType); }
/// <summary> /// It tests for collision among the collision elements which /// have been registered to the collision layer and returns the result. /// </summary> /// <param name="collide">Source collsion element</param> /// <param name="targetLayer">Target collison layer</param> /// <param name="resultType">type of result</param> /// <returns>A result report</returns> public CollisionResult HitTest(CollideElement collide, ref CollisionLayer targetLayer, ResultType resultType) { CollisionResult result = null; tempResult.Clear(); totalCollidingCount = 0; if (activeOn == false) { return(null); } if (collide == null) { throw new ArgumentNullException("collide"); } if (targetLayer == null) { throw new ArgumentNullException("targetLayer"); } // checking all collisions in current collision layer for (int i = 0; i < targetLayer.CollideCount; i++) { CollideElement targetCollide = targetLayer.GetCollide(i); // Skip ifself if (collide.Equals(targetCollide)) { continue; } else if (collide.Id != 0 && targetCollide.Id != 0) { if (collide.Id == targetCollide.Id) { continue; } } // If source collision is BoundingSphere if (collide is CollideSphere) { CollideSphere sourceCollideSphere = collide as CollideSphere; // Test with target sphere if (targetCollide is CollideSphere) { CollideSphere targetCollideSphere = targetCollide as CollideSphere; TestSphereintersectSphere(sourceCollideSphere, targetCollideSphere, ref tempResult); } // Test with target model else if (targetCollide is CollideModel) { CollideModel targetCollideModel = targetCollide as CollideModel; TestSphereintersectModel(sourceCollideSphere, targetCollideModel, ref tempResult); } // Test with target box else if (targetCollide is CollideBox) { CollideBox targetCollideBox = targetCollide as CollideBox; TestSphereintersectBox(sourceCollideSphere, targetCollideBox, ref tempResult); } // Test with target ray if (targetCollide is CollideRay) { CollideRay targetCollideRay = targetCollide as CollideRay; TestRayintersectSphere(targetCollideRay, sourceCollideSphere, ref tempResult); } } // If source collision is Ray else if (collide is CollideRay) { CollideRay sourceCollideRay = collide as CollideRay; // Test with target model if (targetCollide is CollideModel) { CollideModel targetCollideModel = targetCollide as CollideModel; TestRayintersectModel(sourceCollideRay, targetCollideModel, ref tempResult); } // Test with target sphere else if (targetCollide is CollideSphere) { CollideSphere targetCollideSphere = targetCollide as CollideSphere; TestRayintersectSphere(sourceCollideRay, targetCollideSphere, ref tempResult); } // Test with target box else if (targetCollide is CollideBox) { CollideBox targetCollideBox = targetCollide as CollideBox; TestRayintersectBox(sourceCollideRay, targetCollideBox, ref tempResult); } } // If source collision is Ray else if (collide is CollideBox) { CollideBox sourceCollideBox = collide as CollideBox; // Test with target sphere if (targetCollide is CollideSphere) { CollideSphere targetCollideSphere = targetCollide as CollideSphere; TestSphereintersectBox(targetCollideSphere, sourceCollideBox, ref tempResult); } // Test with target box else if (targetCollide is CollideBox) { CollideBox targetCollideBox = targetCollide as CollideBox; TestBoxintersectBox(sourceCollideBox, targetCollideBox, ref tempResult); } // Test with target ray else if (targetCollide is CollideRay) { CollideRay targetCollideRay = targetCollide as CollideRay; TestRayintersectBox(targetCollideRay, sourceCollideBox, ref tempResult); } } // To find the nearest detected collision. if (resultType == ResultType.NearestOne) { if (tempResult.collideCount > 0) { if (result == null) { result = new CollisionResult(); result.distance = float.MaxValue; } if (result.distance > tempResult.distance) { tempResult.CopyTo(ref result); } } } } return(result); }
/// <summary> /// It tests for collision among the collision elements which /// have been registered to the collision layer and returns the result. /// </summary> /// <param name="collide">Source collsion element</param> /// <param name="targetLayer">Target collison layer</param> /// <param name="resultType">type of result</param> /// <returns>A result report</returns> public CollisionResult HitTest(CollideElement collide, ref CollisionLayer targetLayer, ResultType resultType) { CollisionResult result = null; tempResult.Clear(); totalCollidingCount = 0; if (activeOn == false) return null; if (collide == null) { throw new ArgumentNullException("collide"); } if (targetLayer == null) { throw new ArgumentNullException("targetLayer"); } // checking all collisions in current collision layer for (int i = 0; i < targetLayer.CollideCount; i++) { CollideElement targetCollide = targetLayer.GetCollide(i); // Skip ifself if (collide.Equals(targetCollide)) { continue; } else if (collide.Id != 0 && targetCollide.Id != 0) { if (collide.Id == targetCollide.Id) continue; } // If source collision is BoundingSphere if (collide is CollideSphere) { CollideSphere sourceCollideSphere = collide as CollideSphere; // Test with target sphere if (targetCollide is CollideSphere) { CollideSphere targetCollideSphere = targetCollide as CollideSphere; TestSphereintersectSphere(sourceCollideSphere, targetCollideSphere, ref tempResult); } // Test with target model else if (targetCollide is CollideModel) { CollideModel targetCollideModel = targetCollide as CollideModel; TestSphereintersectModel(sourceCollideSphere, targetCollideModel, ref tempResult); } // Test with target box else if (targetCollide is CollideBox) { CollideBox targetCollideBox = targetCollide as CollideBox; TestSphereintersectBox(sourceCollideSphere, targetCollideBox, ref tempResult); } // Test with target ray if (targetCollide is CollideRay) { CollideRay targetCollideRay = targetCollide as CollideRay; TestRayintersectSphere(targetCollideRay, sourceCollideSphere, ref tempResult); } } // If source collision is Ray else if (collide is CollideRay) { CollideRay sourceCollideRay = collide as CollideRay; // Test with target model if (targetCollide is CollideModel) { CollideModel targetCollideModel = targetCollide as CollideModel; TestRayintersectModel(sourceCollideRay, targetCollideModel, ref tempResult); } // Test with target sphere else if (targetCollide is CollideSphere) { CollideSphere targetCollideSphere = targetCollide as CollideSphere; TestRayintersectSphere(sourceCollideRay, targetCollideSphere, ref tempResult); } // Test with target box else if (targetCollide is CollideBox) { CollideBox targetCollideBox = targetCollide as CollideBox; TestRayintersectBox(sourceCollideRay, targetCollideBox, ref tempResult); } } // If source collision is Ray else if (collide is CollideBox) { CollideBox sourceCollideBox = collide as CollideBox; // Test with target sphere if (targetCollide is CollideSphere) { CollideSphere targetCollideSphere = targetCollide as CollideSphere; TestSphereintersectBox(targetCollideSphere, sourceCollideBox, ref tempResult); } // Test with target box else if (targetCollide is CollideBox) { CollideBox targetCollideBox = targetCollide as CollideBox; TestBoxintersectBox(sourceCollideBox, targetCollideBox, ref tempResult); } // Test with target ray else if (targetCollide is CollideRay) { CollideRay targetCollideRay = targetCollide as CollideRay; TestRayintersectBox(targetCollideRay, sourceCollideBox, ref tempResult); } } // To find the nearest detected collision. if (resultType == ResultType.NearestOne) { if (tempResult.collideCount > 0) { if(result == null) { result = new CollisionResult(); result.distance = float.MaxValue; } if (result.distance > tempResult.distance) { tempResult.CopyTo(ref result); } } } } return result; }
protected bool TestUsingQuadTree(CollideElement sourceCollide, QuadNode quadNode, out Vector3 intersect, out Vector3 normal, out float distance) { bool result = false; float tempDistance = 0.0f; Vector3 tempIntersect = Vector3.Zero; Vector3 tempNormal = Vector3.Zero; float closestDistance = float.MaxValue; Vector3 closestIntersection = Vector3.Zero; Vector3 closestNormal = Vector3.Zero; distance = 0.0f; intersect = Vector3.Zero; normal = Vector3.Zero; // checks upper left node. if (quadNode.UpperLeftNode != null) { if (TestUsingQuadTree(sourceCollide, quadNode.UpperLeftNode, out tempIntersect, out tempNormal, out tempDistance)) { result = true; // checks closest if (closestDistance > tempDistance) { closestDistance = tempDistance; closestIntersection = tempIntersect; closestNormal = tempNormal; } } } // checks upper right node. if (quadNode.UpperRightNode != null) { if (TestUsingQuadTree(sourceCollide, quadNode.UpperRightNode, out tempIntersect, out tempNormal, out tempDistance)) { result = true; // checks closest if (closestDistance > tempDistance) { closestDistance = tempDistance; closestIntersection = tempIntersect; closestNormal = tempNormal; } } } // checks lower left node. if (quadNode.LowerLeftNode != null) { if (TestUsingQuadTree(sourceCollide, quadNode.LowerLeftNode, out tempIntersect, out tempNormal, out tempDistance)) { result = true; // checks closest if (closestDistance > tempDistance) { closestDistance = tempDistance; closestIntersection = tempIntersect; closestNormal = tempNormal; } } } // checks lower right node. if (quadNode.LowerRightNode != null) { if (TestUsingQuadTree(sourceCollide, quadNode.LowerRightNode, out tempIntersect, out tempNormal, out tempDistance)) { result = true; // checks closest if (closestDistance > tempDistance) { closestDistance = tempDistance; closestIntersection = tempIntersect; closestNormal = tempNormal; } } } // checks vertices in quad node. if (quadNode.Contains(ref sourceCollide)) { // checks vertices with bounding sphere. if (sourceCollide is CollideSphere) { CollideSphere collide = sourceCollide as CollideSphere; // Hit test sphere with the model BoundingSphere sphere = collide.BoundingSphere; if (quadNode.Vertices != null) { if (TestSphereintersectModel(sphere, quadNode.Vertices, Matrix.Identity, out tempIntersect, out tempNormal, out tempDistance)) { result = true; // checks closest if (closestDistance > tempDistance) { closestDistance = tempDistance; closestIntersection = tempIntersect; closestNormal = tempNormal; } } } } // checks vertices with ray. else if (sourceCollide is CollideRay) { CollideRay collide = sourceCollide as CollideRay; if (quadNode.Vertices != null) { if (TestRayintersectModel(collide.Ray, quadNode.Vertices, Matrix.Identity, out tempIntersect, out tempNormal, out tempDistance)) { result = true; // checks closest if (closestDistance > tempDistance) { closestDistance = tempDistance; closestIntersection = tempIntersect; closestNormal = tempNormal; } } } } } // resolve final result. if (result) { distance = closestDistance; intersect = closestIntersection; normal = closestNormal; } return(result); }
/// <summary> /// Finds a contaning quad node. /// </summary> public QuadNode GetNodeContaining(ref CollideElement bounds) { if (this.IsLeafNode == false) { // checks with upper left node. if (UpperLeftNode != null) { if (UpperLeftNode.Contains(ref bounds) ) return UpperLeftNode.GetNodeContaining(ref bounds); } // checks with upper right node. if (UpperRightNode != null) { if (UpperRightNode.Contains(ref bounds) ) return UpperRightNode.GetNodeContaining(ref bounds); } // checks with lower left node. if (LowerLeftNode != null) { if (LowerLeftNode.Contains(ref bounds) ) return LowerLeftNode.GetNodeContaining(ref bounds); } // checks with lower right node. if (LowerLeftNode != null) { if (LowerLeftNode.Contains(ref bounds) ) return LowerLeftNode.GetNodeContaining(ref bounds); } } // checks with this node. if( this.Contains(ref bounds) ) return this; return null; }