javascript - 正确使用Physics.aabb.union()

标签 javascript aabb physicsjs

我正在学习PhysicsJS ,我尝试使用 union像这样:

// Window bounds
var rect1 = Physics.aabb(0, 100, 300, 200);
var rect2 = Physics.aabb(100, 0, 200, 300);
var viewportBounds = Physics.aabb.union(rect1, rect2);

// Constrain bodies to these bounds
world.add(Physics.behavior('edge-collision-detection', {
  aabb: viewportBounds,
  restitution: 0.99,
  cof: 0.99
}));

但是球刚刚从底部掉下来。

Physics(function(world){

  var viewWidth = 300;
  var viewHeight = 300;

  var renderer = Physics.renderer('canvas', {
    el: 'viewport',
    width: viewWidth,
    height: viewHeight,
    meta: false
  });

  // add the renderer
  world.add(renderer);
  // render on each step
  world.subscribe('step', function(){
    world.render();
  });

  // Window bounds
    var rect1 = Physics.aabb(0, 100, 300, 200);
    var rect2 = Physics.aabb(100, 0, 200, 300);
    var viewportBounds = Physics.aabb.union(rect1, rect2);
  
  // Constrain bodies to these bounds
  world.add(Physics.behavior('edge-collision-detection', {
      aabb: viewportBounds,
      restitution: 0.99,
      cof: 0.99
  }));

  // Add the ball
  world.add(
      Physics.body('circle', {
        x: 0, // x-coordinate
        y: 0, // y-coordinate
        vx: 0.2, // x-velocity
        vy: 0.01, // y-velocity
        radius: 2.0
      })
  );

  // ensure objects bounce when edge collision is detected
  world.add( Physics.behavior('body-impulse-response') );

  // add some gravity
  world.add( Physics.behavior('constant-acceleration') );

  // subscribe to ticker to advance the simulation
  Physics.util.ticker.subscribe(function( time, dt ){

      world.step( time );
  });

  // start the ticker
  Physics.util.ticker.start();

});
body {
  /*background: #121212;*/
}
.pjs-meta {
  display: none;
}

#viewport {
  border: 1px solid #666;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src='http://wellcaffeinated.net/PhysicsJS/assets/scripts/vendor/physicsjs-0.5.0/physicsjs-full-0.5.0.min.js'></script>

<canvas id="viewport" width="300" height="300"></canvas>

我在 GitHub 或任何使用它的地方找不到任何代码。有人可以提供一些指导吗?

最佳答案

I can't find any code on GitHub or anywhere using it. Can someone lend some guidance, please?

您可以尝试阅读unit tests in the .spec, on Github .

示例测试,即使几乎不了解 Javascript,它也应该看起来非常可读:

it("should initialize provided a width/height and point", function() {
    var aabb = Physics.aabb( 4, 5, { x: 20, y: 9 } );
    matches( aabb, { x: 20, y: 9, hw: 2, hh: 2.5 });
});

spec.js 看起来是测试代码。测试实际上兼作文档,像spec这样的库可以让测试代码像文档一样读取。此外,测试代码当然是如何使用代码的示例的集合。享受吧。

Try reading other test code.

关于javascript - 正确使用Physics.aabb.union(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35422830/

相关文章:

javascript - 解决 webpack 中的外部子模块依赖

javascript - 按带有 ":"(冒号)的属性选择元素

javascript - 如何在当前鼠标悬停的元素上显示/隐藏 div?

javascript - Bluebird - 混合each() 和all()?

javascript - setAttribute() 无法按我预期的方式工作

javascript - 生成砌体布局,然后对其应用重力

compiler-errors - CGAL surface_mesh封面示例

c++ - 对象旋转/平移后重新计算正确的 AABB

c++ - 水平碰撞不起作用 AABB C++

javascript - Physicsjs - 使用 Sprite 表将一个位图的部分分配给多个主体