0

I've been experimenting with attractors in physics.js, rigging up a simple object in zero gravity, with an attractor at a point. This creates a great little 'gravity well' as can be seen here.

Where the simple square vector attracts towards a point, at 'x':200,'y':200, and then orbits around it. I'm looking for a way to turn this attractor into more of a gravity well, so that the objects attracted to it slow down over time and eventually settle static and stationary at the point of the attractor, until it was collided with or dragged with the mouse again. Is this a possibility?

Currently the object is created with:

 var bodies = [Physics.body('convex-polygon', {
  // place the center of the square at (0, 0)
  x: 150,
  treatment : 'dynamic',
  cof: 0.01,
  mass : 1,
  y: 100,
  vertices: [
    { x: 0, y: 0 },
    { x: 0, y: 200 },
    { x: 200, y: 200 },
    { x: 200, y: 0 }
  ]
})];

The attractor is created thusly:

var attractor = Physics.behavior('attractor', {
    order: 0,
    strength: 0.0005
}).applyTo(bodies);

attractor.position({'x':200,'y':200});

Affecting the strength of the attractor doesn't appear to help, it just changes the speed of the attraction and subsequent orbit.

I'm looking for a way, in effect, to add friction to the entire space, which I think will do the job in naturally slowing down the object so it ends up stationary at the attractor point. Not sure how to go about this with PhysicsJS.

Michael Watson
  • 224
  • 3
  • 11

1 Answers1

0
  1. There is the possibility to create your own Attractor-Behaviour: See this documentation.

  2. If you don't want to that, you can set the option min of the attractor to the size of the polygon, so the forces stops, when the body is at the center of the attractor. Strength and order are optional options, so you don't need to specify them (According to the API). For example this should work for you:

    world.add(Physics.behavior("attractor", {
        min: 200,
        pos: {"x": 200, "y": 200}
    }));
    
Enthusiasmus
  • 303
  • 2
  • 9