【问题标题】:Bounding box for C++ particle system not workingC ++粒子系统的边界框不起作用
【发布时间】:2023-03-26 19:25:01
【问题描述】:

我使用 Qt 在 C++ 中创建了一个粒子系统,其中包括重力。我想包括一个边界框,以便系统也可以包括碰撞,但我似乎无法让它工作。这是我的 .h 代码:

typedef struct {
    int top;
    int bottom;
    int left;
    int right;

}BoundingBox;

BoundingBox makeBoundingBox(int top, int bottom, int left, int right);

.cpp:

BoundingBox makeBoundingBox(int top, int bottom, int left, int right)
{
    BoundingBox boundingBox;
    boundingBox.top = top;
    boundingBox.bottom = bottom;
    boundingBox.left = left;
    boundingBox.right = right;

    return boundingBox;
}

然后我使用这个循环更新了我的发射器类中的边界框:

for(int i=0; i<m_numParticles; ++i)
    {

        if (m_pos.m_y >= _boundingBox.top)
        {
            m_dir.m_y = (-1)*(m_dir.m_y);
        }

        if (m_pos.m_y <= _boundingBox.bottom)
        {
            m_dir.m_y = (-1)*(m_dir.m_y);
        }

        if (m_pos.m_x <= _boundingBox.left)
        {
            m_dir.m_x = (-1)*(m_dir.m_x);
        }

        if (m_pos.m_x >= _boundingBox.right)
        {
            m_dir.m_x = (-1)*(m_dir.m_x);
        }

        m_particles[i].update(m_gravity, _boundingBox);

并在我的窗口中设置了边界框:

m_emitter->setBoundingBox(makeBoundingBox(m_height, 0, 0, m_width));

我没有收到任何错误,但它似乎不起作用, 任何意见,将不胜感激 谢谢

【问题讨论】:

  • 在这种情况下“它不起作用”是什么意思?小心物理中的简单箱盒相交测试。如果速度大于盒子大小,它们有可能直接穿过对方。
  • 我会尝试在你的函数中加入一些 cout 语句来检查冲突,看看你的函数得到了什么样的数字,看看你的 if 语句是否正确。您是否正在对粒子本身进行转换?如果是这样,请检查您的转换是否也应用于边界框

标签: c++ physics bounding-box particles particle-system


【解决方案1】:

假设减小 X 意味着向左移动,减小 Y 意味着向顶部移动,那么您的条件似乎不正确。我还会更新速度旁边的位置,以确保它不会连续多次触发。

Y坐标示例:

// Are we going up too much?
if (m_pos.m_y < _boundingBox.top)
{
    m_dir.m_y = (-1)*(m_dir.m_y);
    m_pos.m_y = _boundingBox.top + 1;
}
// Or are we going down too much?
else if (m_pos.m_y > _boundingBox.bottom)
{
    m_dir.m_y = (-1)*(m_dir.m_y);
    m_pos.m_y = _boundingBox.bottom - 1;
}

【讨论】:

    【解决方案2】:

    不应该:

    m_emitter->setBoundingBox(makeBoundingBox(m_height, 0, 0, m_width));
    

    是:

    m_emitter->setBoundingBox(makeBoundingBox(0, m_height, 0, m_width));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多