liabru / matter-js

a 2D rigid body physics engine for the web ▲● ■
MIT License
16.88k stars 1.97k forks source link

Is gravitational pull possible? #1220

Open SciJosh opened 1 year ago

SciJosh commented 1 year ago

I am trying to get two bodies to attract to each other based on their mass's. I've tried using the plugin matter-attractors but I can't get it to work. Anyways, what would this look like in a script and how can it be manipulated?

Kibergx commented 1 year ago

You need to call applyForce before each update (https://brm.io/matter-js/docs/classes/Body.html#method_applyForce) The position vector should point towards the other body's center, and you can calculate the force based on the two body's position difference.