gravityはデフォルトでも用意されているが、ビリヤードみたいに上から見下ろしたとした時の摩擦を表現する方法は、デフォルトではない。
書いた。
Physics.behavior 'constant-friction', ( parent ) ->
defaults = {f: 0.00002}
init: ( options ) ->
parent.init.call( this );
@options.defaults( defaults );
@options( options );
@_v = new Physics.vector();
behave: ( data ) ->
bodies = this.getTargets();
f = 0.0002
for body in bodies
ax = 0
ay = 0
cof = body.cof
if body.state.vel.x > 0
ax -= f * cof
else if body.state.vel.x < 0
ax += f * cof
if body.state.vel.y > 0
ay -= f * cof
else if body.state.vel.y < 0
ay += f * cof
@_v.clone {x: ax, y: ay}
body.accelerate( @_v );
これを参考にした。だいたいどんなビヘイビアでも書けそう。
https://github.com/wellcaffeinated/PhysicsJS/blob/master/src/behaviors/constant-acceleration.js#L41
friction = Physics.behavior 'constant-friction', {}
world.add [
friction
]