gpt4 book ai didi

c++ - 2 QGraphicsPixmapItems 之间的 QT 碰撞检测

转载 作者:行者123 更新时间:2023-11-30 05:01:52 25 4
gpt4 key购买 nike

Link to project有趣的部分应该在gameengine.cpp的“launchSplash”-function和splashanimation.cpp中

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

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

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

游戏板看起来像这样game board ,水滴从屏幕中下部向光标方向射出。

最终我会让水滴从墙上弹射出来,但目前我不屑于弄清楚如何首先检测碰撞。

游戏板是 500x600 单位(宽 x 高),所以水滴的射点是 (250, 600)。

射水滴的时候,我用

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);
}

哪里xDestinationyDestionation是水滴在不受阻碍的情况下最终会到达的地方。水滴将在 x=0/x=500 和/或 y=0/y=600 结束,但我认为这无关紧要。

气泡被添加到游戏板上

board_.clear();

for(int y = 0; y < HEIGHT; ++y)
{
std::vector< std::shared_ptr<Bubble> > row;
for(int x = 0; x < WIDTH; ++x)
{
std::shared_ptr<Bubble> newBubble = nullptr;

// There will be bubbles only in the top 3/4 of the board only in the middle
// (i.e. not in the first two and last two columns).
if (y < HEIGHT*3/4 && x > 1 && x < WIDTH-2)
{
// Generate random numbers using the enumearation type defined in
// file bubble.hh. The value COLOR_COUNT is used to represent no bubble.
std::uniform_int_distribution<int> distribution(RED,COLOR_COUNT);

// If you want no empty squares, change the initialization to:
// std::uniform_int_distribution<int> distribution(RED,BLUE);

Color color = static_cast<Color>(distribution(randomEngine_));
if(color != COLOR_COUNT) {
newBubble = std::make_shared<Bubble>(x, y, color);
}
}
row.push_back(newBubble);
}
board_.push_back(row);
}

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));
}

我想有两种方法可以做到这一点:内置的 QT 碰撞检测或进行单独的计算。我无法使用 QT Collision,而且我尝试手动检测碰撞的效果并不理想。

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

std::shared_ptr<Bubble> GameEngine::bubbleAt(int x, int y) const
{
if (0 <= x and x < WIDTH and 0 <= y and y < HEIGHT){
return board_.at(y).at(x);
}
else{
return nullptr;
}
}

编辑:目前我正在尝试做这样的事情,但我担心它对游戏来说会有点沉重,因为它迭代了太多(或没有?):

for (int i = 0; i<600;++i)
{
xfract = (xDestination+250.0)/600.0;
yfract = (600.0-yDestination)/600.0;
xStep = xfract*i;
yStep = yfract*i;
if (xStep >= 50){
thisX = xStep/50-5;
}else{
thisX=5;
}
if (yStep >= 50){
thisY = 11-yStep/50 + 1;
}else{
thisY = 11;
}
thisX = abs(thisX);
if (bubbleAt(thisX, thisY)!=nullptr){
endX = xfract*i;
endY = yfract*i;
i = 600;
std::cout << "collision at x: "<<thisX<< " y: "<<thisY<<std::endl;
std::cout << "collision at x: "<<xStep<< " y: "<<yStep<<std::endl;
std::cout << graphicalGameBoard_.width() << " " << graphicalGameBoard_.height()<<std::endl;
removeBubble(thisX, thisY);
graphicalGameBoard_.removeBubble(thisX, thisY);
endY = 600-endY;
}
}


graphicalGameBoard_.animateSplash(graphicalGameBoard_.width()/2, graphicalGameBoard_.height(), endX, endY);

我试图将步骤分成小部分,并检查当前步骤中是否有气泡,直到水滴到达终点或发现气泡。

这在计算方面仅在我的一侧有效,但动画不合时宜,右侧 (x>250) 碰撞检测由于某种原因根本不起作用(它在右侧不可能的位置击中看似随机的气泡)。

编辑^2:以下是我为使用 QT 的实际碰撞检测而尝试过的东西:

在 splashanimation.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()
{
scene_->removeItem(&item_);
}

void SplashAnimation::updateCurrentValue(QVariant const& value)
{
item_.setPos(value.toPointF());
}

其中 scene 是 QGraphicsScene,并且是 this 的父级包含气泡。

我在 gameboard.cpp(它是气泡和动画的父级)和 splash.cpp(它为水滴设置动画)上都试过了,但它们都给我同样的编译错误。

    QGraphicsItem::QGraphicsItem();

给予

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

QList<QGraphicsItem *> list = collidingItems() ;

给出 error: ?collidingItems? was not declared in this scope
QList<QGraphicsItem *> list = collidingItems() ;
^

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

给出 error: cannot call member function ?QList<QGraphicsItem*> QGraphicsItem::collidingItems(Qt::ItemSelectionMode) const? without object
QList<QGraphicsItem *> list = QGraphicsItem::collidingItems() ;
^

我也尝试添加参数,但我想尝试的任何方法都效果不佳。

最佳答案

在这个回答中,我将为您提供一些用于实现解决方案的建议:

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

    while (animations_.state() == QAbstractAnimation::Running)
    {
    QCoreApplication::processEvents(QEventLoop::AllEvents);
    }
  • QGraphicsScene 使用支持浮点的坐标,因此场景的坐标由 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()))

该实现的优点是它更具可读性

我不明白为什么你不能实现collidingItems,可能是你.pro的配置问题,

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

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

还可以使用我之前的 answer 实现动画

完整的功能代码可以在以下link找到.

关于c++ - 2 QGraphicsPixmapItems 之间的 QT 碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50138172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com