Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Box2D碰撞检测失败_C++_Box2d_Collision - Fatal编程技术网

C++ Box2D碰撞检测失败

C++ Box2D碰撞检测失败,c++,box2d,collision,C++,Box2d,Collision,我最近开始将Box2D 2.1版与Allegro5结合使用。目前,我建立了一个地面和4个盒子的测试。3个箱子被堆放在一起,另一个被砸碎,导致它们翻转。在演示过程中,我注意到出现了两个小故障 一种是,在Box2D中创建一个框SetAsBox width,height,只提供使用allegro绘制到屏幕上的普通框的一半大小。示例:在Box2D中,我创建了一个大小为15、15的框。当我使用allegro绘制形状时,我必须在y轴上偏移-15,并将形状缩放两倍大小 另一个问题是在碰撞检测期间,我的箱子由于

我最近开始将Box2D 2.1版与Allegro5结合使用。目前,我建立了一个地面和4个盒子的测试。3个箱子被堆放在一起,另一个被砸碎,导致它们翻转。在演示过程中,我注意到出现了两个小故障

一种是,在Box2D中创建一个框SetAsBox width,height,只提供使用allegro绘制到屏幕上的普通框的一半大小。示例:在Box2D中,我创建了一个大小为15、15的框。当我使用allegro绘制形状时,我必须在y轴上偏移-15,并将形状缩放两倍大小

另一个问题是在碰撞检测期间,我的箱子由于碰撞而旋转。大多数方块落地,但其中一些方块与地面的高度有偏移,使其浮动

以下是制作我的盒子的代码:

cBox2D::cBox2D( int width, int height ) {

  // Note: In Box2D, 30 pixels = 1 meter
  velocityIterations  = 10;
  positionIterations  = 10;
  worldGravity  = 9.81f;
  timeStep    = ( 1.0f / 60.0f );
  isBodySleep   = false;

  gravity.Set( 0.0f, worldGravity );
  world = new b2World( gravity, isBodySleep );

  groundBodyDef.position.Set( 0.0f, height ); // ground location
  groundBody = world->CreateBody( &groundBodyDef );

  groundBox.SetAsBox( width, 0.0f ); // Ground size
  groundBody->CreateFixture( &groundBox, 0.0f );

 }

 cBox2D::~cBox2D( void ) {}

 void cBox2D::makeSquare( int width, int height, int locX, int locY, float xVelocity, float yVelocity, float angle, float angleVelocity ) {

  sSquare square;

  square.bodyDef.type = b2_dynamicBody;
  square.bodyDef.position.Set( locX, locY ); // Box location
  square.bodyDef.angle = angle; // Box angle
  square.bodyDef.angularVelocity = angleVelocity;
  square.bodyDef.linearVelocity.Set( xVelocity, yVelocity ); // Box Velocity
  square.body = world->CreateBody( &square.bodyDef );

  square.dynamicBox.SetAsBox( width, height ); // Box size
  square.fixtureDef.shape = &square.dynamicBox;
  square.fixtureDef.density = 1.0f;
  square.fixtureDef.friction = 0.3f;
  square.fixtureDef.restitution = 0.0f; // Bouncyness
  square.body->CreateFixture( &square.fixtureDef );

  squareVec.push_back( square );

 }

 int cBox2D::getVecSize( void ) {
  return squareVec.size();
 }

 b2Body* cBox2D::getSquareAt( int loc ) {
  return squareVec.at( loc ).body;
 }

 void cBox2D::update( void ) {

  world->Step(timeStep, velocityIterations, positionIterations);
  world->ClearForces();

 }
编辑: 感谢Chris Burt Brown向我解释第一个问题,至于第二个问题,这是个好主意,但没有解决它。我试过你教给我的两种取整方法

编辑: 我想我找到了第二个问题的答案。事实证明,Allegro的坐标系与OpenGL不同。结果,我没有做-重力,而是做+重力,这导致Box2D变得不稳定,行为怪异

编辑: 糟糕的是,我认为这是问题所在,但事实证明这并没有改变任何事情。

它实际上是设置为BoxHalfWidth,halfheight。我知道这听起来很奇怪,但是看看SetAsBox里面。输入参数15和15将得到一个角为-15、-15和15,15的盒子,即尺寸为30x30的盒子

我认为这是一种优化,但这是一种相当愚蠢的优化

我不知道是什么导致了你的另一个问题,但是当你用Allegro画盒子的时候,试着看看当你绕过坐标时它是否被修正了。如果不起作用,请尝试ceil。

它实际上设置为BoxHalfWidth,halfheight。我知道这听起来很奇怪,但是看看SetAsBox里面。输入参数15和15将得到一个角为-15、-15和15,15的盒子,即尺寸为30x30的盒子

我认为这是一种优化,但这是一种相当愚蠢的优化

我不知道是什么导致了你的另一个问题,但是当你用Allegro画盒子的时候,试着看看当你绕过坐标时它是否被修正了。如果不起作用,试试ceil