This post is part of my Mobile Game Development series. Start at the beginning to catch up. This series was inspired by the things I learned developing a new game for Android and iOS called Mirror Maze.
BoundingBox is a property on CCNode that describes the rectangular box at the boundary of the node. BoundingBox is of type CCRect, which has some really useful properties: MinX, MaxX, MinY, MaxY, and Center (a CCPoint).
It’s important to understand exactly what BoundingBox represents and what it doesn’t. Much like Position, BoundingBox is relative to the node’s parent. In fact, Position is a big part in calculating the node’s BoundingBox. Position, AnchorPoint, and ContentSize are all factored in to the node’s final BoundingBox. However, BoundingBox does NOT factor in any rotation or scale that has been applied to the node. It may seem strange that those seemingly key transforms are not factored in because it can be very useful to know what the bounding box of a node is after rotating and scaling it. And that is why there are a couple more flavors of BoundingBox that you can make use of.
BoundingBoxTransformedToParent does pretty much what you would expect based on the name. It returns the bounding box of the node relative to the parent AFTER applying positioning and all transformations (scale and rotation). If you don’t do any transformations on a node, this property will return the exact same result as BoundingBox would.
BoundingBoxTransformedToWorld returns the bounding box of the node relative to the world space, after applying positioning and all transformations. When you want to compare the bounding box of one node versus another node, this is likely the property you want to use. It will give you a more “real” version of the bounding box because the coordinates will be in relation to the coordinates of all the other nodes in the world space, whether they are children of the same parent or not.
It’s very important to understand exactly which bounding box property you want to use when you need to reference the bounding box of a node. I wish I could give you more direction around which one to use in what scenario, but the reality is that it is highly dependent on the specifics of your app and what you are trying to accomplish with it. Based on my experiences so far, I can say that I found myself using BoundingBoxTransformedToWorld in most cases, but not all. I tend to want to know where the node is in the world, rather than relative to the parent, when I grab the bounding box.
Working with the bounding box of a node is something you will likely do a LOT throughout the development of your game. Whether it’s for collision detection, relative positioning of other nodes, or reacting to touch interaction, this is one of the more important properties you will need to understand. The best way to really wrap your head around it is to play around with it or set a breakpoint and inspect the results of the different bounding box properties at different times within your application.