qt - Rotating a QGraphicsItem and finding the new position of it -
i working on tower defence game , trying shoot projectile turret. sprite of turret have looks this
what want make turret shot bullet point (for example attackdestination = qpointf(1000, 500);
for this, have class bullet, slot move:
void bullet::move() { int stepsize = 20; // how fast bullet moves double angle = rotation(); double dy = stepsize * qsin(qdegreestoradians(angle)); // x needs moved double dx = stepsize * qcos(qdegreestoradians(angle)); // y needs moved setpos(x() + dx, y() + dy); } which triggered qtimer.
i have slot in tower class (which stands turret)
void tower::attacktarget() { bullet *bullet = new bullet(); //getwidthmap() returns width of tower //getheightmap() returns height of tower bullet->setpos(x() + getwidthmap() /2, y()); qlinef line(qpointf(x() + getwidthmap() /2, y()), attackdestination); double angle =(-1) * line.angle(); bullet->setrotation(angle); this->setrotation(90 + angle); game->scene->additem(bullet); } i have rotated turret +90 degrees because initial position vertical , needs form angle (that of line ox) bullet. rotation happens clockwise.
the problem position of bullet relative turret when attacking.
without line this->setrotation(90 + angle); (first picture), (second picture)
as can see, bullets starting initial position of turret (when not rotated), because pos() function keeps initial x , y. how can fix ?




Comments
Post a Comment