Billiard Ball bounce from bottom and right borders problem

Back to Problem Solutions forum

Leonid_Manieiev     2016-08-26 17:05:22

Hello everyone.

I've been working on problem 133(Billiard Ball) and I stuck with bouncing from bottom and right borders, as I may suggest by wrong output.

Here is the code of bounces(bottom, right)

Bounce from bottom border: if(Y >= H-R) { Y += 2 * (H - R - Y); Vy *= -1; }

Bounce from right border: if(X >= W-R) { X += 2 * (W - R - X); Vx *= -1; }

I'm thinking I made it right, did I?

I don't write code for top and left borders, because it can be found in the problem.

Quandray     2016-08-26 19:43:53
User avatar

Hi,

I'm not saying your code is correct, as I haven't tried it, but nothing sticks out as being wrong.

My code had ">" instead of ">=", but I can't see that making it fail.

If you "submit" your full code, on the problem page, we'll be able to see it.

Leonid_Manieiev     2016-08-26 21:01:18

Here it is:

#include <iostream>
using std::cout;
using std::endl;
#include <fstream>
using std::ifstream;
#include <cmath>
using std::sqrt;
using std::pow;
#include <utility>
using std::pair;

constexpr double dt = 0.0001;

typedef pair<double, double> coord_type;
typedef const size_t c_size_t;

coord_type find_final_position(c_size_t &W, c_size_t &H, double &X, 
                               double &Y, c_size_t &R, double &Vx,
                               double &Vy, c_size_t &A)
{
    double V = sqrt(pow(Vx, 2) + pow(Vy, 2));

    while(V > 0.0)
    {
        X += Vx * dt;
        if(X < R) 
        {
            X += 2 * (R - X);
            Vx *= -1;
        }   
        if(X >= W-R)    
        {
            X += 2 * (W - R - X);
            Vx *= -1;
        }

        Y += Vy * dt;
        if(Y < R) 
        {
            Y += 2 * (R - Y);
            Vy *= -1;
        }   
        if(Y >= H-R)    
        {
            Y += 2 * (H - R - Y);
            Vy *= -1;
        }

        V -= A * dt;
    }

    return {X, Y};
}

int main()
{
    size_t W{}, H{}, R{}, A{};
    double X{}, Y{}, Vx{}, Vy{};
    ifstream fin("data.txt");
    fin >> W >> H >> X >> Y >> R >> Vx >> Vy >> A;
    coord_type final_position = 
        find_final_position(W, H, X, Y, R, Vx, Vy, A);

    cout << final_position.first << " " << final_position.second;

    return 0;
}

Didn't want to post all code first, because of those who didn't solve this problem yet, to let them solve it by themselves.

Quandray     2016-08-27 06:45:51
User avatar

Hi,

What I meant for you to do, was to copy your code into the "your solution" area of the problem page, then press the submit button. That way, only people who have solved the problem can see your code.

Anyway, Vx & Vy are the velocities in the x & y direction, while V is the full velocity. Your code decreases V, but not Vx or Vy.

What do the {} do in the first two lines of main?

Try to read input from stdin so that your code can be run unchanged on the problem page using the C++ button. There should be somewhere in your compiler where you can tell it to take stdin from data.txt. On my compiler it's Debugging...Command Arguments...<data.txt

Leonid_Manieiev     2016-08-27 13:50:57

Hi, Quandray.

Sorry for delay.

1) My code decreases only V (not Vx and Vy), because it says in problem statement in "Slowing down" section V -= A * dt

2) {} those brackets just initialize variables with default-initialization, so I can be sure that they are not uninitialize.

3) I was sure, that all variables were initialize correctly, but I did what you said and read input from stdin. Nothing changed.

BTW, I copy my code into the "your solution" area and run it, just in case.

Any more suggestions?

Quandray     2016-08-27 14:16:01
User avatar

1) Although the problem statement doesn't say that Vx & Vy need to be decreased, they are the horizontal & vertical component of the overall velocity V. So, if the overall velocity V changes, so must Vx & Vy.

2) Regarding the initialization done with {}, thanks, I've not seen that before.

Leonid_Manieiev     2016-08-27 14:41:59

1) Damn, it's logical.

But how can I calculate a - angle between OX asix and vector of velocity?

And why I should calculate this angle to use it in Vx = V * cos(a) and Vy = V * sin(a) if I can simpy do this Vx = sqrt(pow(V, 2) - pow(Vy, 2)) and Vy = sqrt(pow(V, 2) - pow(Vx, 2)) isn't this one correct?

Quandray     2016-08-27 15:13:38
User avatar

I just worked out the percentage that V was reduced, then reduced Vx & Vy by the same percentage.

Leonid_Manieiev     2016-08-27 15:57:24

Thank you very much, Quandray!

Solve it with reduce percentage too.

Leonid_Manieiev     2016-08-27 15:58:12

Admin, please delete source code above if posible.

Please login and solve 5 problems to be able to post at forum