Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Has the ball hit my paddle?

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Has the ball hit my paddle?

    Hi all, I'm creating a game of Pong and I'm having trouble seeing whether the ball has hit the paddle. My game so far has a ball that bounces around the screen, and a paddle on the left which can be moved with the up and down key.

    Here is how I've drawn said objects:

            // The ball at the current x, y position (width, height) 
     
            g.setPaint( Color.red );
            g.fill( new Ellipse2D.Double( BALLX-HALF_BALL_SIZE, BALLY-HALF_BALL_SIZE, 
                    BALL_SIZE,     BALL_SIZE ) );
     
            // Paddle
     
            g.setPaint( Color.pink );
            g.fill( new Rectangle2D.Double( PADDLEX, PADDLEY, PADDLE_WIDTH, PADDLE_HEIGHT ) );

    BALLX & PADDLEX = X coordinate of the ball/paddle and same goes to BALLY & PADDLEY with the y coordinate.

    What would be the best way of checking whether the ball has collided with paddle?

    I tried this:

        public Rectangle getPaddleBounds(){
            return new Rectangle(PADDLEX,PADDLEY,PADDLE_WIDTH,PADDLE_HEIGHT);
        }
     
        public Rectangle getBallBounds(){
            return new Rectangle(BALLX,BALLY,BALL_SIZE,BALL_SIZE);
        }
     
        public void checkCollisions(){
            if (getPaddleBounds().intersects(getBallBounds()))           
                collision = true;
            else collision = false;
        }

    but it didn't work (and I realise the ball isn't a rectangle but ellipse doesn't work). Any help would be greatly appreciated

    Thank you


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Has the ball hit my paddle?

    Seems reasonable to me. What exactly do you mean when you say it "doesn't work"?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Has the ball hit my paddle?

    it didn't work
    Can you describe what happens? Does one shape completly ignore the other? Or is there slight penetration befor e the collision is detected?
    Is the collision variable ever set true? When does that happen? Add a println to show the values when it happens.

    Try debugging the code by adding println statements to print out the values of the bounds of each shape so you see what the computer is seeing.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Has the ball hit my paddle?

    Ahh my first post on this forum and it's a shambolic one. I apologise. I didn't call the checkCollisions(); function while running the main program loop.
    I added this and now it's working fine.

    One last thing, when I open my program I am unable to move the paddle, firstly I have to click on the Terminal window and then back onto the application to be able to play the game. Is there anyway I don't have to do this?

    Attached Images Attached Images

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Has the ball hit my paddle?

    How are you controlling the movements of the shapes?
    What kind of listeners are you using? Does the component with the listeners have the focus?
    There are methods you can call to request that the component get the focus.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Has the ball hit my paddle?

    Quote Originally Posted by Norm View Post
    How are you controlling the movements of the shapes?
    What kind of listeners are you using? Does the component with the listeners have the focus?
    There are methods you can call to request that the component get the focus.
    I am using this listener

        class Transaction implements KeyListener  // When character typed 
        {
            public void keyPressed( KeyEvent e)      // Obey this method 
            {
                // Key typed includes specials 
                switch ( e.getKeyCode() )              // Character is 
                {
                    case KeyEvent.VK_Q:                 // Q key
                    PADDLEY = PADDLEY -10;
                    break;
                    case KeyEvent.VK_A:               // A key 
                    PADDLEY = PADDLEY + 10;
                    break;
                    case KeyEvent.VK_UP:                 // Up arrow 
                    PADDLERIGHTY = PADDLERIGHTY -10;
                    break;
                    case KeyEvent.VK_DOWN:                 // Down arrow 
                    PADDLERIGHTY = PADDLERIGHTY +10;
                    break;
                }
                // x,y could send to a server instead of calling  
                // Call update method 
                repaint();
            }

    are you saying there is a way I can set the focus to this listener?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Has the ball hit my paddle?

    The component with the listener that is shown in GUI must have the focus.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. 2D Ball Animation
    By Deidelus in forum AWT / Java Swing
    Replies: 6
    Last Post: November 20th, 2011, 05:07 PM
  2. [SOLVED] Pong: Ricochet off Paddle Issue
    By Staticity in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 25th, 2011, 06:57 AM
  3. Getting my paddle to move left and right in a straight line-STUCK
    By warnexus in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 20th, 2011, 08:06 PM
  4. Need help with a third ball in game.
    By vlan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 12th, 2010, 03:35 PM
  5. breakout paddle algorithm
    By Brain_Child in forum Algorithms & Recursion
    Replies: 0
    Last Post: December 30th, 2009, 05:24 AM