CGRectIntersectsrect, need help with image position

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

  1. moomy macrumors newbie

    Joined:
    Apr 28, 2010
    #1
    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.
     
  2. NickFalk macrumors 6502

    NickFalk

    Joined:
    Jun 9, 2004
    #2
    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 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    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

    NickFalk

    Joined:
    Jun 9, 2004
    #4
    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

    Joined:
    Apr 28, 2010
    #5
    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.
     
  6. NickFalk macrumors 6502

    NickFalk

    Joined:
    Jun 9, 2004
    #6
    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