coordinate mapping



  • Hello Guys
    I've been working on a physics simulation, and I've been having some trouble with cordinates.

    I haven't been able to find how the scenes are mapped, and to what objects are mapped ot when I don't use mapToScene();

    My Code is as follows:

    Ball.cpp

    Ball::Ball()
    {
        //random start angle
    
        angle = (qrand() % 360);
        setRotation(angle);
    
        //set the speed
        speed = 5;
    
    
        //random start position
        int StartX = 0;
        int StartY = 0;
    
    
        if((qrand() % 1))
        {
            StartX = (qrand() % 200);
            StartY = (qrand() % 200);
    
        }
        else
        {
            StartX = (qrand() % 100);
            StartY = (qrand() % 100);
        }
    
    
        setPos(mapToScene(StartX,StartY));
        qDebug() << "xposition:" << pos().x();
        qDebug() << "yposition:" << pos().y();
        qDebug() << "xposition mapped:"  << mapToScene(pos()).x();
        qDebug() << "yposition mapped:" << mapToScene(pos()).y();
    
    }
    
    

    mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        scene = new QGraphicsScene (this);
        ui->graphicsView->setScene (scene);
    
        ui->graphicsView->setRenderHint(QPainter::Antialiasing);
        double sceneULx = -400;
        double sceneULy = 200;
        double scenewidth = 800;
        double sceneheight = 400;
    
        scene->setSceneRect(sceneULx, sceneULy,scenewidth,sceneheight);
    
        QPen mypen = QPen(Qt::red);
    
        QLineF TopLine(scene->sceneRect().topLeft(),scene->sceneRect().topRight());
        QLineF LeftLine(scene->sceneRect().topLeft(),scene->sceneRect().bottomLeft());
        QLineF RightLine(scene->sceneRect().bottomRight(),scene->sceneRect().topRight());
        QLineF BottomLine(scene->sceneRect().bottomLeft(),scene->sceneRect().bottomRight());
    
        scene->addLine(TopLine,mypen);
        scene->addLine(LeftLine,mypen);
        scene->addLine(RightLine,mypen);
        scene->addLine(BottomLine,mypen);
    
        int ItemCount = 5;
        for(int i = 0; i <ItemCount; i++)
        {
           Ball *item = new Ball();
           scene->addItem(item);
        }
    
        timer = new QTimer(this);
        connect(timer,SIGNAL(timeout()), scene,SLOT(advance()));
        timer->start(10);
    }
    
    

    currently all the objects I generate are not in the area of the scene, which doesn't make any sense to me since I thought that the upper left corner of the scene was 0/0 and it went into the positive when you went down or to the right.

    also the qDebugs yield following results:

    xposition: 25.6601
    yposition: 22.306
    xposition mapped: 30.392
    yposition mapped: 55.9751
    xposition: -56.6299
    yposition: 79.0004
    xposition mapped: -136.607
    yposition mapped: 23.7579
    xposition: 31.6448
    yposition: 32.3822
    xposition mapped: 76.5177
    yposition mapped: 38.4172
    xposition: 50.017
    yposition: -97.4695
    xposition mapped: -59.4804
    yposition mapped: -93.9612
    xposition: -44.0378
    yposition: 9.25605
    xposition mapped: -33.8154
    yposition mapped: -34.5675

    I can't really find a correlation between the mapped and the unmapped numbers and it is also very strange the the mapped numbers have decimals altough I define them with an int.

    I hope somebody can help me clear my confusion with the coordinate system of this programm.



  • @Tomathor Looks like more of your algorithm problem than Qt related problem..


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.