Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

The exact intersection of objects

RossoRosso Posts: 10New Users
edited May 2013 in iOS SDK Development
Hello !

I check the intersection of objects using - CGRectIntersectsRect

One object (rectangle) is rotated. And created a bug with build boundingBox... He handles most of the body than there. But, I need the exact intersection of objects. How the way?

image

Thanks

Replies

  • riclabsriclabs New York CityPosts: 72New Users @
    edited May 2013
    you can try implementing circle collision instead of using CGRectIntersectsRect. here one i wrote for UIImageViews:
    
    -(CGFloat)PointDistance:(CGPoint)p1 p2:(CGPoint)p2
    {
        return sqrtf(powf(p1.x - p2.x, 2) + powf(p1.y - p2.y, 2));
    }
    
    -(BOOL)DidCollide:(UIImageView):obj1 obj2:(UIImageView)obj2 obj1Rad:(CGFloat)obj1Rad obj2Rad:(CGFloat)obj2Rad
    {
        CGFloat rad1 = obj1.frame.size.width * obj1Rad;
        CGFloat rad2 = obj2.frame.size.width * obj2Rad;
    
        if([self PointDistance:obj1.center p2:obj2.center] < (rad1 + rad2))
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
    
    you can modify this to take height into account or you can first check if the bounding box intersect first using CGRectIntersectRect then use circle collision changing the radius of the objects till you reach the desire collision.
    Post edited by riclabs on
  • Duncan CDuncan C Posts: 9,116Tutorial Authors, Registered Users @ @ @ @ @ @ @
    Rosso said:

    Hello !

    I check the intersection of objects using - CGRectIntersectsRect

    One object (rectangle) is rotated. And created a bug with build boundingBox... He handles most of the body than there. But, I need the exact intersection of objects. How the way?

    image

    Thanks

    What is your goal? To figure out the shape of the intersection, or just to detect if there is an intersection?

    In either case, you will need to convert a rectangle (which is an origin and a size, assumed to be always square to the display) into a closed polygon. That's easy. Then you need to apply a transformation matrix to the points of the polygon. The output of that step would be a transformed (rotated, shifted, scaled, etc) polygon. That isn't as easy, but it is straightforward.

    Then you need code that figures out if 2 polygons have any intersecting points. You should probably find a book on computer graphics and study it. This is a solved problem that's no doubt documented. Try a google search on detecting intersections between 2 closed polygons.

    If I were tackling this, I'd probably:

    Write code that

    detected crossing between the line segments in all the polygons.
    detected the case where polygon A is inside polygon B
    Detected the case where polygon B is inside polygon A.

    If any of those things is true, the 2 rotated rectangles intersect. If not, they don't

    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
Sign In or Register to comment.