public static bool Intersect(CollisionRectangle ColRectA, CollisionRectangle ColRectB) { bool status = false; float A_minx = ColRectA.x - ColRectA.width / 2; float A_maxx = ColRectA.x + ColRectA.width / 2; float A_miny = ColRectA.y - ColRectA.height / 2; float A_maxy = ColRectA.y + ColRectA.height / 2; float B_minx = ColRectB.x - ColRectB.width / 2; float B_maxx = ColRectB.x + ColRectB.width / 2; float B_miny = ColRectB.y - ColRectB.height / 2; float B_maxy = ColRectB.y + ColRectB.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); }
public static void detectCollision(GameObject pSafeTreeA, GameObject pSafeTreeB) { // Debug.WriteLine("Dectecting collision"); // A vs B GameObject pNodeA = pSafeTreeA; GameObject pNodeB = pSafeTreeB; // Debug.WriteLine("\nColPair: start {0}, {1}", pNodeA.cGameObjectName, pNodeB.cGameObjectName); while (pNodeA != null) { // Restart compare pNodeB = pSafeTreeB; while (pNodeB != null) { // who is being tested? // Debug.WriteLine("ColPair: collide: {0}, {1}", pNodeA.cGameObjectName, pNodeB.cGameObjectName); // Get rectangles CollisionRectangle rectA = pNodeA.cCollisionObj.cCollisionRectangle; CollisionRectangle rectB = pNodeB.cCollisionObj.cCollisionRectangle; // test them if (CollisionRectangle.Intersect(rectA, rectB)) { // Boom - it works (Visitor in Action) pNodeA.Accept(pNodeB); break; } pNodeB = (GameObject)pNodeB.pSibling; } pNodeA = (GameObject)pNodeA.pSibling; } }
protected void updateUnionBox() { PCSNode node = (PCSNode)this; node = node.pChild; if (node == null) { this.cCollisionObj.cCollisionRectangle.Set(0, 0, 0, 0); this.x = 0; this.y = 0; } else { GameObject gameObj = (GameObject)node; CollisionRectangle collisionTotal = this.cCollisionObj.cCollisionRectangle; collisionTotal.Set(gameObj.cCollisionObj.cCollisionRectangle); while (node != null) { gameObj = (GameObject)node; collisionTotal.Union(gameObj.cCollisionObj.cCollisionRectangle); node = node.pSibling; } this.x = this.cCollisionObj.cCollisionRectangle.x; this.y = this.cCollisionObj.cCollisionRectangle.y; } }
public void Union(CollisionRectangle ColRect) { float minX; float minY; float maxX; float maxY; if ((this.x - this.width / 2) < (ColRect.x - ColRect.width / 2)) { minX = (this.x - this.width / 2); } else { minX = (ColRect.x - ColRect.width / 2); } if ((this.x + this.width / 2) > (ColRect.x + ColRect.width / 2)) { maxX = (this.x + this.width / 2); } else { maxX = (ColRect.x + ColRect.width / 2); } if ((this.y + this.height / 2) > (ColRect.y + ColRect.height / 2)) { maxY = (this.y + this.height / 2); } else { maxY = (ColRect.y + ColRect.height / 2); } if ((this.y - this.height / 2) < (ColRect.y - ColRect.height / 2)) { minY = (this.y - this.height / 2); } else { minY = (ColRect.y - ColRect.height / 2); } this.width = (maxX - minX); this.height = (maxY - minY); this.x = minX + this.width / 2; this.y = minY + this.height / 2; }
// Extract the rectangle co ordinates of the sprite in the proxy sprite // Set those to the sprite box. public CollisionObject(ProxySprite mProxySprite) { Debug.Assert(mProxySprite != null); //Create a BoxSprite Sprite mSprite = mProxySprite.rcSprite; Debug.Assert(mSprite != null); cSpriteBox = SpriteBoxManager.add(SpriteBox.SpriteBoxName.Box, mSprite.screenRect); Debug.Assert(this.cSpriteBox != null); // Set the Collision Rectangle this.cCollisionRectangle = new CollisionRectangle(cSpriteBox.cSpriteBoxRect); Debug.Assert(this.cCollisionRectangle != null); // this.cSpriteBox.setColor(1, 1, 1); // this.cSpriteBox.setColor(Unit.spriteBoxColor); }