How do I do to trigger a collision among two rectangles?
Locked
General and Desktop
-
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]