Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nickculbertson

macrumors regular
Original poster
Nov 19, 2010
226
0
Nashville, TN
Hey all,
I'm having a problem using CGPointMake with CGRectIntersectsRect. When I move imageview itemtoss over imageview rightbox the collision is only detected if itemtoss's CGPointMake coordinates are contained within rightbox's frame. However, If itemtoss passes over rightbox in a CGPointMake animation, and itemtoss comes to rest at a point outside of the rightbox frame, no collision is detected. Can anyone think of a work around to detect a collision when the frames are "intersected" through a CGPointMake animation? I know I could move my imageview itemtoss in a chain of small increments but I like the fluidity of the one CGPointMake animation.

Code:
// car is an image view and newy is an int

-(IBAction)tossright{
	flight = CGPointMake(newy-40, car.center.y-250);
	itemtoss.center = flight; 
	
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:1.];
	[UIView setAnimationDelay:0.0];
	itemtoss.center = CGPointMake(newy+1000,car.center.y-400);	
	[UIView commitAnimations];

-(void)collisioncheck{
	if (CGRectIntersectsRect(itemtoss.frame, rightbox.frame))
	{
		NSLog(@"yes");
		itemtoss.hidden=YES;
		// collision!!
	}
}

- (void)viewDidLoad {
[NSTimer scheduledTimerWithTimeInterval:.02
                                   target:self
				   selector:@selector(collisioncheck)
				   userInfo:nil
				repeats:YES];}

Thanks,
Nick
 
Given a view, view.layer.presentationLayer will given you a close approximation of the version of a view's layer that is being displayed when the presentationLayer property is accessed.

A layer (class CALayer) has frame and bounds properties such like a view does.
 
Animations like that are not intended for interactive applications. Trying to make them interactive is possible, but requires you to be very skilled.

Having said that, I'm going to let you know what I understand about it so far:

1.) Once you call commitAnimations, the frame and bound properties of the views are set immediately to what you would expect them to be at the conclusion of the animation.

2.) There's something call the presentation layer of the UIView that is being animated. It has some properties that will let you know what the user is being shown as the current frame and bounds of the UIView as.

For the first game I released on the app store, I used the animation methods. It worked out alright, but I wasn't able to give the player as much control as I wanted. Collision detection I pretty much cheated on, instead knowing where an item would be 1 second later, setting a timer to expire in one second, and then after the one second checking if the player was in a position to get the item.

For my second game, one I'm still working on, I decided that instead of using animation methods, I'd just set the frames of everything myself 50x a second. This means what the user sees matches up with what the system says. So I can use methods like CGRectIntersectsRect without doing anything weird or finding information from the presentation layer. It runs perfectly on my iPhone 3GS, but so far the test levels I've made are pretty simple, so I haven't really given it a stress test yet. But with the iPhone 4 being a lot faster than my 3GS and the 4S being faster still, I feel confident that even if my 3GS can't handle the game I'd like to make, it doesn't really matter as I'll be replacing it with a 4S in less than a week (its expected to be delivered on the 14th :) )
 
Thanks for the feedback guys. I think I'll try moving the imageview with performSelector in a loop with smaller intervals.

maybe something like
Code:
//int Count1

-(IBAction)move{
if (CGRectIntersectsRect(itemtoss.frame, rightbox.frame))
	{      Count1=-120;
		NSLog(@"yes");
		itemtoss.hidden=YES;
		// collision!!
	}

if(Count1>=-40){
Count1--;
flight = CGPointMake(Count1+40, car.center.y-250);
	itemtoss.center = flight; 

[self performSelector:@selector (move) withObject:nil afterDelay:.01];
}else{
Count1=60;
}
}

Thanks,
Nick
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.