Results 1 to 8 of 8

Thread: QGraphicsView coordinate system

  1. #1
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default QGraphicsView coordinate system

    I'm having a real hard time with finding the correct positioning of items in a scene.
    1-rotate an item based on the angle of the mouse-to-item position. = Succesfull!
    2-create an item, and send it from it's parent position to in direction to the mouse = Problems...

    The following code is successful in other platforms such as Adobe Flash:
    Qt Code:
    1. // When mouse is clicked =
    2.  
    3. Shot *tiro = new Shot();
    4. double velocity = 10;
    5. double radians = angle * PI / 180; // where angle is successfully defined previously
    6.  
    7. double velx = velocity * cos(radians);
    8. double vely = velocity * sin(radians);
    9. double x0 = ((redShip->scenePos().x()+redShip->boundingRect().width()/2)+50 ) * cos(radians);
    10. double y0 = ((redShip->scenePos().y()+redShip->boundingRect().width()/2)+50 ) * sin(radians);
    11. double x = x0 + velx ;
    12. double y = y0 + vely ;
    13.  
    14. tiro->newShot(scene,x0,y0);
    15. tiro->setDirection(x,x);
    To copy to clipboard, switch view to plain text mode 

    Shot.cpp:
    Qt Code:
    1. void Shot::newShot(QGraphicsScene * scene, double x, double y)
    2. {
    3. ball = new QGraphicsEllipseItem(x,x,10,10);
    4.  
    5. scene->addItem(ball);
    6.  
    7. }
    8.  
    9. void Shot::setDirection(double x, double y)
    10. {
    11. dirx = x;
    12. diry = y;
    13. }
    14.  
    15. //updatePos() is called every second by a QTimer in Shot::Shot()
    16. void Shot::updatePos()
    17. {
    18. x += dirx;
    19. y += diry;
    20. ball->setPos(x,y);
    21. }
    To copy to clipboard, switch view to plain text mode 

    The above just creates a shot anywhere in a up-down / left-right diagonal
    and sends it following the said diagonal. What am I doing wrong (never Qt's fault...)

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView coordinate system

    If I understand correctly then your problems are caused by the fact that you are trying to make the missile a child of the cannon (or whatever you have there). The problem is that you are probably handling mouse events in the scene (in scene coordinates) but trying to move the ball in cannon's coordinates. This is of course doable, all you need is to map coordinates from the scene to the item but if you rotate the cannon again (or whatever you have there) the ball will follow the rotation which I don't think is something you want.

    To understand Graphics View coordinate system, imagine that the scene is a sheet of paper that has an origin and x and y axis marked on it. Each item is another sheet of paper with its own x and y axis and origin. The origin of the item is pinned to the position of the item on the piece of paper representing its parent. So if you have an item that has its own children, you pin them to the parent item and not to the scene. Now if you start rotating or moving the sheet representing the parent you will notice all children also change their position. It's a good idea to actually take a few pieces of paper (or better yet transparency foils or something that is semi-transparent), a pencil and a couple of pins and repeat all this as an exercise to understand how it all behaves.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsView coordinate system

    I watched the ICS video on QGraphicsView and I do understand the relationship between child/parent items.
    I traced the new items position and it starts at 0, and that would mean that it is a child of my reference item(the "cannon"). But I don't understand why, if I add it to the scene and not to the item(the "cannon").

    Qt Code:
    1. void Shot::newShot(QGraphicsScene * scene, double x, double y)
    2. {
    3. ball = new QGraphicsEllipseItem(x,x,10,10);
    4.  
    5. scene->addItem(ball); // adds new item to the scene, not to the GraphicsItem("cannon")
    6. }
    To copy to clipboard, switch view to plain text mode 

    Why does that happen?

    EDIT: Another thing I dont understand is this:
    problem.png

    between the walls is what the scene is supposed to be, 400x400. But it re-sizes to the whole view and the red cannon instead of being inside the scene, is outside. I set the app to fullscreen.
    Last edited by been_1990; 31st October 2010 at 01:42.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView coordinate system

    I'd say the code should be:
    Qt Code:
    1. void Shot::newShot(QGraphicsScene * scene, double x, double y)
    2. {
    3. ball = new QGraphicsEllipseItem(-5,-5,10,10);
    4. scene->addItem(ball); // adds new item to the scene, not to the GraphicsItem("cannon")
    5. ball->setPos(x,x);
    6. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsView coordinate system

    Adding "ball->setPos(x,x) " does not change anything.

  6. #6
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView coordinate system

    First, read this: http://doc.qt.nokia.com/4.5/graphicsview.html It contains a very good discussion on the various coordinate systems employed and how they relate to one another.

    Second, statements like

    The following code is successful in other platforms such as Adobe Flash
    are meaningless at best, disastrous at worst. There are dozens/scores/hundreds of software toolkits out there that perform various graphics tasks, and the way they treat coordinate systems is different in every case. You can't expect to simply plop existing code written using some totally different geometry system into another framework and expect it to work the same. Or to work at all.

    You have to understand what you're doing. You can't code by cutting and pasting, at least not until you are thoroughly grounded in the tools you are using.

  7. #7
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsView coordinate system

    The following code is successful in other platforms such as Adobe Flash
    Sorry, the math is successful in other platforms. And their coordinate system is the same as QT, excluding parent-child items relationship.

    Seems that what was causing the problem was not related to this positioning code but to another "feature" I had in the program. Thanks for the time, I'm very grateful.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView coordinate system

    Quote Originally Posted by been_1990 View Post
    Adding "ball->setPos(x,x) " does not change anything.
    This is not the only difference between my code and yours.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Printing with a coordinate system given in millimeters
    By doberkofler in forum Qt Programming
    Replies: 7
    Last Post: 5th May 2010, 07:09
  2. coordinate system
    By Wojtek.wk in forum Newbie
    Replies: 7
    Last Post: 12th April 2010, 13:47
  3. 2D Graphics on coordinate system
    By soumya in forum Qt Programming
    Replies: 0
    Last Post: 4th November 2009, 06:27
  4. Replies: 1
    Last Post: 9th April 2009, 14:54
  5. The coordinate system
    By avis_phoenix in forum Qt Programming
    Replies: 1
    Last Post: 28th July 2008, 12:16

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.