views:

348

answers:

2

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 :/

A: 

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.

Marcelo Cantos
Okay, thanks, i just downloaded Chipmunk and had a mess around with it. I don't quite understand how i would use this in my app?Also Basically what my app is: your a balloon and you control it by tilting the device, and you have to dodge darts which are falling down. i am using a NSTimer to move the darts and the accelerometer to move the balloon.Thanks :)
Harry
This is a perfect use-case for Chipmunk. [WARNING: shameless plug imminent...] Try my [MultiMaze](http://multimaze.com/) app, which uses Chipmunk Physics; it might give you a better sense of whether it's appropriate for you.
Marcelo Cantos
A: 

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.

Bird
Okay, Thanks. What should running through 40 bits of code be easy? maybe i need to create a timer for it and call it using that.
Harry