box2d垂直边缘或墙壁碰撞检测

时间:2014-11-17 02:31:14

标签: libgdx box2d collision-detection

我正在尝试使用Box2d进行碰撞检测在一个球和一个矩形块的四面墙之间我称之为gameBed。

我使用下面的代码来设置球和LeftEdge(在这种情况下是左墙)身体。为简洁起见,我仅为左墙提供代码。

  b2World = new World(new Vector2(0, 0), false);
  World.setVelocityThreshold(0);
  GameBed gameBed = levelManager.retGameBed();   // rectangular block 
  Vector2 origin = new Vector2();

  BodyDef leftWallDef = new BodyDef();
  leftWallDef.type = BodyType.StaticBody;
  leftWallDef.position.set(gameBed.position); // gameBed.position is the world co-ordinate of the bottom left corner of the rectangular block
  Body body = b2World.createBody(leftWallDef);
    PolygonShape leftWallShape = new PolygonShape();
    origin.x = 0.005f;
    origin.y = (float)(gameBed.bounds.getHeight()) / 2;
    float widthOfLeftWall = BallsMania_Constants.VIEWPORT_WIDTH/(2 * Gdx.graphics.getWidth());
    Gdx.app.log(TAG, "Width of Left Wall is " + widthOfLeftWall);
    leftWallShape.setAsBox(.1f, 
            gameBed.bounds.height/2, origin, 0); // setting the half of width to .1f so that it represents an edge. The height of the edge should be as long as the rectangular edge.
    FixtureDef leftWallFixtureDef = new FixtureDef();
    leftWallFixtureDef.shape = leftWallShape;
    leftWallFixtureDef.restitution = 0.9f;
    body.createFixture(leftWallFixtureDef);

    leftWallShape.dispose();

  // Defining shape and fixture for ball
          Ball ball = new Ball();
     BodyDef ballBodyDef = new BodyDef();
     ballBodyDef.type = BodyType.DynamicBody;
     ballBodyDef.position.set(ball.position);
     ballBodyDef.linearDamping = 0.4f;
     ball.body = b2World.createBody(ballBodyDef);
     CircleShape ballShape = new CircleShape();
     ballShape.setPosition(new Vector2(0, -1 * (float)(BallsMania_Constants.VIEWPORT_HEIGHT/2.0 
           - BallsMania_Constants.bottomBaseDelta 
           + ball.dimension.x/2)));
     ballShape.setRadius(ball.dimension.x/2);
     FixtureDef ballBodyFixture = new FixtureDef();
     ballBodyFixture.shape = ballShape;
     ballBodyFixture.friction = 0.5f;
     ballBodyFixture.restitution = 0.5f;
     ball.body.createFixture(ballBodyFixture);
     ballShape.dispose();

问题: 球不会与leftedge发生碰撞;它只是通过它而不会反弹。

任何指导都将不胜感激。

0 个答案:

没有答案
相关问题