
-----------------------------------
whoareyou
Wed May 18, 2011 5:36 pm

help - pong
-----------------------------------
I have a ball that can move around the screen and bounce off the edges of the console screen. Currently, I'm trying to add the paddle and make the ball bounce off of it. I tried to make an array to store the front side y-coordinates so if the ball touches it, then it would bounce off. My problem is that sometimes the ball gets stuck in the paddle and sometime it sticks to the paddle while moving down. Can anyone help me please?

ps. yes I'm using the HSA console, and sorry for the flashy graphics



        int x, y, w, h, vx, vy, timer;

        x = c.maxx () / 2;
        y = c.maxy () / 2;
        w = 50;
        h = w;
        vx = 5;
        vy = 5;

        int x2, y2, w2, h2;

        x2 = 0;
        y2 = 124;
        w2 = 30;
        h2 = 250;

        timer = 5;

        c.setColor (Color.black);

        c.fillOval (x, y, w, h);
        c.fillRect (x2, y2, w2, h2);

        int

-----------------------------------
Zren
Wed May 18, 2011 6:42 pm

RE:help - pong
-----------------------------------
Good programming practices no1. Use descriptive variables. When you have two things with x and y values (or something similar), the approach is to use paddleX and ballX, not x1, x2. The latter kind of implies the two are connected somehow.

This code is a mess.

[code]
        int start = y2; 

        for (int i = 0 ; i < vertical.length ; i++) 
        { 
            vertical [i] = start; 
            start++; 
        } 
[/code]

can be done with:

[code]
        for (int i = 0 ; i < vertical.length ; i++) 
        { 
            vertical [i] = y2 + i; 
        } 
[/code]

I don't really understand to point of those two arrays anyways...

Your actual problem is probably do to the fact your paddle is moving onto the ball, and your collision detection is making it freak out and flip back and forth.

