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 10-11-2010, 05:33 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 14
Glych002 is on a distinguished road
Default Help with specific collision detection.

Ok, so I have 100 sprite images floating around, each has a different tag number and one image that is stationary. What I am trying to figure out is how to retrieve the tag number of any image that enters the frame of the static image, using the coda below.
Code:
if(CGRectIntersectsRect (static.frame, ball.frame)) {
        
    }
Any help or push in the correct direction would be great. I may be way off. I don't know if using tag is the way to go. Is there a way to have an image send info about itself upon entering the static.frame image?
Glych002 is offline   Reply With Quote
Old 10-11-2010, 05:48 PM   #2 (permalink)
Banned
 
Join Date: Jul 2009
Posts: 576
Not_Appy_At_All is on a distinguished road
Default

Quote:
Originally Posted by Glych002 View Post
Ok, so I have 100 sprite images floating around, each has a different tag number and one image that is stationary. What I am trying to figure out is how to retrieve the tag number of any image that enters the frame of the static image, using the coda below.
Code:
if(CGRectIntersectsRect (static.frame, ball.frame)) {
        
    }
Any help or push in the correct direction would be great. I may be way off. I don't know if using tag is the way to go. Is there a way to have an image send info about itself upon entering the static.frame image?

You can do it using a for loop that checks tags constantly...

This may help you get started....

