Weihang Jian
Weihang Jian

Reputation: 8755

Confusing source code in TrollTech's Qt Tutorial Ch11

I am learning Qt from TrollTech's Qt Tutorial these days, and I'am confused about the source code of calculating the position of bullet in this page:

QRect CannonField::shotRect() const
{
    const double gravity = 4;

    double time = timerCount / 20.0;
    double velocity = shootForce;
    double radians = shootAngle * 3.14159265 / 180;

    double velx = velocity * cos(radians);
    double vely = velocity * sin(radians);
    double x0 = (barrelRect.right() + 5) * cos(radians);
    double y0 = (barrelRect.right() + 5) * sin(radians);
    double x = x0 + velx * time;
    double y = y0 + vely * time - 0.5 * gravity * time * time;

    QRect result(0, 0, 6, 6);
    result.moveCenter(QPoint(qRound(x), height() - 1 - qRound(y)));
    return result;
}

In the third-last line:

result.moveCenter(QPoint(qRound(x), height() - 1 - qRound(y)));

I think that - 1 is nonsense, isn't it?

Upvotes: 0

Views: 258

Answers (1)

Lol4t0
Lol4t0

Reputation: 12557

You have a widget:

Widget

If the height of widget is height, then y == 0 line is on the top of the widget and bottom line has y == height - 1 coordinate. So, if you want to show a point on the bottom line of the widget, you should set it y coordinate to height - 1.

Apparently, they use bottom of the widget as a ground level, so the bullet can be only above or on this level.

Upvotes: 3

Related Questions