Bouncing Ball program

It's a little program that render a bouncing ball, I did what they said but when I pressed run and nothing happened, then it ended with exit code:9.
Anyone have ideas what is going on here?Did I place any function in a wrong place?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <math.h>
using namespace std;

int width = 80;
int height = 80;
int ballX = 40;
int ballY = 40;
int forceX = 1;
int forceY = 1;

int distance(int x1, int y1, int x2, int y2){
    int xDelta = x1 - x2;
    int yDelta = y1 - y2;
    return sqrt(xDelta * xDelta + yDelta * yDelta);
}

void updatePhysics(){
    ballX += forceX;
    ballY += forceY;
}

void drawFrame(){
    for(int y = 0; y < height; y ++){
        for(int x = 0; x < width; x ++){
            
            //Render pixels.
            if(x == ballX && y == ballY){
                if(distance(ballX, ballY, x, y) < 6){ //If the pixel is inside the ellipse.
                    cout << '.'; //Render dots.
                }else if(distance(ballX, ballY, x, y) >= 6){ //Otherwise
                    cout << "x"; //render X marks.
                }
            }
            cout << endl;
            
            //I try to get the x and y output to see what's going on here
            // but I just cannot figure it out.
            //cout << "X = " << x << endl;
            //cout << "Y = " << y << endl;
        }
    }
}

int main(){
    while (true) {
        updatePhysics();
        drawFrame();
    }
    
    return 0;
}
Last edited on
1
2
            //cout << "X = " << x << endl;
            //cout << "Y = " << y << endl; 


The ball positions are ballX and ballY

Put the position logging code at the end of drawFrame. NOT inside the loop.
1
2
            cout << "ball position X = " << ballX << endl;
           cout << "ball position Y = " << ballY << endl;
Last edited on
I did what you suggest but still, nothing. : (
Maybe there's still something wrong.
Your output is mostly line 35 ; a blank line.

It is running. It will run forever. It is showing you many many blank lines, so fast, over and over, and sometimes there is something else being shown as well, but it goes by so fast you aren't seeing it.

You can prove this to yourself by replacing line 35 with:

cout << "LINE 35 " << endl;
Last edited on
Yes! So I should delete this line, maybe change something else...
I think you really hit some point.
Still working on it, but thanks!
Will update here when I actually solve this problem.
Finally solved!
The main error goes with my updateFrame() function where I forgot to add the if() to change the direction of the ellipse. And yes, the line 35 where I put a stupid cout << endl; that couse a massive output of the RETURN.

Update correct version of code here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 *  Bouncing Ellipse.cpp
 *  C++ First Programming
 *
 *  Draw a ellipse that bouce inside the canvas.
 *
 *  Created by Kape_01 on 5/30/16.
 *  Copyright © 2016 Kapes17. All rights reserved.
 */
 
#include <iostream>
#include <math.h>
using namespace std;
 
int width = 80; 
int height = 80; //Canvas size
int ballX = 40; //Ellipse x-axis
int ballY = 40; //Ellipse y-axis
int forceX = 1; //Change the direction of ellipse
int forceY = 1;
 
int distance(int x1, int y1, int x2, int y2){ 
    int xDelta = x1 - x2;
    int yDelta = y1 - y2;
    return sqrt(xDelta * xDelta + yDelta * yDelta); //Calculate the half radius of the ellipse
}
 
void updatePhysics(){
    if(ballX > width - 2 || ballX < 0){ //Keep the ellipse inside the canvas
        forceX = -forceX; //Change the direction
    }
        //Keep bouncing
        ballX += forceX;
        ballY += forceY;
}
 
void drawFrame(){
    for(int y = 0; y < height; y ++){
        for(int x = 0; x < width; x ++){
             
            if(distance(x, y * 2, ballX, ballY) < 4){
                cout << ' ';
            }else if(distance(x, y * 2, ballX, ballY) >= 4){
                cout << "x";
            }
        }
        cout << endl;
         
    }
}
 
int main(){
    while (true) {
        updatePhysics();
        drawFrame();
    }
     
    return 0;
}
Topic archived. No new replies allowed.