0

If I have some bodies that are repulsed and also have a distance. Right now they bounce back and forth endlessly. Is there a way to add a resistance to the physics engine so that they may come to rest?

var context = Engine.createContext();

var contextSize = context.getSize();

var handler = new EventHandler();

var physicsEngine = new PhysicsEngine();

function addBall(color, i) {

    var ball = new Surface ({
      size: [50,50],
      properties: {
        backgroundColor: color,
        borderRadius: '100px'
      }
    });

    ball.state = new StateModifier({origin:[0.5,0.5]});

    ball.particle = new Circle({radius:50, mass: 20 + i * 1});

    return ball;
}
var leftWall    = new Wall({normal : [1,0,0],  distance : contextSize[0]/2.0, restitution : 0.7});
var rightWall   = new Wall({normal : [-1,0,0], distance : contextSize[0]/2.0, restitution : 0.7});
var topWall     = new Wall({normal : [0,1,0],  distance : contextSize[1]/2.0, restitution : 0.7});
var bottomWall  = new Wall({normal : [0,-1,0], distance : contextSize[1]/2.0, restitution : 0.7});
var walls = [leftWall,rightWall,bottomWall,topWall];

var bodies = [];

_.each(['yellow', 'blue','green', 'red', 'orange', 'purple','gray', 'black'],function(color, index) {
    var body = addBall(color, index);
    bodies.push(body);
});
_.each(bodies, function (body) {
    physicsEngine.addBody(body.particle);
    context.add(body.state).add(body);
});

var particles = _.map(bodies, function(body){
    return body.particle;
});

_.each(particles, function(particle, index){

    var r = new Repulsion({strength: 61});

    var d = new Distance({length: 80, minLength: 0});

    physicsEngine.attach(r, [particles[(index + 4) % 8]], particle);

    physicsEngine.attach(r, [particle], particles[(index + 4) % 8]);

    physicsEngine.attach(d, [particles[(index + 1) % 8]], particle);
    if (index == 0) {
        physicsEngine.attach(d, [particle], particles[7]);
    } else {
        physicsEngine.attach(d, [particle], particles[(index - 1) % 8]);
    }
    particle.setVelocity([0.004101*index,0.004101*index,0 ]);

});

_.each(walls, function(wall) {
    physicsEngine.attach(wall);

});


Engine.on('prerender', function(){
  _.each(bodies, function(body) {
        body.state.setTransform(body.particle.getTransform());
    });
});
Dan Baker
  • 1,757
  • 3
  • 21
  • 36

1 Answers1

1

I was looking for Drag!

var drag = new Drag({strength: 0.1})

physicsEngine.attach(drag, [particle]);
Dan Baker
  • 1,757
  • 3
  • 21
  • 36