Collision detection across several layers

Collision detection across several layers
0.0 0



I am trying to implement some simple collision detection with CCRect::CCRectIntersectsRect.
My main player object resides on it’s own layer. A layer that does not move. The player stores a separateCCPoint for his position, so that the level class can offset the level-elements layer accordingly and thus emulate a camera rig.

The problem that I am facing is that CCPoint::getPosition* only returns the position relative to the layer the node is on. Hence, testing the collision of objects across layers becomes a chore.
I have successfully solved my problem by reverse-calculating all offsets involved and the code is posted below. However I wonder if there is an easier, more efficient way to solve this. It would be great to simply be able to convert a node’s position to the most top layer position or even screen space automatically. Is that possible?
My solution:
// Get the on-screen bounding box
CCRect collectibleRect = CCRectMake~~>getPosition.x + pParallaxNode~~>getPosition.x - ->getContentSize.width / 2
( it )>getScale ),
>getPosition.y + pParallaxNode>getPosition.y
->getContentSize.height / 2
( it )->getScale ),
( it )->getScale,
( **it )->getScale
’**it’ is a pointer to a CCSprite


Yeah! Every cc node has a nodeToWorld and nodeToParent method that does that for you! Be advise though, that after some testing, i notice that it uses some CPU to transverse everything (when in a game loop). Since my game used a lot of CPU and i wanted to optimize my game in every way i can, i ended up doing more or less the same way you did. But nothing stops you from using those methods! They are quite a helper and makes your code a lot more readable!

Cheers! (:


Great thanks, that method gives me back some readability.

I will also keep your advice in mind, since I might be iterating over a possibly huge vector of sprites.

By the way, is it possible to have a rotated CCRect? Haven’t looked into that myself yet though.