You should only be flipping it when it goes first hits (when the ball detects it will hit the paddle after it's moved). Not after it's moved.

Logic should go something like:

[code]
while {
    getInput
    movePaddle(s)
    checkForBallsFutureCollision -> changeDirection?
    moveBall
    
    render
}
[/code]

Basically the responce is to let the ball phaze through the paddle if it didn't reach it in time.

-----------------------------------
whoareyou
Wed May 18, 2011 7:19 pm

Re: help - pong
-----------------------------------
So I took your advice into consideration and this is what I got:


        int ballx, bally, paddlex, paddley, ballw, ballh, paddlew, paddleh, velox, veloy, pause;

        ballx = c.maxx () / 2;
        bally = c.maxy () / 2;
        ballw = 50;
        ballh = ballw;

        paddlex = 0;
        paddley = c.maxy () / 4;
        paddlew = c.maxx () / 20;
        paddleh = c.maxy () / 2;

        velox = 5;
        veloy = 5;

        pause = 15;

        c.fillRect (paddlex, paddley, paddlew, paddleh);
        c.fillOval (ballx, bally, ballw, ballh);

        while (true)
        {

            if (ballx = paddley && bally = c.maxx () - ballw)
            {
                velox *= -1;
            }

            if (bally < 0 || bally >= c.maxy () - ballh)
            {
                veloy *= -1;
            }

            ballx += velox;
            bally += veloy;

            c.setColor (Color.black);

            //c.fillRect (paddlex, paddley, paddlew, paddleh);

            c.setColor (Color.red);

            c.fillOval (ballx, bally, ballw, ballh);

            Thread.sleep (pause);

            //c.clear ();
        }


This time around, the ball doesn't even detect the paddle and just pretends as if it isn't there. I inserted the code:


            if (ballx = paddley && bally = c.maxy () - ballh)
            {
                veloy *= -1;
            }


-----------------------------------
Zren
Wed May 18, 2011 8:53 pm

Re: help - pong
-----------------------------------
Read the latter part of my first post. I addressed the issue there. It's not the math, it's the method and order. You're moving to the new coordinates, then reacting, when you should be reacting before moving.

You could do a hackish approach (which I frown on) and don't do the flip if the ball is moving outwards...

-----------------------------------
whoareyou
Wed May 18, 2011 8:57 pm

Re: RE:help - pong
-----------------------------------

Your actual problem is probably do to the fact your paddle is moving onto the ball, and your collision detection is making it freak out and flip back and forth.

You should only be flipping it when it goes first hits (when the ball detects it will hit the paddle after it's moved). Not after it's moved.

Logic should go something like:



Isn't that what I'm doing? I have my if statements before I actually add the values to the new position of the ball.

-----------------------------------
Zren
Wed May 18, 2011 9:03 pm

RE:help - pong
-----------------------------------
Yes but your if statements are for where the ball currently is. Not where it will be. So basicaly it's like:

checkHere
move
render

which, logic wise, is no different from

move
checkhere

Which is checking after you've moved.

Make a couple new variables, that you use to calculate where it will be, then after you've done the CD, set the ball's coords there.

-----------------------------------
whoareyou
Wed May 18, 2011 9:15 pm

RE:help - pong
-----------------------------------
Okay, what I did was I created two new variables (ballx2 and bally2) 
        while (true)
        {

            ballx2 = ballx + velox;
            bally2 = bally + veloy;

            if (ballx2 = paddley && bally2 = c.maxx () - ballw)
            {
                velox *= -1;
            }

            if (bally2 < 0 || bally2 >= c.maxy () - ballh)
            {
                veloy *= -1;
            }

            ballx += velox;
            bally += veloy;

            c.setColor (Color.black);

            c.fillRect (paddlex, paddley, paddlew, paddleh);

            c.setColor (Color.red);

            c.fillOval (ballx, bally, ballw, ballh);

            Thread.sleep (pause);

            c.clear ();
        }


-----------------------------------
Zren
Wed May 18, 2011 9:38 pm

RE:help - pong
-----------------------------------
Only at the beginning? Does it start within the paddle? The flip could also be that it's not traveling fast enough to exit the paddle. Perhaps moving the ballx to the edge of the paddle when it collides there?

PS: there's no point to calculating twice.
ballx = ballx2

-----------------------------------
whoareyou
Thu May 19, 2011 9:14 am

RE:help - pong
-----------------------------------
No, the ball starts in the middle of the screen, but like after a few bounces around, when it touches the top edge and bottom edge of the paddle, it zig zags up/down the paddle.

-----------------------------------
Raknarg
Thu May 19, 2011 10:51 am

Re: help - pong
-----------------------------------
It might actually be because of your direction statement. If it's on the edge, and it doesn't get out before that statement comes back, it could get stuck in an infinite loop of being multiplied by -1. 
Idk if that's it, but you could try something else for the direction and see if it helps. something like:


if (bally2 = c.maxy () - ballh) 
            { 
                veloy = -1; 
            } 

if (balx2 = c.maxx () - ballw) 
            { 
                velox= -1; 
            }


Idk Java, so it's probs something like that

-----------------------------------
whoareyou
Thu May 19, 2011 2:41 pm

RE:help - pong
-----------------------------------
I tried changing it the direction statements, but those didn't make a difference.

I tried changing the speed much higher and that throws the ball off the screen after a while.

This is why at the beginning I had my array; I was setting up all the points of the paddle into the array and checking the see if the ballx and bally coordinates touch the points in the array, then the necessary action would be taken.

I can't seem to fix the problem by doing this with what I currently have - I just wanna know if it would be a good idea to do the arrays?

-----------------------------------
chrisbrown
Thu May 19, 2011 3:11 pm

Re: RE:help - pong
-----------------------------------
I just wanna know if it would be a good idea to do the arrays?This is a good example of a terrible idea, no offence intended. That would involve much more work than is necessary.

If you detect a collision, adjust the position of the ball so that it only touches the border instead of overlapping it. For example, if x < 0, set x to 0 and then reverse direction. That way, your collision detection corrects for invalid ball positions.

Similarly (and this will involve just a little more logic), if you detect a ball-paddle collision (i.e. that the ball is inside the paddle), move the ball so that it is located just outside the paddle on the side that it is coming from, and then reverse direction.

-----------------------------------
whoareyou
Thu May 19, 2011 3:51 pm

Re: help - pong
-----------------------------------
I added an if statement, so now it doesn't zig zag down the paddle (or at least, in the first 2 minutes of runtime that I've been watching) but now, you see how theres like a barrier at paddlex+paddlew?  Even though it's not hitting the paddle, the ball will still bounce off at paddlex+paddlew; it doesn't go to the spaces above and below the paddle.

ps. the random colors are so that I can see where the ball is moving :)
pss. i tried chrisbrown's way, and the result is the same ...

http://i.imgur.com/7eRDA.jpg


// The "Moving_Ball" class.
import java.awt.*;
import hsa.Console;

public class Moving_Ball
{
    static Console c;

    public static void main (String
