CGRectIntersectsrect, need help with image position

Discussion in 'iOS Programming' started by moomy, Jun 25, 2010.

  1. moomy macrumors newbie

    Apr 28, 2010
    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...

    -(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)( +valueX);
    	if (newX > 320-BALL_RADIUS)
    		newX = 320-BALL_RADIUS;
    	if (newX < 0+BALL_RADIUS)
    		newX = 0+BALL_RADIUS;
    	int newY = (int)( -valueY);
    	if (newY > 460-BALL_RADIUS)
    		newY = 460-BALL_RADIUS;
    	if (newY < 0+BALL_RADIUS)
    		newY = 0+BALL_RADIUS;
    	CGPoint newCenter = CGPointMake(newX, newY);
  = newCenter; 
    	[self checkCollision];
    	if(CGRectIntersectsRect(drip.frame, plug.frame))  {
    		[ball removeFromSuperview];	

    any help would be super fantastic!!

  2. NickFalk macrumors 6502


    Jun 9, 2004
    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.
  3. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Why don't you change your collision detection so that it checks if the centers are within a certain distance of each other?
  4. NickFalk macrumors 6502


    Jun 9, 2004
    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...
  5. moomy thread starter macrumors newbie

    Apr 28, 2010
    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!!

  6. NickFalk macrumors 6502


    Jun 9, 2004
    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...

Share This Page