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

moomy

macrumors newbie
Original poster
Apr 28, 2010
28
0
Hi everyone. well I have an image of a drip of water and an image of a plughole. The image of the drip moves freely with the accelerometer and the plug is a static image.

I can move the drip near the plug but it disappears as soon as the bounding box touches the plug bounding box. I want the two centres to be over each other before it disappears.... so it looks like the drip disappears into the plughole rather than just before it touches it.

here is a snippet from .m file...

Code:
-(void)awakeFromNib {
	[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0/30.0];
	[[UIAccelerometer sharedAccelerometer] setDelegate:self];
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
	valueX = acceleration.x*30.0;
	valueY = acceleration.y*30.0;
	
	int newX = (int)(ball.center.x +valueX);
	if (newX > 320-BALL_RADIUS)
		newX = 320-BALL_RADIUS;
	if (newX < 0+BALL_RADIUS)
		newX = 0+BALL_RADIUS;
	
	int newY = (int)(ball.center.y -valueY);
	if (newY > 460-BALL_RADIUS)
		newY = 460-BALL_RADIUS;
	if (newY < 0+BALL_RADIUS)
		newY = 0+BALL_RADIUS;
	
	CGPoint newCenter = CGPointMake(newX, newY);
	
	ball.center = newCenter; 
	[self checkCollision];
}

-(void)checkCollision{
	
	if(CGRectIntersectsRect(drip.frame, plug.frame))  {
		[ball removeFromSuperview];	
}
}


any help would be super fantastic!!

thanks
x.x.
 

NickFalk

macrumors 6502
Jun 9, 2004
347
1
If the drop is falling straight down how about making it into two different sprites/images and have the collision check on the upper one? You can experiment to find just how small the upper part of the drip needs to be for the best possible effect.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Why don't you change your collision detection so that it checks if the centers are within a certain distance of each other?
 

NickFalk

macrumors 6502
Jun 9, 2004
347
1
Good point PhoneyDeveloper.

@moomy if the plughole is static you don't really need to check for collisions at all. Just make the drop disappear when it's position is below a certain vertical-value and within the outerbounds of the plugholes width: if (y<40 && x>50 && x<150) for example...
 

moomy

macrumors newbie
Original poster
Apr 28, 2010
28
0
thanks all,

this is so helpful, I'm a bit of a n00b so didn't know you didn't need collision if one image was static!!

x.x.
 

NickFalk

macrumors 6502
Jun 9, 2004
347
1
thanks all,

this is so helpful, I'm a bit of a n00b so didn't know you didn't need collision if one image was static!!

x.x.
Don't worry, I don't think it's a particulary n00b mistake to make, it's fairly common to not think outside your normal routines...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.