Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I do to trigger a collision among two rectangles?
Forum Update on Monday, May 27th 2025

How do I do to trigger a collision among two rectangles?

Scheduled Pinned Locked Moved General and Desktop
collisions
3 Posts 3 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cold_Distance
    wrote on 13 Jul 2015, 17:40 last edited by kshegunov 9 Aug 2017, 00:44
    #1

    Hi, I don't know how to do a collision among two rectangles. I was searching for some day but I don't see any clear example that guides me to do it in a simple program.

    One of the rectangle moves automatically, and the other is controlled by the user. I will put the code I wrote:

    CoheteAuto.qml

    import QtQuick 2.3
    
    Item {
        id: root
    
        Rectangle {
    
            id: contcohete
            width: 78
            height: 151
            color: "#00000000"
    
            Image {
                id: imacohete
                anchors.fill: parent
                source: "images/cohete.png"
            }
    
            MouseArea {
                id: clicacohete
                anchors.fill: parent
                onClicked: anim.start()
            }
    
            transitions: [
                Transition {
                    to: anim.start()
                }
            ]
    
    
        }
    
        ParallelAnimation {
            id: anim
    
            NumberAnimation {
                target: contcohete
                properties: "y"
                to: 20
                duration: 5000
    
            }
            NumberAnimation {
                target: contcohete
                properties: "x"
                to: 160
                duration: 5000
            }
    
            onStopped: anim2.start()
        }
    
        SequentialAnimation {
            id: anim2
            NumberAnimation {
                target: contcohete
                properties: "y"
                to: 100
                // 60% of time to travel up
                duration: 10000
            }
            NumberAnimation {
                target: contcohete
                properties: "x"
                to: 400
                // 40% of time to travel sideways
                duration: 10000
            }
        }
    }
    

    CuadroManual.qml

    import QtQuick 2.3
    
    Item{
        id: root
    
    
        Rectangle {
            id:cuadrado
            width: 150
            height: 150
            color: "#53a135"
            border.color: "#61e361"
    
        }
    
        // A partir de aquĆ­ podemos empezar a manipular el 'cuadrado' desde el teclado
        focus: true
        //Keys.onLeftPressed: cuadrado.x -= 8
        //Keys.onRightPressed: cuadrado.x += 8
        //Keys.onUpPressed: cuadrado.y -= 8
        //Keys.onDownPressed: cuadrado.y += 8
        Keys.onPressed: {
            switch(event.key) {
            case Qt.Key_Plus:
                cuadrado.scale += 0.2
                break;
            case Qt.Key_Minus:
                cuadrado.scale -= 0.2
                break;
            case Qt.Key_A:
                cuadrado.x -= 8
                break;
            case Qt.Key_D:
                cuadrado.x += 8
                break;
            case Qt.Key_W:
                cuadrado.y -= 8
                break;
            case Qt.Key_S:
                cuadrado.y += 8
                break;
            }
        }
    }
    

    main.qml

    import QtQuick 2.3
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 800
        height: 600
    
        Image {
            id: fondoventana
            anchors.fill: parent
            source: "images/campo.jpg"
        }
    
        CoheteAuto{
            id: cohete
            x: 65
            y: 215
    
            Behavior on x{
    
            }
            Behavior on y{
    
            }
    
        }
    
        CuadroManual{
            id: cuadro
            x: 500
            y: 300
    
        }
    }
    

    It doesn't mind if the code is not orthodox, the only thing I want is to collide the two rectangles.

    [Locked due to necroposting ~kshegunov]

    1 Reply Last reply
    0
    • G Offline
      G Offline
      GeoWorm
      wrote on 7 Sept 2017, 13:51 last edited by kshegunov 9 Aug 2017, 00:44
      #2

      I think u follow rectangles corner point so u know where is the rectangles. And when locations are collision, u can alert something.

      kshegunovK 1 Reply Last reply 8 Sept 2017, 00:43
      0
      • G GeoWorm
        7 Sept 2017, 13:51

        I think u follow rectangles corner point so u know where is the rectangles. And when locations are collision, u can alert something.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on 8 Sept 2017, 00:43 last edited by
        #3

        Please don't raise threads from the dead without a good reason.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved