Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-04-2012, 08:14 AM   #1 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 5
wadheraswati is on a distinguished road
Default check for UIIMage objects drawn using coregraphics intersection not UIImageview

I have two UIImage objects drawn using core graphics. I want to check whether they intersect i.e. if one is touching the other or if one is overlapping the other. The thing I know is to check the CGRect but it takes rectangular bounds but if i have irregular shape image - circle or star shape image. It shows intersection as YES even when it is not actually intersecting on iPhone screen because of the rectangular bounds.

What I need to do is develop a game in which an object should get deleted if that is on the top of all objects otherwise not. For this I have the only idea of checking the intersection, nothings else. I could check the proper intersection of lines using beziers but unable to do it regarding images. Please help.
wadheraswati is offline   Reply With Quote
Old 02-05-2012, 08:18 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

Maybe this will lead you down a path of more accurate collision detection -- http://maniacdev.com/2010/03/easy-ac...2d-collisions/
smithdale87 is offline   Reply With Quote
Old 02-05-2012, 10:24 AM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by wadheraswati View Post
I have two UIImage objects drawn using core graphics. I want to check whether they intersect i.e. if one is touching the other or if one is overlapping the other. The thing I know is to check the CGRect but it takes rectangular bounds but if i have irregular shape image - circle or star shape image. It shows intersection as YES even when it is not actually intersecting on iPhone screen because of the rectangular bounds.

What I need to do is develop a game in which an object should get deleted if that is on the top of all objects otherwise not. For this I have the only idea of checking the intersection, nothings else. I could check the proper intersection of lines using beziers but unable to do it regarding images. Please help.
Approach 1:


The slow but accurate way to do it is to create an 8-bit mask for each image, then either composite them into an off-screen context or write code to byte-by-byte AND the pixels of the two images together, looking for non-zero results.

To make it faster, you would probably want to create the masks and the context during setup, and re-use them during game play.

Approach 2:

It would probably be possible to distill it down to packed bit arrays, and shift the bit arrays and and them together to lower the memory footprint to 1 bit per pixel and speed things up. That would be some pretty hairy code to write though. Shades of my days as an assembler programmer.

Approach 3:

For non-rotated shapes, and ignoring holes in your shapes, you could probably calculate the minimum and maximum x extent of opaque pixels for every strip of pixels in both images, and then check for overlaps in each strip of pixels. That would be reasonably fast, and very accurate. It would still be a lot of work to write however.

Approach 4:

If your shapes are static, you should be able to come up with a specific algorithm that's much faster. Checking for the intersection of 2 circles, for example, you can pre-calculate the square of the distance at which they intersect, and then compare the square of the object's distances. You could also break up your shapes into a series of smaller bounding rectangles and check for intersections of those.

I think approach 3 would probably be the best general-purpose approach for non-rotating shapes. You'd pre-build a table of strip extents for each shape, and could determine intersection with something like 4•image_height integer comparisions, where image height would be the height of the smaller of the images you were comparing.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
cocoa touch, core graphics, ios 5, uiimage, xcode 4.2

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 398
18 members and 380 guests
Apptronics RBC, Atatator, chiataytuday, dre, FrankWeller, gwelmarten, ipodphone, jeroenkeij, jleannex55, kukat, LunarMoon, MAMN84, n00b, pbart, reficul, Retouchable, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:13 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0