Results 1 to 2 of 2

Thread: How to find pos relative to QPixMapItem after rotation

  1. #1
    Join Date
    Aug 2017
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default How to find pos relative to QPixMapItem after rotation

    I am working on a tower defence game and trying to shoot a projectile from a turret. The sprite of the turret I have looks like this :greenTower.png
    Also, the bullet looks like this blueBullet.png
    What I want to do is make the turret shot the bullet to a certain point (for example attackDestination = QPointF(1000, 500)

    For this, I have a class Bullet, with the slot move, which is connected to a QTimer:


    void Bullet::move()
    {

    int stepSize = 20; // how fast the bullet moves
    double angle = rotation();

    double dy = stepSize * qSin(qDegreesToRadians(angle)); // The X that needs to be moved
    double dx = stepSize * qCos(qDegreesToRadians(angle)); // The Y that needs to be moved

    setPos(x() + dx, y() + dy);
    }

    I also have a slot in the Tower class (which stands for the turret)

    void Tower::attackTarget()
    {
    Bullet *bullet = new Bullet();
    //getWidthMap() returns the width of the tower
    //getHeightMap() returns the height of the 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);

    }

    have rotated the turret by +90 degrees because its initial position is vertical and it needs to form an angle (that of the line with oX) just like the bullet. The rotation happens clockwise.
    The problem is with the position of the bullet relative to the turret when attacking.

    Without the line this->setRotation(90 + angle); (first picture), with it (second picture)

    Untitled.jpg
    Untitled.jpg

    As you can see, the bullets are starting from the initial position of the turret (when it was not rotated), because the pos() function keeps the initial X and Y. How can I fix that so the bullet is always shooting from the turret?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to find pos relative to QPixMapItem after rotation

    It was long time ago since I have dealt with special painting, so I might not be 100% with this but as I remember, the default rotation axis is not the center of your object but its upper left corner (of the bounding box).
    So what I think is happening is that your bullet is in the right position, but your turret is not (after turning), since it is not turning on its own axis but rather on the upper left corner of the turret bonding box.
    You need to move the axis point to be in the middle of your object.
    If you switch between the two screenshots back and forth you actually can see what I am talking about.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. QPixmapItem OpenGL black item
    By Talei in forum Qt Programming
    Replies: 4
    Last Post: 30th April 2013, 23:05
  2. Relative paths in Qt
    By drmath in forum Qt Programming
    Replies: 2
    Last Post: 30th June 2010, 16:50
  3. relative file name
    By benlyboy in forum Newbie
    Replies: 3
    Last Post: 28th June 2010, 03:44
  4. how to find mouse position relative to a cell
    By winkle99 in forum Qt Programming
    Replies: 0
    Last Post: 30th December 2009, 21:32
  5. relative position
    By franco.amato in forum Newbie
    Replies: 2
    Last Post: 8th December 2009, 18:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.