How to port CGRectInset to cocos2dx?

Hi!

I’m soing the platformer tutorial that I found at Ray Wenderlich page:
https://www.raywenderlich.com/15230/how-to-make-a-platform-game-like-super-mario-brothers-part-1

And I have no idea how to port this method:

-(CGRect)collisionBoundingBox {
  CGRect collisionBox = CGRectInset(self.boundingBox, 3, 0);
  CGPoint diff = ccpSub(self.desiredPosition, self.position);
  CGRect returnBoundingBox = CGRectOffset(collisionBox, diff.x, diff.y);
  return returnBoundingBox;
}

Especialy the part with the CGRectInset and CGRectOffset :frowning:
Can someone guide me how to port this part of the code?

From a quick google, it appears that CGRectInset shrinks the Rect and adjusts the origin, while CGRectOffset just moves the origin of the Rect. Because I can’t see a cocos equivalent in the definition of Rect, my solution would be to manually do the work.

So, something like

cocos2d::Rect MyClass::collisionBoundingBox() {
  cocos2d::Rect collisionBox = getBoundingBox();

  // CGRectInset
  collisionBox.size.width -= 6;
  collisionBox.origin.x += 3;
  
  cocos2d::Point diff = desiredPosition - getPosition();

  // CGRectOffset
  collisionBox.origin += diff;
  
  return collisionBox;
}

Hopefully that works.

1 Like

@grimfate
Works perfectly! Thanks!