SFML边界框碰撞检测 [英] SFML Bounding Box Collision Detection

查看:165
本文介绍了SFML边界框碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的第一个SFML项目编写一个非常基本的游戏。它是一个带有关节臂的机器人,只能用一只手弹出气球。我遇到的问题是,当检查钳子精灵是否与气球精灵相交时,无论气球或机器人钳的位置如何,它始终返回true。我正在使用transform放置机器人手臂的各个部分,这是导致我认为的问题的原因,但我不知道为什么。我尝试在单独的程序中使用包围盒碰撞,该程序不使用转换并且效果很好。转换和检测代码如下。如果有人可以向我解释,我将不胜感激。我是SFML的新手,所以为我的无知深表歉意!

I am trying to write a very basic game for my first SFML project. Its a robot with a jointed arm that will be able to pop balloons with his hand only. The problem I am having is that when checking to see if the pincer sprite intersects with the balloon sprite , it returns true all the time, regardless of the placement of the balloon or the robot pincer. I am using transform to place the various parts of the robot arm, and this is whats causing the problem I think, but I don't know why. I have tried using bounding box collision in a separate program where transform is not used and it worked perfectly. The transformation and detection code is below. I would be grateful if anyone could explain this to me.I am brand new to SFML so apologies for my ignorance here!!

    sf::Transform trBody;
    trBody.translate(sBodyPos);
    /////////////////////////////////////////////////

    sf::Transform trArm1;
    trArm1.translate(sArm1Pos);

    sf::Transform rotArm1;
    rotArm1.rotate(sArm1Rot);

    sf::Transform TR1 = trBody*trArm1*rotArm1;
    /////////////////////////////////////////////////

    sf::Transform trArm2;
    trArm2.translate(sArm2Pos);

    sf::Transform rotArm2;
    rotArm2.rotate(sArm2Rot);

    sf::Transform TR2 = TR1*trArm2*rotArm2;
    /////////////////////////////////////////////////

    sf::Transform trPincer1;
    trPincer1.translate(sPincer1Pos);

    sf::Transform TR3 = TR2*trPincer1;
    /////////////////////////////////////////////////

    sf::Transform trPincer2;
    trPincer2.translate(sPincer2Pos);

    sf::Transform TR4 = TR2*trPincer2;
    /////////////////////////////////////////////////
    sf::Transform trBalloon1;
    trBalloon1.translate(sBalloon1Pos);

    if (sPincer1.getGlobalBounds().intersects(sBalloon1.getGlobalBounds())){

        cout << "Bang" << endl;
        ballOneHit = true;

    }

    // Clear screen
    app.clear();
    app.draw(sArm2, TR2);
    app.draw(sPincer1, TR3);
    app.draw(sPincer2, TR4);
    app.draw(sArm1, TR1);
    app.draw(sBody, trBody);

    if (ballOneHit == false){

        app.draw(sBalloon1, trBalloon1);

    }

    // Update the window
    app.display();


推荐答案

危险的猜测,我想说物体是罪魁祸首,因为旋转不幸地扩大了物体的边界。旋转后的边界矩形仍在屏幕确定的x-y平面中,但包含对象的旋转后。作为说明,请看这张图片中文字的全局边界框:

Hazarding a guess, I'd say that rotation of the object is the culprit, as rotation has the unfortunate effect of expanding the bounds of an object. The bounding rectangle after rotation is still in the x-y plane determined by the screen, but encompasses the object post-rotation. As illustration, look at the global bounding box on text in this picture:

请注意,该框包围了对象,但并非您可能希望获得的原始边界框的旋转版本。用有关图形变换的教程

Note that the box surrounds the object, but isn't the rotated version of the original bounding box that you might expect to get. In the words of the tutorial on graphics transforms:


SFML实体可以给您边界框。边界框是包含实体的最小矩形,边在X和Y轴上对齐。

SFML entities can give you their bounding box. The bounding box is the minimal rectangle that contains the entity, with sides aligned on the X and Y axes.

要解决此问题,您可以尝试创建一个矩形,该矩形将始终完全包含在对象的可见部分内,然后将其用于命中检测,而不是对象的边界框。

To solve this issue, you might try creating a rectangle that will always be wholly contained inside the visible parts of the object, and then use that for hit detection instead of the object's bounding box.

这篇关于SFML边界框碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