【问题标题】:QML components collision test methodQML组件碰撞测试方法
【发布时间】:2018-06-05 01:09:23
【问题描述】:

嗯,可能有很多 QML 程序,如果两个组件相互碰撞,程序员会根据它定义一个动作。在下面这样的程序中,我们希望专注于碰撞,并尝试知道如何定义一个函数来告诉我们是否发生了这种碰撞。

下面的代码是一个更大程序的一部分。为此,我尝试在代码中使用一个名为 collision 的函数。到目前为止的问题是它运行得不好。我想知道 QML 中是否有内置函数“或”是否有用于此目的的好代码。

球拍有三个可能与球发生碰撞的面:前面的一个长面,一个上部和一个下部。具体的问题是,当球从上表面或下表面与球拍碰撞时,球会进入球拍内部!
我要解决的问题是。我希望当球从任何面击中球拍时,它会反射。

main.qml:

import QtQuick 2.9
import QtQuick.Window 2.2

Window {
    visible: true
    width: 720
    height: 620
    title: qsTr("Collision Test")

    Rectangle {
        id: table
        anchors.fill: parent
        color: "gray"

        Rectangle {
            id: ball
            property double xincrement: Math.random() + 0.5
            property double yincrement: Math.random() + 0.5
            width: 15
            height: width
            radius: width / 2
            color: "white"
            x: 300; y: 300
        }

        Racket {
            id: myRacket
            x: table.width - 50
            y: table.height/3
            color: "blue"
        }

        Timer {
            interval: 5; repeat: true; running: true

            function collision() {
                if((ball.x + ball.width >= myRacket.x  &&
                    ball.x < myRacket.x + myRacket.width) &&
                   (ball.y + ball.height >= myRacket.y &&
                    ball.y <= myRacket.y + myRacket.height))
                    return true
                return false
            }

            onTriggered: {
                if(ball.x + ball.width >= table.width)
                    running = false

                else if(ball.x <= 0)
                    ball.xincrement *= -1

                else if (collision())
                    ball.xincrement *= -1

                ball.x = ball.x + (ball.xincrement * 1.5);
                ball.y = ball.y + (ball.yincrement * 1.5);

                if(ball.y <= 0 || ball.y + ball.height >= table.height)
                    ball.yincrement *= -1
            }
        }
    }
}

Racket.qml

import QtQuick 2.9

Rectangle {
    id: root
    width: 15; height: 50

    MouseArea {
        anchors.fill: parent
        drag.target: root
        drag.axis: Drag.YAxis
        drag.minimumY: table.y
        drag.maximumY: table.y + table.height - 50
    }
}

【问题讨论】:

  • 不幸的是,在 Qt Quick 中没有碰撞(与 QGraphicsScene 不同)。我推荐 QML Box2D 插件:github.com/qml-box2d/qml-box2d
  • 感谢您的推荐,但它包含很多文件,不仅很多很多行代码!我需要的只是比collision 中的4 行代码更好的代码。 :)
  • 你不能在两行代码中完成collision。那是物理学和大量的计算。因此,对于您应该重新发明现有事物的内容。正如@Mitch 所说,使用qml-box2d。 QML Box2D 是一个插件,您只需将 *.pro 包含到您的项目中,您就会立即拥有所有 2D 功能 - 碰撞、反弹、力等。
  • QtQuick2 本身不是游戏引擎,因此可能缺少许多通用的功能。 Mitch 推荐的是一个插件,可以让你(在某种程度上?)QML 中的物理。当然,库是包含许多行代码的许多文件。但至少你不需要自己写。
  • 您需要复制整个内容,而不仅仅是个人资料。

标签: qt qml collision-detection collision qqmlapplicationengine


【解决方案1】:

对于简单的检查,一个点是否在Item 内,您可以使用contains(point p)

这个函数需要一个局部坐标点,(相对于Items原点),所以你应该使用mapFromItem(...)mapToItem(...)来获得正确的位置。

【讨论】:

  • 谢谢。我打算在该程序的代码中使用它们。 +1
  • 我试图在代码中制作一个版本,但无论我做什么,我都得到了this message。我厌倦了编辑代码this way,但我遇到了错误! :(
  • 你在使用 PyQt 吗?
  • 那些行var ... 在函数之外,所以你不能在那里使用JS。将它们移到函数中。
  • 不,由 Qt 5.10 我正在使用 Qt Quick Application - Empty 项目。我在函数中移动了这些行,it was 结果!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多