简体   繁体   中英

IPhone sdk, more accurate collision detection and set the frame/bounds of UIImageView

Hey, Im having a big problem with my app at the moment, its all too inaccurate.

I have a image of a ballooon, https://dl.dropbox.com/u/2578642/Balloonedit.png

And i have a dart which if it collides into the balloon the game ends.

At the moment i am populating the image of the balloon with 8 UIImageViews. and i am detecting if the dart hits them, this was suppose to make it really accurate but its not, the dart pretty much passes through the balloon when its meant to collide, so i have a plan, is there any way to detect when the dart hits the actual image of the balloon not the UIImageView, or is there any way to draw a border around the balloon and detect if it hits that?

currently i am using this code to detect the collision:

if (CGRectIntersectsRect(pinend.frame, balloonbit1.frame)){
    [maintimer invalidate];
    accelManeger.delegate = nil;
[ball setImage:img];
[UIImageView beginAnimations:nil context:NULL];
[UIImageView setAnimationDuration:0.3];
ball.transform = CGAffineTransformMakeScale(2, 2);
[UIImageView commitAnimations];

}

So in one method there are 40 of these bits of code and as you can imagine it is not very accurate/fast to respond. So like i said is there a way to draw a border or something around the balloon and detect the collision between the border and dart? Because then i would imagine it would run a lot smother because it would only have to process 5 bits of code.

Thanks for any help.

This is a big Question so if you can answer it i will buy your app :)

Cheers, Harry :/

Alarm bells started ringing when I reached, "... 40 of these bits of code ...". Why don't you refactor that into one method that you call repeatedly?

Without knowing the nature of your app, I'm not sure if this is suitable, but can I suggest that you use a physics engine like Chipmunk Physics ? It'll let you define the collision shapes, gravity on moving objects, and trigger callbacks when things hit each other. This will be orders of magnitude faster than what you're doing right now.

Your balloons are almost perfect ellipse. I think you can use something like this to check the collision

(x-cx)**2 / a**2 + (y-cy)**2 / b**2 <= 1.0

where..

  • x, y = pin position
  • cx, cy = center of the balloon
  • a = balloon width / 2
  • b = balloon height / 2

However, I still have no idea why your code running not smooth. The problems might not be here.

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