How can I test if an oriented rectangle contains another oriented rectangle?

Posted by gronzzz on Game Development See other posts from Game Development or by gronzzz
Published on 2014-08-21T10:42:14Z Indexed on 2014/08/22 4:28 UTC
Read the original article Hit count: 349

Filed under:
|

I have the following situation:

To detect whether is the red rectangle is inside orange area I use this function:

- (BOOL)isTile:(CGPoint)tile insideCustomAreaMin:(CGPoint)min max:(CGPoint)max {
    if ((tile.x < min.x) ||
        (tile.x > max.x) ||
        (tile.y < min.y) ||
        (tile.y > max.y)) {
        NSLog(@" Object is out of custom area! ");
        return NO;
    }
    return YES;
}

But what if I need to detect whether the red tile is inside of the blue rectangle? I wrote this function which uses the world position:

- (BOOL)isTileInsidePlayableArea:(CGPoint)tile {

    // get world positions from tiles
    CGPoint rt = [[CoordinateFunctions shared] worldFromTile:ccp(24, 0)];
    CGPoint lb = [[CoordinateFunctions shared] worldFromTile:ccp(24, 48)];
    CGPoint worldTile = [[CoordinateFunctions shared] worldFromTile:tile];

    return [self isTile:worldTile insideCustomAreaMin:ccp(lb.x, lb.y) max:ccp(rt.x, rt.y)];
}

How could I do this without converting to the global position of the tiles?

© Game Development or respective owner

Related posts about mathematics

Related posts about objective-c