简体   繁体   中英

problem with collision of two images

Well here is my problem: I have two images : flakeImage and ViewToRotate. What I want is that if flakeImage touches ViewToRotate, ViewToRotate.alpha=0.5; but when FlakeImage appears on the screen ViewToRotate.alpha=0.5; without touching it. I think it's a problem with my view beacause I have :

UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage];

here is the code :

UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage];

// use the random() function to randomize up our flake attributes
int startY = round(random() % 320);

// set the flake start position
flakeView.center = CGPointMake(490, startY);
flakeView.alpha = 1;

// put the flake in our main view
[self.view addSubview:flakeView];

[UIView beginAnimations:nil context:flakeView];
// set up how fast the flake will fall
[UIView setAnimationDuration:7 ];

// set the postion where flake will move to
flakeView.center = viewToRotate.center;

// set a stop callback so we can cleanup the flake when it reaches the
// end of its animation
[UIView setAnimationDelegate:self];
[UIView commitAnimations];

How can I solve this please ? if someone could help me it would be very cool.

CGRect has a intersection function. The frames of UIViews are CGRects.

if (CGRectIntersectsRect(view1.frame, view2.frame) == 1)
  NSLog(@"The views intersect");
else
  NSLog(@"The views do not intersect");

The problem I foresee is that if the rects have lots of whitespace, they will appear to intersect before they actually touch

Secondly, you should switch up to block animations. It's strongly encouraged

UIImageView* flakeView = [[[UIImageView alloc] initWithImage:flakeImage] autorelease];

// use the random() function to randomize up our flake attributes
int startY = round(random() % 320);

// set the flake start position
flakeView.center = CGPointMake(490, startY);
flakeView.alpha = 1;

// put the flake in our main view
[self.view addSubview:flakeView];

[UIView animateWithDuration:.7
                 animations:^ {
                         // set the postion where flake will move to
                         flakeView.center = viewToRotate.center;
                  };

Did this all from memory, no idea if there are errors.

Circular Collision:

a^2 + b^2 < c^2 means they collide

if(pow(view2.frame.origin.x - view1.frame.origin.x, 2) + 
    pow(view2.frame.origin.y - view1.frame.origin.y, 2) < 
    pow(view1.frame.size.width/2, 2))
{
     //collision
}
else
{
     //no collision
}

Again, all from memory, check for errors on your own

I have a bit of sophomoric experience with this, having written http://itunes.apple.com/us/app/balls/id372269039?mt=8 . If you check that app out, you will see a bit of the same problem. This topic is a pretty deep rabbit hole. WHen I started that app, I didn't even know how to write a decent game loop. You will need that first because you need to do precise time-step calculations. AFA the collisions, you update your model and view separately, so if you update the model and objects overlap, you need to back them up until they don't collide and then update your view with the result. If you plan to have a lot of colliding objects, you may hit a wall using UIViews. To complicate things more, if your objects are round, CGRectIntersectsRect won't exactly work. That complicates the math a bit, but it's not too bad. With my app, I found it quite difficult to get the physics to look realistic. THe problem became that ball A and B overlap, so you back them up, then they now intersect other balls, etc, etc. This link is a good starting point, but there are quite a few examples of code out there that "almost" work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM