Pong game bugging out - mayby pointer to class : <p>get a problem with my code, this is just an extract from a larger piece of code involving OpenGL but still demonstrates the problem. It works without the collision detection call (col.detect()) - if you consider unbouncy walls working - but when I uncomment it the program breaks. Nothing seems to be wrong with the code itself it compiles just fine but doesn't work the way that I am expecting.</p>

<p>Thanks for all the help
Best Regards</p>

<p><strong>collision.h</strong></p>

<pre><code>#pragma once
#include "Ball.h"

class collision
{
public:
    collision();
    collision(Ball ball);
    void detect();
    ~collision();
private:
    Ball *point;
};
</code></pre>

<p><strong>Ball.h</strong></p>

<pre><code>#pragma once
class Ball
{
public:
    Ball();
    double getpx();
    double getpy();
    double getvx();
    double getvy();

    void setpx(const double px);
    void setpy(const double py);
    void setvx(const double vx);
    void setvy(const double vy);

    void update();
    ~Ball();
private:
    double position[2] = { 0, 0 };
    double velocity[2] = { 0.1, 0 };
};
</code></pre>

<p><strong>collision.cpp</strong></p>

<pre><code>#include "collision.h"

collision::collision()
{
}

collision::collision(Ball ball)
{
    point = &amp;ball;
}

void collision::detect()
{
    if (point-&gt;getpx() &gt; 1 || point-&gt;getpx() &lt; -1)
        point-&gt;setvx(-point-&gt;getvx());
    else if (point-&gt;getpy() &gt; 1 || point-&gt;getpy() &lt; -1)
        point-&gt;setvy(-point-&gt;getvy());
}

collision::~collision()
{
}
</code></pre>

<p><strong>Ball.cpp</strong></p>

<pre><code>#include "Ball.h"

Ball::Ball()
{
}

double Ball::getpx()
{
    return position[0];
}

double Ball::getpy()
{
    return position[1];
}

double Ball::getvx()
{
    return velocity[0];
}

double Ball::getvy()
{
    return velocity[1];
}

void Ball::setpx(const double px)
{
    position[0] = px;
}

void Ball::setpy(const double py)
{
    position[1] = py;
}

void Ball::setvx(const double vx)
{
    velocity[0] = vx;
}

void Ball::setvy(const double vy)
{
    velocity[1] = vy;
}

void Ball::update()
{
    position[0] += velocity[0];
    position[1] += velocity[1];
}

Ball::~Ball()
{
}
</code></pre>

<p><strong>main.cpp</strong></p>

<pre><code>#include &lt;iostream&gt;
#include "Ball.h"
#include "collision.h"

using namespace std;

int main()
{
    Ball tennis;
    collision col(tennis);

    while (true)
    {

        tennis.update();
        col.detect();

        cout &lt;&lt; tennis.getpx() &lt;&lt; endl;
        cin.get();
    }

    return 0;
}
</code></pre>
