C++ 两个QGraphicsPixMapItem之间的QT碰撞检测

C++ 两个QGraphicsPixMapItem之间的QT碰撞检测,c++,qt,qt5,qgraphicspixmapitem,C++,Qt,Qt5,Qgraphicspixmapitem,有趣的部分应该在gameengine.cpp的“launchSplash”函数和splashanimation.cpp中 游戏在可接受的区域随机创建泡泡。玩家的工作是用水滴射出泡泡。水滴从游戏屏幕的中下部发射。网格仅用于调试,稍后将消失,但它使可视化区域更容易 将水滴射向泡泡会破坏泡泡,但当水滴击中泡泡或游戏的上边界时,泡泡就会消失。水滴射向鼠标点击的方向 我正在尝试为一个基本的泡泡射击游戏创建一个碰撞检测,但我不确定如何以一种简洁的方式检测碰撞 游戏板看起来像这样,水滴从屏幕的中间底部向光标的

有趣的部分应该在gameengine.cpp的“launchSplash”函数和splashanimation.cpp中

游戏在可接受的区域随机创建泡泡。玩家的工作是用水滴射出泡泡。水滴从游戏屏幕的中下部发射。网格仅用于调试,稍后将消失,但它使可视化区域更容易

将水滴射向泡泡会破坏泡泡,但当水滴击中泡泡或游戏的上边界时,泡泡就会消失。水滴射向鼠标点击的方向

我正在尝试为一个基本的泡泡射击游戏创建一个碰撞检测,但我不确定如何以一种简洁的方式检测碰撞

游戏板看起来像这样,水滴从屏幕的中间底部向光标的方向射出

最终我会让水滴从墙上弹出来,但目前我对如何首先检测碰撞感到不屑

游戏板是500x600个单位(宽x高),所以水滴射入的点是(250600)

当水滴被射出时,我使用

void GameEngine::launchSplash(int clickX, int clickY){
// <my long method of calculating the coordinates for the water drop's path>

    graphicalGameBoard_.animateSplash(graphicalGameBoard_.width()/2, graphicalGameBoard_.height(), xDestination, yDestination);
}
gameengine.cpp
中,它绘制了棋盘和向气泡喷射的水滴

水滴是用水笔画出来的

SplashAnimation::SplashAnimation(GameBoard* scene, QPointF startPoint, QPointF endPoint):
    QVariantAnimation(0),
    scene_(scene),
    item_()
{
    scene_->addItem(&item_);

    // The animation runs for the given duration and moves the splash
    // smoothly from startpoint to endpoint.
    setDuration(2000);
    setKeyValueAt(0, QPointF(startPoint));
    setKeyValueAt(1, QPointF(endPoint));
}
SplashAnimation::SplashAnimation(GameBoard* scene, QPointF startPoint, QPointF endPoint):
    QVariantAnimation(0),
    scene_(scene),
    item_()
{
    scene_->addItem(&item_);


    // The animation runs for the given duration and moves the splash
    // smoothly from startpoint to endpoint.
    setDuration(2000);
    setKeyValueAt(0, QPointF(startPoint));
    setKeyValueAt(1, QPointF(endPoint));   
}

SplashAnimation::~SplashAnimation()
{
    scene_->removeItem(&item_);
}

void SplashAnimation::updateCurrentValue(QVariant const& value)
{
    item_.setPos(value.toPointF());
}
我认为有两种方法可以做到这一点:要么内置QT碰撞检测,要么进行单独的计算。我无法使用QT碰撞,我手动检测碰撞的尝试也没有很好的效果

我已经有了一个在某些单元格中检测气泡对象的函数,但它位于列/行中,而不是原始坐标(500x600)

其中,场景为QGraphicscene,且该的父级包含气泡

我在gameboard.cpp(泡泡和动画的父对象)和splash.cpp(水滴的动画)上都尝试过这个方法,但它们都给了我相同的编译错误

    QGraphicsItem::QGraphicsItem();
给予

错误:无法调用构造函数?QGraphicsItem::QGraphicsItem?直接[-F允许] QGraphicsItem::QGraphicsItem(); ^

QList<QGraphicsItem *> list = collidingItems() ;
QList list=collingitems();
给出
错误:?冲突项?未在此范围中声明
QList list=冲突项();
^

    QList<QGraphicsItem *> list = QGraphicsItem::collidingItems() ;
QList=QGraphicsItem::collingitems();
给出
错误:无法调用成员函数?QList QGraphicsItem::collingitems(Qt::ItemSelectionMode)const?毫无目的
QList=QGraphicsItem::collidingItems();
^

    QList<QGraphicsItem *> list = QGraphicsItem::collidingItems() ;

我也试着添加一些论据,但没有什么比这更好的了

在这个回答中,我将给您一些建议,供您使用以实施解决方案:

  • 避免使用以下指令,使用Qt最强大的元素之一的信号以及事件循环完成工作的信号

    while (animations_.state() == QAbstractAnimation::Running)
    {
        QCoreApplication::processEvents(QEventLoop::AllEvents);
    }
    
  • qgraphicscene
    使用支持浮点的坐标,因此使用
    QPointF
    处理场景的坐标

  • 使用上述方法,如果要发送点信息,请在信号中使用
    QPointF
    而不是
    int,int

  • 使用Qt提供的方法,例如:

    if (0 <= clickPosition.x() and clickPosition.x() <= GRID_SIDE*WIDTH and
        0 <= clickPosition.y() and clickPosition.y() <= GRID_SIDE*HEIGHT)
    
    if(sceneRect().contains(event->scenePos()))
    
    该实现的优点是可读性更强

    我不明白你为什么不能实现冲突项,可能是你的.pro的配置

    使用以下命令添加gui模块、核心和小部件

    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    同样,要实现动画,请使用我的上一个


    完整的功能代码可以在以下位置找到。

    为什么不使用collidingItems来检测冲突?我试图寻找正确使用它的方法,但它无法以我尝试过的任何方法为我编译。我不知道这是因为QT更新和改变了它的使用方式还是什么,但一开始我放弃了<代码>QList list=冲突项()未编译,因为未定义“collidingItems()”,并且我无法获取
    QList list=QGraphicsItem::collidingItems()的任何工作参数
    在我看来,你没有正确地使用它,你可以提供你项目的一部分,如果你提供无法复制的代码片段,很难找到错误。我缺乏从头开始复制的技能,但是,我可以更详细地描述我正在尝试的内容以及我遇到的编译错误如果您共享您的项目,那么会更容易提供帮助,您是否了解创建一个项目会耗费太多时间,并且会让我们感到沮丧?
    if(sceneRect().contains(event->scenePos()))
    
    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets