LoginSignup
0
0

More than 5 years have passed since last update.

PhysicsJSで「上から見下ろしだとしたときの摩擦」を表現するbehaviour

Last updated at Posted at 2015-01-06

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
]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0