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 05-27-2011, 05:50 PM   #1 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 686
orange gold is an unknown quantity at this point
Default CGRect Collision With multiple CGRects ?v

I have created a game where I have a floor made of multiple images varying in heights.. (So the floor isn't flat)
Rather then opening a million if statements:
Code:
if (CGRectIntersectsRect(ball1.frame, floor1.frame)) {
}
if (CGRectIntersectsRect(ball1.frame, floor2.frame)) {
}
if (CGRectIntersectsRect(ball1.frame, floor3.frame)) {
}
if (CGRectIntersectsRect(ball1.frame, floor4.frame)) {
}
//and so on...
Is there a way I can check collision using 1 if statement? All of the UIImageView floor pieces are named, "floor1, floor2, floor3, floor4, and so on"

Thanks!
__________________
APPS4LIFE
search for me in the app store.
orange gold is offline   Reply With Quote
Old 05-27-2011, 09:43 PM   #2 (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 orange gold View Post
I have created a game where I have a floor made of multiple images varying in heights.. (So the floor isn't flat)
Rather then opening a million if statements:
Code:
if (CGRectIntersectsRect(ball1.frame, floor1.frame)) {
}
if (CGRectIntersectsRect(ball1.frame, floor2.frame)) {
}
if (CGRectIntersectsRect(ball1.frame, floor3.frame)) {
}
if (CGRectIntersectsRect(ball1.frame, floor4.frame)) {
}
//and so on...
Is there a way I can check collision using 1 if statement? All of the UIImageView floor pieces are named, "floor1, floor2, floor3, floor4, and so on"

Thanks!

Put your floor objects (or their frame rectangles) in an array. Then use a for loop to check the rects for intersections.

You can build the array of floor objects by marking them with tags, then looping though the tag numbers, calling -viewWithTag on the floor view's parent view.
__________________
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
Old 05-27-2011, 10:09 PM   #3 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 686
orange gold is an unknown quantity at this point
Default

Did you mean something like this? The program is crashing with "SIGBART" /:

Code:
NSMutableArray *floors = [NSMutableArray array];
    [floors addObject:wall1];
    [floors addObject:wall2];
    [floors addObject:wall3];
    
    
    for (int i = 0; i < 3; i++) {
    if (CGRectIntersectsRect(ball1.frame, [floors.self viewWithTag:i].frame)) {
Thanks again for your time so far..

Also, when I'm having levels that have all the way up to wall359 or higher, will looping still be a sufficient method? Thanks.
__________________
APPS4LIFE
search for me in the app store.
orange gold is offline   Reply With Quote
Old 05-28-2011, 07:09 AM   #4 (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 orange gold View Post
Did you mean something like this? The program is crashing with "SIGBART" /:

Code:
NSMutableArray *floors = [NSMutableArray array];
    [floors addObject:wall1];
    [floors addObject:wall2];
    [floors addObject:wall3];
    
    
    for (int i = 0; i < 3; i++) {
    if (CGRectIntersectsRect(ball1.frame, [floors.self viewWithTag:i].frame)) {
Thanks again for your time so far..

Also, when I'm having levels that have all the way up to wall359 or higher, will looping still be a sufficient method? Thanks.

No, you either need to use an array, or ask the owning view for the views using viewWithTag. You don't send an array the message viewWithTag. NSArray objects don't know about views.

Code:
UIView aFloor;
for (int i = 0; i < 3; i++) 
{
  aFloor = [floors objectAtIndex: i];
  if (CGRectIntersectsRect(ball1.frame, aFloor.frame))
   {
   }
}
__________________
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
Old 05-28-2011, 09:00 AM   #5 (permalink)
Gold Orange
 
orange gold's Avatar
 
Join Date: Sep 2008
Posts: 686
orange gold is an unknown quantity at this point
Default

Works great! But now, how can I detect which wall is being hit so I can do something to that wall (i.e. change the image to a cracked version)
Edit: using afloor works as long as it's still in the if statement, thanks!
__________________
APPS4LIFE
search for me in the app store.
orange gold is offline   Reply With Quote
Old 05-28-2011, 09:15 AM   #6 (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 orange gold View Post
Works great! But now, how can I detect which wall is being hit so I can do something to that wall (i.e. change the image to a cracked version)
Edit: using afloor works as long as it's still in the if statement, thanks!

Jeez, dude. Would you like the forum to write your app for you?

Do you want the code to do something unique for each intersection, or just see if ANY floor intersected?

If you want to stop checking on the first rectangle overlap you detect, add a break statement to the if (intersects) code. Set aFloor to nil at the top of the method so it's nil UNLESS a collision is detected. Then, when the loop exits (because of the break statement) aFloor will contain the floor of the collision, or nil if there were no collisions.
__________________
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

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: 344
6 members and 338 guests
doffing81, dre, iOS.Lover, jenniead38, Kirkout, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

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