Football Collision

Discussion in 'iOS Development' started by hodgey87, Oct 26, 2010.

  1. hodgey87

    hodgey87 New Member

    Joined:
    Oct 21, 2007
    Messages:
    10
    Likes Received:
    0
    HI Guys,

    Ive got a simple game where a ball collides with a box at the bottom of the screen. I cant get the collision and score to work correctly ie if the ball hits the box you get 10 points.

    This is the code im using if anyone can help.

    Code:
    - (void)animateBall:(NSTimer *)theTimer {
    	ball.center = CGPointMake(ball.center.x+ballMovement.x, ball.center.y+ballMovement.y);
    	
    	BOOL paddleCollision = ball.center.y >= paddle.center.y - 16 &&
    	ball.center.y <= paddle.center.y + 16 &&
    	ball.center.x > paddle.center.x - 32 &&
    	ball.center.x < paddle.center.x + 32;
    	
    	
    	if(paddleCollision)
    		ballMovement.y = -ballMovement.y;
    	
    	if (ball.center.x > 310 || ball.center.x < 16)
    		ballMovement.x = -ballMovement.x;
    	
    	if (ball.center.y < 32)
    		ballMovement.y = -ballMovement.y;
    	if (ball.center.y > 444) {
    		[self pauseGame];
    		isPlaying = NO;
    		lives--;
    		livesLabel.text = [NSString stringWithFormat:@"%d", lives];
    		if (!lives) {
    			messageLabel.text = @"Game Over";
    		} else {
    			messageLabel.text = @"Ball Hit The Floor :(";
    		}
    		messageLabel.hidden = NO;
    	}
    	
    	if ( CGRectIntersectsRect(ball.frame, paddle.frame) )
    	{
    		
    		[self checkcollision];
    	}
    }
    
    Code:
    - (void)checkcollision {
    	if (CGRectIntersectsRect(ball.frame, paddle.frame)) {
    		score += 10;
    		scoreLabel.text = [NSString stringWithFormat:@"%d", score];
    		
    		if (ball.center.y < paddle.center.y) {
    			ballMovement.y = -ballMovement.y;
    			float diff=ball.center.x - paddle.center.x;
    			ballMovement.x=100;  // default if it hits the middle
    			if (diff>100) {
    				ballMovement.x=10;
    			}
    			if (diff<100) {
    				ballMovement.x=-10;
    			}
    		}
    		
    	}
    		 
    }
    
    
  2. hodgey87

    hodgey87 New Member

    Joined:
    Oct 21, 2007
    Messages:
    10
    Likes Received:
    0
    Ive got the collision to work right now, its just the score i cant figure out properly. Everytime the ball hits the box the score goes up like 50 at a time rather than just once

    Please Register or Log in to view images



    Code:
    - (void)startPlaying {
    	if (!lives) {
    		lives = 3;
    		score = 0;
    	}
    	scoreLabel.text = [NSString stringWithFormat:@"%05d", score];
    	livesLabel.text = [NSString stringWithFormat:@"%d", lives];
    	ball.center = CGPointMake(159, 239);
    	ballMovement = CGPointMake(4,4);
    	// choose whether the ball moves left to right or right to left
    	if (arc4random() % 100 < 50)
    		ballMovement.x = -ballMovement.x;
    	messageLabel.hidden = YES;
    	isPlaying = YES;
    	[self initialiseTimer];
    
    }
    - (void)pauseGame {
    	[theTimer invalidate];
    	theTimer = nil;
    }
    
    
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
        [super viewDidLoad];
    	
    	[self startPlaying];
    	
    	ballMovement = CGPointMake(4,4);
    	[self initialiseTimer];
    }
    - (void)initialiseTimer {
    	if (theTimer == nil) {
    		float theInterval = 1.0f/30.0f;
    		theTimer = [NSTimer scheduledTimerWithTimeInterval:theInterval target:self
    												  selector:@selector(animateBall:) userInfo:nil repeats:YES];
    	}
    }
    
    - (void)animateBall:(NSTimer *)theTimer {
    	ball.center = CGPointMake(ball.center.x+ballMovement.x, ball.center.y+ballMovement.y);
    	
    	BOOL paddleCollision = ball.center.y >= paddle.center.y - 16 &&
    	
    	ball.center.y <= paddle.center.y + 16 &&
    	ball.center.x > paddle.center.x - 32 &&
    	ball.center.x < paddle.center.x + 32;
    	
    	
    	if(paddleCollision)
    		ballMovement.y = -ballMovement.y;
    	
    	if (ball.center.x > 310 || ball.center.x < 16)
    		ballMovement.x = -ballMovement.x;
    	
    	if (ball.center.y < 32)
    		ballMovement.y = -ballMovement.y;
    	if (ball.center.y > 444) {
    		[self pauseGame];
    		isPlaying = NO;
    		lives--;
    		livesLabel.text = [NSString stringWithFormat:@"%d", lives];
    		if (!lives) {
    			messageLabel.text = @"Game Over";
    		} else {
    			messageLabel.text = @"Ball Hit The Floor :(";
    		}
    		messageLabel.hidden = NO;
    	}
    	[self checkcollision]; ---------I THINK THIS IS THE PROBLEM
    	
    }
    
    - (void)checkcollision {
    	if (CGRectIntersectsRect(ball.frame, paddle.frame)) {
    		score += 1;
    		scoreLabel.text = [NSString stringWithFormat:@"%d", score];
    		
    		/*
    		if (ball.center.y < paddle.center.y) {
    			ballMovement.y = -ballMovement.y;
    			
    			
    			float diff=ball.center.x - paddle.center.x;
    			ballMovement.x=100;  // default if it hits the middle
    			if (diff>100) {
    				ballMovement.x=10;
    			}
    			if (diff<100) {
    				ballMovement.x=-10;
    			}
    		
    			 }
    		*/
    	}
    		 
    }
    
    
    
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    	if (isPlaying) {
    		UITouch *touch = [[event allTouches] anyObject];
    		touchOffset = paddle.center.x -
    		[touch locationInView:touch.view].x;
    		
    	} else {
    		[self startPlaying];
    	}
    }
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    	if (isPlaying) {
    		UITouch *touch = [[event allTouches] anyObject];
    		float distanceMoved =([touch locationInView:touch.view].x + touchOffset) -paddle.center.x;
    		float newX = paddle.center.x + distanceMoved;
    		if (newX > 30 && newX < 290)
    			
    			paddle.center = CGPointMake( newX, paddle.center.y );
    		
    	}
    }
    
    
    - (void)didReceiveMemoryWarning {
    	// Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
    	
    	// Release any cached data, images, etc that aren't in use.
    }
    
    - (void)viewDidUnload {
    	// Release any retained subviews of the main view.
    	// e.g. self.myOutlet = nil;
    }
    
    
    - (void)dealloc {
    	[scoreLabel release];
    	[ball release];
    	[paddle release];
    	[livesLabel release];
    	[messageLabel release];
        [super dealloc];
    }
    
    @end
    Anyone help?
  3. pwnthe2G

    pwnthe2G Member

    Joined:
    Mar 7, 2009
    Messages:
    683
    Likes Received:
    5
    Device:
    4G iPod touch
    Code:
    - (void)checkcollision {
    	if (CGRectIntersectsRect(ball.frame, paddle.frame)) {
    		[B][U]score += 1;[/U][/B]
    		scoreLabel.text = [NSString stringWithFormat:@"%d", score];


    i think it may be as simple as score += 1. I dont program but i noticed theres no spaces anywhere else between the = and the number and nowhere do i see a +(unless i missed it) and also '+=' together doesnt seem right. I am prob. wrong but just something i noticed doesnt fit in with everything else
  4. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    First, use "score ++;" instead of "score += 1;". It requires fewer processor instructions to increment a number than to take a number and add one to it.

    Now, without taking a solid look at your code, this is what I see. You are checking for collision 30 times a second (ideally, I can't speak for actual performance). The timer likely fires several times before the ball rect can exit the paddle rect. Hence the rapidfire score increases.
  5. comet

    comet Well-Known Member

    Joined:
    Oct 18, 2010
    Messages:
    3,501
    Likes Received:
    355
    Device:
    Samsung Galaxy S4
    score += 1 is the problem.

Share This Page