我已经使用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));我没有得到任何错误,但它似乎不工作,任何建议将不胜感激
发布于 2016-04-29 23:06:04
假设减少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;
}发布于 2016-04-29 23:07:12
不应该:
m_emitter->setBoundingBox(makeBoundingBox(m_height, 0, 0, m_width));Be:
m_emitter->setBoundingBox(makeBoundingBox(0, m_height, 0, m_width));https://stackoverflow.com/questions/36941679
复制相似问题