Hi,
I'm pretty new to Qt (and C++ also) and I hope I'm at the right place to ask this question
I'm trying to code a small Billiard game and wanted to reimplement collidesWithItem() as it is suggested in the Qt Documentation.
My problem is, that once I reimplemented the function, it doesn't seem to be called by Qt. It still uses the "default" one. The default one works fine, that's not the problem. It's purely for educational purposes that I'd like to know why it's not called. I did reimplement functions like paint() and they worked perfectly.
Here is my function:
bool Ball::collidesWithItem(const Ball *ball, Qt::ItemSelectionMode mode) const
{
if (this==ball) {return false;}
double epsilon = 100; // I put it this high, so I can see if it works. The Balls change color when colliding.
double distance = sqrt(pow(pos().x()- ball->pos().x(),2) + pow(pos().y()-ball->pos().y(),2));
if(distance-radius-ball->radius < epsilon) {return true;}
else {return false;}
}
bool Ball::collidesWithItem(const Ball *ball, Qt::ItemSelectionMode mode) const
{
if (this==ball) {return false;}
double epsilon = 100; // I put it this high, so I can see if it works. The Balls change color when colliding.
double distance = sqrt(pow(pos().x()- ball->pos().x(),2) + pow(pos().y()-ball->pos().y(),2));
if(distance-radius-ball->radius < epsilon) {return true;}
else {return false;}
}
To copy to clipboard, switch view to plain text mode
Is it linked to the fact that I'm not using QGraphicsItem as the first argument, but Ball? Shouldn't it be overloading in this case?
Here is the Ball.h:
#ifndef BALL_H
#define BALL_H
#include <QGraphicsItem>
{
public:
Ball();
Ball(double x, double y);
Ball(double x, double y, double angle);
~Ball();
bool collidesWithItem(const Ball *ball, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
qreal getRadius() const;
void setRadius(const qreal &value);
protected:
void advance(int step);
private:
qreal angle;
double position[2];
qreal speed[3];
qreal radius;
qreal mass;
};
#endif // BALL_H
#ifndef BALL_H
#define BALL_H
#include <QGraphicsItem>
class Ball : public QGraphicsItem
{
public:
Ball();
Ball(double x, double y);
Ball(double x, double y, double angle);
~Ball();
QRectF boundingRect() const;
QPainterPath shape() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget);
bool collidesWithItem(const Ball *ball, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
qreal getRadius() const;
void setRadius(const qreal &value);
protected:
void advance(int step);
private:
qreal angle;
double position[2];
qreal speed[3];
qreal radius;
qreal mass;
QColor color;
};
#endif // BALL_H
To copy to clipboard, switch view to plain text mode
Thank you in advance for your answer
Bookmarks