Collision of 2 imageView ios

321 Views Asked by At

I am having 2 images and i need to collide them properly. I tried many solutions but can't collide them properly.

Here are the images

Image 1Image 1

Image 2 Image 2

I tried CGRectIntersectsRect(), but its not working properly because images are colliding before they actually meet because of frame issue. Please help me.

I tried using Sprite Kit also but dint work for me.

Edit 1 :

Tried this code but its not working or is very slow :

- (void) checkIntersectionOfRect1 :(CGRect) frame1 Rect2 : (CGRect) frame2 {

    CGAffineTransform cg1 = CGAffineTransformIdentity;
    CGMutablePathRef *cgPath1 = CGPathCreateWithRect(frame1,&cg1);

    CGAffineTransform cg2 = CGAffineTransformIdentity;
    CGMutablePathRef *cgPath2 = CGPathCreateWithRect(frame2,&cg1);

    ANPathBitmap * bm1 = [[ANPathBitmap alloc] initWithPath:cgPath1];
    ANPathBitmap * bm2 = [[ANPathBitmap alloc] initWithPath:cgPath2];

    [bm1 generateBitmap];
    [bm2 generateBitmap];

    ANPathIntersection * intersection = [[ANPathIntersection alloc] initWithPathBitmap:bm1                                                                        anotherPath:bm2];

    if ([intersection pathLinesIntersect:&CGPointZero]) {
        NSLog(@"intersect");
        [self endGame];
    }
    else {
        NSLog(@"No intersection");
    }
}

Please someone help.

2

There are 2 best solutions below

0
On

This is an example of collision of two images, crow1 and crow2 are two uiimage which will collide with each other.

class GameViewController: UIViewController {

    var dynamicAnimator : UIDynamicAnimator!
    var collisionBehavior : UICollisionBehavior!
    var gravityBeehavior : UIGravityBehavior!

    @IBOutlet weak var aeroplane: UIImageView!
    @IBOutlet weak var crow1: UIImageView!
    @IBOutlet weak var crow2: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()




        dynamicAnimator = UIDynamicAnimator(referenceView: self.view)

        gravityBeehavior = UIGravityBehavior(items: [crow2])
        dynamicAnimator.addBehavior(gravityBeehavior)

        collisionBehavior = UICollisionBehavior(items: [crow1, crow2])
        collisionBehavior.translatesReferenceBoundsIntoBoundary = true
        dynamicAnimator.addBehavior(collisionBehavior)
}
0
On

Umm, Im not sure but. I checked your images, and downloaded them on my mac. They are transparent, so any space before your actual image which is transparent you cannot see and if that hits the image that will trigger the collision.

Step 1:

To be sure, set the background colour of your image view, to a stand out colour like blue and see where the problem happens. Then you would need to use preview and cut some space off, until it is exact to the point of the edge.

Step 2 :

Or change the view of your image view to, Redraw. That will make your image FIT your image view. Your code looks fine to me.

Step 3 :

Or you could use a NSTimer and set the time to 0.0001, and make a if statement.

//Try my methods, I'm pretty sure it  is not to do with your code but I am not a professional and maybe it is. You never know. 

You will know this, your code is quite complex , for you could have done it a much simpler way. If you have any questions, please ask! Thanks!