我正在学习PhysicsJS,我试着使用友联市,如下所示:
// 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或任何使用它的地方找不到任何代码。有人能给我指点吗?
发布于 2016-02-17 00:09:01
弄明白了。我使用的是一个超级老版本(physicsjs-0.5.0)。我链接到了最新版本(physicsjs-0.7.0),它具有多更多的功能(这两个版本之间有4,088行新代码)。我不得不重构我的代码一点,以符合更新的规范,但它是好的!
发布于 2016-02-16 02:56:55
我在GitHub或任何使用它的地方找不到任何代码。有人能给我指点吗?
你可以试着读.spec中的单元测试,在Github上。
示例测试,即使几乎不了解Javascript,它也应该看上去非常可读性:
它(“应初始化提供的宽度/高度和点”,函数(){ var aabb = Physics.aabb( 4,5,{ x: 20,y: 9 });匹配( aabb,{ x: 20,y: 9,hw: 2,hh: 2.5 });};
spec.js看起来是测试代码。测试实际上是文档的翻倍,而像spec这样的库可以让测试代码读起来像文档一样。此外,测试代码当然是关于如何使用代码的示例集合。好好享受吧。
https://stackoverflow.com/questions/35422830
复制相似问题