Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2011
    Posts
    28
    Rep Power
    0

    Default And another pong problem!

    I had a slight collision problem with my pong game. If the ball happens to collide with the "side" of the paddle, the ball will "stick" to the paddle and slowly move to the side of the paddle, and then fall off. I have a feeling this is happening because on the side, the ball can collide with the paddle for multiple loops of the game, because there is more than a single point it is touching, but I can't think of a way to fix this. Here is the code dealing with the collision:
    Java Code:
    if(ball.posY >= windowHeight-85 && ball.posX >= paddle.posX && ball.posX <= paddle.posX + paddle.width)
        	{
        		ball.speedY = -ball.speedY;
        		score++;
        		
        		if(score % 5 == 0)
        		{
        	    // speed the ball up
        	    if(ball.speedX > 0)
        	        ball.speedX++;
        	    else 
        	        ball.speedX--;
        	    if(ball.speedY > 0)
        	        ball.speedY++;
        	    else 
        	        ball.speedY--;
        		}
        	}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: And another pong problem!

    One possible solution: don't try to solve all collisions in one block. Choose the conditions which should make the speedX positive and make speedX definitely positive in that block using Math.abs. Same for speedX negative, speedY positive and speedY negative. This way you won't have your speed? flip-flopping directions.

Similar Threads

  1. Ping Pong Server/Client problem
    By simon.black325 in forum New To Java
    Replies: 0
    Last Post: 09-20-2011, 05:42 AM
  2. ping pong
    By ahmed_ijsh in forum Threads and Synchronization
    Replies: 2
    Last Post: 11-01-2010, 12:56 PM
  3. ping pong
    By ahmed_ijsh in forum Networking
    Replies: 0
    Last Post: 10-31-2010, 03:11 AM
  4. ping pong
    By ahmed_ijsh in forum Java Gaming
    Replies: 0
    Last Post: 10-31-2010, 03:10 AM
  5. Help with pong game
    By Eric in forum New To Java
    Replies: 2
    Last Post: 07-03-2007, 07:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •