我正在努力弄清楚如何使用PhysicsJS。我首先只想知道如何改变,让我们说一个位置或一个物体的速度点击.但我就是搞不懂!
( function()
{
var viewWidth = 500,
viewHeight = 300,
renderer = Physics.renderer( 'canvas',
{
el: 'viewport',
width: viewWidth,
height: viewHeight,
meta: false,
styles:
{
'circle' :
{
strokeStyle: 'hsla(60, 37%, 17%, 1)',
lineWidth: 1,
fillStyle: 'hsla(60, 37%, 57%, 0.8)',
angleIndicator: 'hsla(60, 37%, 17%, 0.4)'
}
}
}),
viewportBounds = Physics.aabb(0, 0, viewWidth, viewHeight),
constraint = {
aabb: viewportBounds,
restitution: 0.99,
cof: 0.99
},
ballOptions = {
x: 100, // x-coordinate
y: 100, // y-coordinate
vx: 0.0, // velocity in x-direction
vy: 0.0, // velocity in y-direction
radius: 20
},
gravity = Physics.behavior('constant-acceleration',
{
acc: { x : 0, y: 0.0004 }
}),
ball = Physics.body('circle', ballOptions );
Physics( function( world )
{
// add the renderer
world.add( renderer );
// add circle
world.add( ball );
// subscribe to ticker to advance the simulation
Physics.util.ticker.subscribe(function( time, dt )
{
world.step( time );
});
// on every step...
world.subscribe( 'step', function()
{
world.render();
});
world.subscribe( 'collisions:detected', function( $collision )
{
});
var onElementClick = function()
{
// do something
};
document.getElementById( 'viewport' ).addEventListener( 'click', onElementClick, false );
// Lets GO!
Physics.util.ticker.start();
});
})();任何帮助都很感激
发布于 2014-02-22 19:49:40
一种选择是将创建但从未添加到世界上的重力进行单击。
world.add(gravity);这是欺骗,因为你要求改变物体的位置或速度。要做到这一点,修改球的状态。看身体上的医生,特别是属性。您可以设置state.pos来移动它。为了启动它,设定速度:
ball.state.vel.set(.1,-.5); // move right and upward设定速度的小提琴
https://stackoverflow.com/questions/21615444
复制相似问题