kwigbo

Adventures in iOS!

2 notes &

Rectangle To Rectangle Collision Detection

There is a built in method for testing collisions between rectangles.

BOOL colide = CGRectIntersectsRect(rect1, rect2);
Here is a method I wrote before I new the former had existed.
- (BOOL) rect:(CGRect) rect collisionWithRect:(CGRect) rectTwo
{
	float rect_x1 = rect.origin.x;
	float rect_x2 = rect_x1+rect.size.width;
 
	float rect_y1 = rect.origin.y;
	float rect_y2 = rect_y1+rect.size.height;
 
	float rect2_x1 = rectTwo.origin.x;
	float rect2_x2 = rect2_x1+rectTwo.size.width;
 
	float rect2_y1 = rectTwo.origin.y;
	float rect2_y2 = rect2_y1+rectTwo.size.height;
 
	if((rect_x2 > rect2_x1 && rect_x1  rect2_y1 && rect_y1  

Filed under snippet iphone iphone dev

  1. kwigbo posted this