protected void BaseUpdateBoundingBox(Component pStart) { GameObject pNode = (GameObject)pStart; // point to ColTotal CollRect ColTotal = this.poCollObj.poColRect; ColTotal.width = 0; ColTotal.height = 0; // Get the first child pNode = (GameObject)Iterator.GetChild(pNode); if (pNode != null) { // Initialized the union to the first block ColTotal.Set(pNode.poCollObj.poColRect); while (pNode != null) { ColTotal.Union(pNode.poCollObj.poColRect); //move onto next sibling pNode = (GameObject)Iterator.GetSibling(pNode); } this.x = this.poCollObj.poColRect.x; this.y = this.poCollObj.poColRect.y; } }
//---------------------------------------------------------------------------------- // Methods //---------------------------------------------------------------------------------- public static bool Intersect(CollRect pRectA, CollRect pRectB) { bool status = false; float A_minx = pRectA.x - pRectA.width / 2; float A_maxx = pRectA.x + pRectA.width / 2; float A_miny = pRectA.y - pRectA.height / 2; float A_maxy = pRectA.y + pRectA.height / 2; float B_minx = pRectB.x - pRectB.width / 2; float B_maxx = pRectB.x + pRectB.width / 2; float B_miny = pRectB.y - pRectB.height / 2; float B_maxy = pRectB.y + pRectB.height / 2; // Trivial reject if ((B_maxx < A_minx) || (B_minx > A_maxx) || (B_maxy < A_miny) || (B_miny > A_maxy)) { status = false; } else { status = true; } return(status); }
//---------------------------------------------------------------------------------- // Static Methods //---------------------------------------------------------------------------------- public static void Collide(GameObject pTreeNodeA, GameObject pTreeNodeB) { // Cache A & B GameObject pNodeA = pTreeNodeA; GameObject pNodeB = pTreeNodeB; while (pNodeA != null) { // Start back at the top pNodeB = pTreeNodeB; while (pNodeB != null) { //Debug.WriteLine("ColPair: test: {0}, {1}", pNodeA.GetName(), pNodeB.GetName()); // Get Rectangles CollRect rectA = pNodeA.GetCollObj().poColRect; CollRect rectB = pNodeB.GetCollObj().poColRect; // Check if (CollRect.Intersect(rectA, rectB)) { // Success, liftoff!! pNodeA.Accept(pNodeB); break; } pNodeB = (GameObject)Iterator.GetSibling(pNodeB); } pNodeA = (GameObject)Iterator.GetSibling(pNodeA); } }
public void Union(CollRect pRect) { // To construct the union rectangle float minX; float maxX; float minY; float maxY; if ((this.x - this.width / 2) < (pRect.x - pRect.width / 2)) { minX = (this.x - this.width / 2); } else { minX = (pRect.x - pRect.width / 2); } if ((this.x + this.width / 2) > (pRect.x + pRect.width / 2)) { maxX = (this.x + this.width / 2); } else { maxX = (pRect.x + pRect.width / 2); } if ((this.y + this.height / 2) > (pRect.y + pRect.height / 2)) { maxY = (this.y + this.height / 2); } else { maxY = (pRect.y + pRect.height / 2); } if ((this.y - this.height / 2) < (pRect.y - pRect.height / 2)) { minY = (this.y - this.height / 2); } else { minY = (pRect.y - pRect.height / 2); } //Create the union this.width = (maxX - minX); this.height = (maxY - minY); //center this.x = minX + this.width / 2; this.y = minY + this.height / 2; // origin is in the upper-right corner }
public CollRect(CollRect pRect) : base(pRect) { }