"Detecting Subviews
You can loop through subviews of an existing view. This works especially well if you use the "tag" property on your views.
Code:
for (UIImageView *anImage in [self.view subviews]) { if (anImage.tag == 1) { // do something } }
"

iPhone SDK Examples
Not_Appy_At_All is offline   Reply With Quote
Old 10-11-2010, 06:08 PM   #3 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 14
Glych002 is on a distinguished road
Default

Quote:
Originally Posted by Not_Appy_At_All View Post
You can do it using a for loop that checks tags constantly...

This may help you get started....

"Detecting Subviews
You can loop through subviews of an existing view. This works especially well if you use the "tag" property on your views.
Code:
for (UIImageView *anImage in [self.view subviews]) { if (anImage.tag == 1) { // do something } }
"

iPhone SDK Examples
Well, that helps but I don't know what the tag number is of the image that entered the static frame.
Glych002 is offline   Reply With Quote
Old 10-11-2010, 06:49 PM   #4 (permalink)
Banned
 
Join Date: Jul 2009
Posts: 576
Not_Appy_At_All is on a distinguished road
Default

Quote:
Originally Posted by Glych002 View Post
Well, that helps but I don't know what the tag number is of the image that entered the static frame.
That's where the for loop comes in...

Code:
for(n = 1; n <= 100; n++)
{

   uiview* balls = [self.view viewWithTag:n];

    if(CGRectIntersectsRect (static.frame, balls.frame)) {
     
       do something here
        
    }
}
This assumes that your balls are tagged with 1 to 100...
and I added an "s" to ball to differentiate it from your original declaration

Last edited by Not_Appy_At_All; 10-11-2010 at 08:17 PM.
Not_Appy_At_All is offline   Reply With Quote
Old 10-11-2010, 08:17 PM   #5 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 14
Glych002 is on a distinguished road
Default

Quote:
Originally Posted by Not_Appy_At_All View Post
That's where the for loop comes in...

Code:
for(n = 1; n <= 100; n++)
{

   uiview* balls = [self.view viewWithTag:n;

    if(CGRectIntersectsRect (static.frame, balls.frame)) {
     
       do something here
        
    }
}
This assumes that your balls are tagged with 1 to 100...
and I added an "s" to ball to differentiate it from your original declaration
So I am trying understand what this code is doing.

I have 100 UIImageView *ball each has a different tag 1 - 100, one UIImageView *static. All the ball images move around they start intersecting the static image and the code that you provided me is looping though 1 - 100 changing the tag number of UIView* balls and searching for balls views that intersect. I just don't understand how collision detection is taking place when balls is not moving but ball is?
Glych002 is offline   Reply With Quote
Old 10-11-2010, 11:04 PM   #6 (permalink)
Banned
 
Join Date: Jul 2009
Posts: 576
Not_Appy_At_All is on a distinguished road
Default

Quote:
Originally Posted by Glych002 View Post
So I am trying understand what this code is doing.

I have 100 UIImageView *ball each has a different tag 1 - 100, one UIImageView *static. All the ball images move around they start intersecting the static image and the code that you provided me is looping though 1 - 100 changing the tag number of UIView* balls and searching for balls views that intersect. I just don't understand how collision detection is taking place when balls is not moving but ball is?

Because your are redefining your tagged view...as UIView* balls...to ID it and to then do something. It does not change your original declaration of ball.

Sort of like...my name is Charles...I also go by Chuck....same person, but different monikers.

The tagged view is moving...and was originally declared as ball. The UIView* balls is the tagged view, but with a different name. This sort of thing is done a lot when you try to attach to a view, especially one that was created with IB. To be able to manipulate the view, you need to redefine it using its tag number...which ID's that view. Then you can move it, reframe it, hide it etc etc...

Try the code and see if it works for you.

I've used it to attach to multiple moving objects...and is especially useful when you also need to determine touch...as in which object was touched.
Not_Appy_At_All is offline   Reply With Quote
Old 10-12-2010, 12:44 AM   #7 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 14
Glych002 is on a distinguished road
Default

Quote:
Originally Posted by Not_Appy_At_All View Post
Because your are redefining your tagged view...as UIView* balls...to ID it and to then do something. It does not change your original declaration of ball.

Sort of like...my name is Charles...I also go by Chuck....same person, but different monikers.

The tagged view is moving...and was originally declared as ball. The UIView* balls is the tagged view, but with a different name. This sort of thing is done a lot when you try to attach to a view, especially one that was created with IB. To be able to manipulate the view, you need to redefine it using its tag number...which ID's that view. Then you can move it, reframe it, hide it etc etc...

Try the code and see if it works for you.

I've used it to attach to multiple moving objects...and is especially useful when you also need to determine touch...as in which object was touched.
Ok, thanks that makes sense. I've added the code but cant get it to detect the intersection. Everything is a child of the same view. One thing that may be causing it is I created all the ball images with code not IB. But, the static image was made in IB. I don't think that should matter.
Glych002 is offline   Reply With Quote
Old 10-12-2010, 10:52 AM   #8 (permalink)
Banned
 
Join Date: Jul 2009
Posts: 576
Not_Appy_At_All is on a distinguished road
Default

Quote:
Originally Posted by Glych002 View Post
Ok, thanks that makes sense. I've added the code but cant get it to detect the intersection. Everything is a child of the same view. One thing that may be causing it is I created all the ball images with code not IB. But, the static image was made in IB. I don't think that should matter.

You need a trigger that will start the "for" loop. Then it will run the test to find the view.

You can run the "for" loop over and over, but that gets expensive...and cause hesitation in the game you're writing.

Also, I assume you did tag the balls...

ball.tag = 1, ball.tag = 2, etc etc until you get to 100.
Not_Appy_At_All is offline   Reply With Quote
Old 10-12-2010, 08:49 PM   #9 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 14
Glych002 is on a distinguished road
Default

Quote:
Originally Posted by Not_Appy_At_All View Post
You need a trigger that will start the "for" loop. Then it will run the test to find the view.

You can run the "for" loop over and over, but that gets expensive...and cause hesitation in the game you're writing.

Also, I assume you did tag the balls...

ball.tag = 1, ball.tag = 2, etc etc until you get to 100.
I can't get it to work, here is all the code.



Code:
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(colisionDetection) userInfo:nil repeats:YES];

    
    
}
- (IBAction)sliderButton {
    snowBall.center = CGPointMake(gunSlider.value, snowBall.center.y);
}
- (IBAction)fireGunButton{
    if (tagNumber == 100){
        tagNumber = 0;
        tagNumberRemove = 0;
        
    }
    tagNumber = tagNumber + 1;
    holdSliderValue = gunSlider.value;
    
    
	CGRect myImageRect = CGRectMake(gunSlider.value - 20, 410, 40, 40);
	mySnowBall = [[UIImageView alloc] initWithFrame:myImageRect];
    
	[mySnowBall setImage:[UIImage imageNamed:@"snowball.png"]];
    mySnowBall.tag = tagNumber;
	mySnowBall.opaque = YES; // explicitly opaque for performance
    
    [self.view addSubview:mySnowBall];
  
    CGRect myImageRect1 = CGRectMake( 120, 120, 40, 40);
	tree = [[UIImageView alloc] initWithFrame:myImageRect1];
    
	[tree setImage:[UIImage imageNamed:@"pineTree.png"]];
    
	tree.opaque = YES; // explicitly opaque for performance
    
        
    [self.view addSubview:tree];
    [self.view addSubview:mySnowBall];

    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDuration:1.0];
    
   mySnowBall.frame = CGRectMake(holdSliderValue ,110,2,2);
    //mySnowBall.frame = CGRectMake(0,0,2,2);
           [UIView commitAnimations];
     
    [NSTimer scheduledTimerWithTimeInterval:0.9 target:self selector:@selector(weaponStop) userInfo:nil repeats:NO];
	
	
}


- (void)weaponStop {
    
    tagNumberRemove = tagNumberRemove + 1;
    
   // [mySnowBall removeFromSuperview];
   [[self.view viewWithTag: tagNumberRemove] removeFromSuperview];
    NSLog(@"%i",tagNumberRemove);
}
- (void)colisionDetection {
       
    for(n = 1; n <= 100; n++)
    {
        
        UIView* balls = [self.view viewWithTag:n];
                         
                         if(CGRectIntersectsRect (tree.frame, balls.frame)) {
                             
                             NSLog(@"collision");
                                 
                                 }
                         }
    

    
}

I know that there is a way to do this but I can't see it.
Glych002 is offline   Reply With Quote
Old 10-13-2010, 12:03 AM   #10 (permalink)
Banned
 
Join Date: Jul 2009
Posts: 576
Not_Appy_At_All is on a distinguished road
Default

Quote:
Originally Posted by Glych002 View Post


I know that there is a way to do this but I can't see it.

Let me load it into the SDK and I'll try to get it to work. I'll probably get back to you on Wednesday.
Not_Appy_At_All is offline   Reply With Quote
Old 10-13-2010, 09:26 AM   #11 (permalink)
Banned
 
Join Date: Jul 2009
Posts: 576
Not_Appy_At_All is on a distinguished road
Default

Quote:
Originally Posted by Not_Appy_At_All View Post
Let me load it into the SDK and I'll try to get it to work. I'll probably get back to you on Wednesday.
OK...did it...and it runs fine after I made some minor changes.

The biggest problem as to why you're not getting collision detection is because you're shooting over the top of the tree.

The tree coordinates are 120x120 and your snowball ending coordinate y coordinate is 110.

This type of animation can only be detected at its ending coordinates...it's set and forget. So, where the ball lands is where the rectintersect takes place. Anything in between...the flight of the ball, cannot be intercepted for location on screen.

But for what you're doing it will work fine.

The minor changes I made are the more standard way of handling the animation's end and there's no need to remove each tagNumber. Removing the snowBall UIImageView removes the tagNumber by definition.

And I also substited random number generator for the gunSlider to make it easy to test out.

If you want a copy of the project, give me an email address and I'll send it out to you.
Not_Appy_At_All is offline   Reply With Quote
Reply

Bookmarks

Tags
collision detection, send data, sprite, sprite collision, tag

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: 354
10 members and 344 guests
alexP, gordo26, headkaze, mistergreen2011, nobstudio, Objective Zero, rayjeong, revg, Sloshmonster, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

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