1

我在 Box2D 中有以下代码,我想将我的动态主体移动到鼠标单击的位置。同样在将来我希望能够删除动态主体,我无法找到与 world.DeleteBody(...) 等效的任何东西。请帮忙。

var world;
var   b2Vec2 = Box2D.Common.Math.b2Vec2
        ,   b2BodyDef = Box2D.Dynamics.b2BodyDef
        ,   b2Body = Box2D.Dynamics.b2Body
        ,   b2FixtureDef = Box2D.Dynamics.b2FixtureDef
        ,   b2Fixture = Box2D.Dynamics.b2Fixture
        ,   b2World = Box2D.Dynamics.b2World
        ,   b2MassData = Box2D.Collision.Shapes.b2MassData
        ,   b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
        ,   b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
        ,   b2DebugDraw = Box2D.Dynamics.b2DebugDraw
        ;
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");

var mouseX, mouseY, isMouseDown;
var bBallBody, bBallbodyDef;

function init() {
    world = new b2World(
           new b2Vec2(0, 10)    //gravity
        ,  true                 //allow sleep
    );
    setupWorld() ;
    //setup debug draw
    var debugDraw = new b2DebugDraw();
        debugDraw.SetSprite(document.getElementById("canvas").getContext("2d"));
        debugDraw.SetDrawScale(1.0);
        debugDraw.SetFillAlpha(0.3);
        debugDraw.SetLineThickness(1.0);
        debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);

    world.SetDebugDraw(debugDraw);
    window.setInterval(update, 1000 / 60);
  };

  function setupWorld() {
      setupGround();
      addBouncingBall();
  }

  function setupGround() {
     var fixDef = new b2FixtureDef;
     fixDef.density = 1.0;
     fixDef.friction = 0.5;
     fixDef.restitution = 0.2;
     var bodyDef = new b2BodyDef;
     //create ground
     bodyDef.type = b2Body.b2_staticBody;
     bodyDef.position.x = 300;
     bodyDef.position.y = 400;
     fixDef.shape = new b2PolygonShape;
     fixDef.shape.SetAsBox(290, 10);
     world.CreateBody(bodyDef).CreateFixture(fixDef);
  }

  function addBouncingBall() {
     var fixDef = new b2FixtureDef;
     fixDef.density = 1.0;
     fixDef.friction = 1.0;
     fixDef.restitution = 0.1;
     var bBallbodyDef = new b2BodyDef;
     bBallbodyDef.type = b2Body.b2_dynamicBody;
     fixDef.shape = new b2CircleShape(Math.random() + 30);
     bBallbodyDef.position.x = Math.random() * 300;
     bBallbodyDef.position.y = Math.random() * 300;
     bBallBody = world.CreateBody(bBallbodyDef).CreateFixture(fixDef);  
     console.log(bBallBody.m_body.GetPosition().x);

  };

  document.addEventListener("mousedown", handleMouseMove, true);

  function handleMouseMove(e) {
      isMouseDown = true;
      mouseX = e.clientX;
      mouseY = e.clientY;
  };

  function update() {

     if(isMouseDown)
      {
          for (b = world.GetBodyList() ; b; b = b.GetNext()) {
              if (b.GetType() == b2Body.b2_dynamicBody) {
                   console.log(b.x, b.y);
                   b.x = 100;
                   b.y = 100;
                   }
                   }
      isMouseDown = false; 
      } 
       world.Step(1 / 60, 10, 10);
       world.DrawDebugData();
       world.ClearForces();
  };

更新:

从世界中删除对象的操作如下:

  1. 创建一个计时器来安排对象的删除。

       window.setInterval(removeObjScheduledForRemoval, 1000/90);
    
  2. 将要删除的对象收集到一个 Array 中。

       var bricksScheduledForRemoval = Array();
       var index = -1;
       function removeObjScheduledForRemoval()
       {
         for(var i = 0; i <= index; i++){
        world.DestroyBody(bricksScheduledForRemoval[i]);    
        bricksScheduledForRemoval[i] = null;    
          }
     bricksScheduledForRemoval = Array();
         index = -1;
        }
    

完整的代码在这里, http://box2dinabox.blogspot.in/2012/07/the-completed-bananamonkey-game.html

4

2 回答 2

0

box2dweb 的文档远没有原始 C 版本那么全面,因此请考虑搜索“box2d”而不是“box2dweb”。此外,查看来源并搜索某些方法的拼写可能会有所帮助。或者只是查看 box2d flash 文档。基本上,box2dweb 有方法DestroyJointDestroyBody.

如需更多考虑,这可能会有所帮助:http ://www.iforce2d.net/b2dtut/removing-bodies

于 2012-07-18T15:23:35.193 回答
-2

您可以像此游戏示例一样移除身体或将静态身体更改为动态身体。 http://pixsansar.com/box2dweb-jumping-and-puzzle-ball-level4

Box2dweb 有 removebody 或 changeBodyType 选项,请参阅它的源代码。

于 2012-08-03T16:00:52.977 回答