Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem with Items coordinate system



  • While making a videogame I have had a problem due to the fact that probably I misunderstood how items coordinates system works. In particular in order to know the position in the scene of the mushroom(goomba) in the picture posted below, which is a gridded screenshot of my game, I wrote this line of code:

    std::cout << goomba->scenePos().toPoint().x() << " " << goomba->scenePos().toPoint().y() ;
    

    bea658c2-4aa8-4aa2-8e80-daccf6d3b43a-image.png
    Now every box in the grid is a square of 16*16 pixels and I set scene's origin so that it's in the top-left corner of that grid in particular it's the top-left corner of the square with number 0.
    As I have read in Qt Documentation and according to this picture of Summerfiled's book on Qt
    b66b41ea-834d-4030-a6d8-b826cb5f9431-image.png

    I have learned that an item's position is the coordinate of the item's center point in its parent's coordinate system but since the mushroom is a top level item its position is in scene coordinates .
    The code I wrote prints the top-left corner of the item in scene coordinates as if it is the position of the item but according to what I know it should print the center point of the item ,in scene coordinates, which I know to be its position.
    So what is the position of a top level item? Is it the top-left corner or the center point of the item?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you point where in the documentation you saw that center coordinate ?



  • Hi. Here

    7e8f4a0b-d16f-438a-a3db-51dea0f3ef68-image.png

    I am making confusion between an item's position and its center point probably beacuse I misunderstood this statement in documentation

    beb8ce3b-6d5b-48be-b425-3d20aee0cb73-image.png

    In particular if the scene is the parent of the item what is the item's position?


  • Lifetime Qt Champion

    This just means that every item has it's own coordinate system with 0,0 as it's center point used for transformations. So If you want to rotate an item in it's , you should put it's center at 0,0 - otherwise rotate() will not do what you expect.



  • Ok , I understood that. But if the item is a top level one its position in the scene is identified by its top-left point expressed in scene coordinates, am I right?


  • Lifetime Qt Champion

    Yes, that's correct.


Log in to reply