【问题标题】:SFML 1.6 Collision IssuesSFML 1.6 碰撞问题
【发布时间】:2012-09-23 23:45:14
【问题描述】:

我目前正在使用 C++ 和 SFML 1.6 在业余时间编写游戏代码。当是时候进行碰撞检测时,我遇到了一个烦人的错误,程序检测到碰撞,就好像对象被移动到左上角的量根据我使用的静止精灵的大小而不同测试这个,通常是它的一半大小。我似乎无法找到我的代码中的错误,但也许你可以。我的代码在下面列出,在此先感谢。

#include <SFML/Graphics.hpp>
#include <math.h>

bool collide_rec(sf::Sprite object_1, sf::Sprite object_2);
bool collide_point(sf::Vector2f point, sf::Sprite object);
void collide(sf::Sprite& movable, object& movable_data, sf::Sprite& sationary);

void collide(sf::Sprite& movable, object& movable_data, sf::Sprite& stationary)
{
//Handle Collision NOTE: Results in infinate loop if started on colliding object
for(;collide_rec(movable, stationary);)
{
if((movable_data.move.spd.x<0)||(movable_data.move.left==true||movable_data.direction==LEFT )) movable.Move( 1, 0);
if((movable_data.move.spd.x>0)||(movable_data.move.right==true||movable_data.direction==RIGHT)) movable.Move(-1, 0);
if((movable_data.move.spd.y<0)||(movable_data.move.up   ==true||movable_data.direction==UP   )) movable.Move(0,  1);
if((movable_data.move.spd.y>0)||(movable_data.move.down ==true||movable_data.direction==DOWN )) movable.Move(0, -1);
  }
}

bool collide_point(sf::Vector2f point, sf::Sprite object)
{

}

bool collide_rec(sf::Sprite object_1, sf::Sprite object_2)
{
  bool hit=true;
  sf::Vector2f tl_1, br_1, tl_2, br_2;//Top-Left Coner, Botom Right Corner

  //Assign the corners proporly
  tl_1= object_1.GetPosition();
  tl_2= object_2.GetPosition();

   br_1= (object_1.GetPosition()+object_1.GetSize());
   br_2= (object_2.GetPosition()+object_2.GetSize());

  if((tl_1.x<tl_2.x)&&(br_1.x<tl_2.x) || (tl_1.x<br_2.x)&&(br_1.x<br_2.x)) //if both points are to the left or right on the x demtion
  hit=false;

  if((tl_1.y<tl_2.y)&&(br_1.y<tl_2.y) || (tl_1.y<br_2.y)&&(br_1.y<br_2.y)) //if both points are to the left or right on the y demtion
  hit=false;

  return hit;
}

【问题讨论】:

    标签: debugging collision-detection sfml


    【解决方案1】:

    不确定您是否还需要这个问题的答案,但我不妨尝试并提供帮助。

    您可能需要检查以下几点: - object_1.getPosition() 和 object_2 的位置是否是对象的左上角。 - 如果对象不是正方形,则右下角将需要稍微不同的方程来计算。 (x, y) 坐标为 (Pos.x + Width, Pos.y + Height)。

    一旦你确定了这一点,我在你的命中检测算法中注意到了几件事:

    1)

    if((tl_1.x&lt;tl_2.x)&amp;&amp;(br_1.x&lt;tl_2.x) || (tl_1.x&lt;br_2.x)&amp;&amp;(br_1.x&lt;br_2.x))

    这个条件的第二部分:

    (tl_1.x < br_2.x)&&(br_1.x < br_2.x))
    

    应该是这样的

    (tl_1.x > br_2.x)&&(br_1.x > br_2.x))
    

    2)

    同样:

    (tl_1.y < tl_2.y)&&(br_1.y < tl_2.y) 
    

    应该是这样的

    (tl_1.y > tl_2.y)&&(br_1.y > tl_2.y)
    

    3)

    最后,您的条件对它们有一定的冗余度,这是不必要的。

    一旦有了 topLeft1、BottomRight1 和 topLeft2、BottomRight2,您就可以像这样简单地检查碰撞:

    假设笛卡尔坐标系,原点 (0, 0) 在左下角。

    • X 舞台碰撞:

      如果 (topLeft1.x > BottomRight2.x || BottomRight1.x

    // 如果box1的左边>box2的右边 OR // 如果box1的右边不要在 X 轴上碰撞。

    • Y 舞台碰撞:

      如果 (topLeft1.y TopLeft2.y)

    // 如果 box1 的顶边 box2 的顶边 然后 - 框不要在 Y 轴上碰撞。

    这将其从 8 个条件减少到 4 个。希望有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-10-22
      • 2013-08-22
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-18
      • 2017-07-25
      • 1970-01-01
      相关资源
      最近更新 更多