liabru / matter-js

a 2D rigid body physics engine for the web ▲● ■

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Vulnerability related to the formula of universal gravitation

MLH-AIDS opened this issue · comments

I made such a function according to the formula and called it before updating.

function applyG(body){
    for(let i of Composite.allBodies(engine.world)){
        if(i===body){
            continue;
        }
        let F=__gravitational__*body.mass*i.mass/(distance2body(body,i)**2);
        let p=Vector.sub(i.position,body.position);
        let Fg=Vector.mult(p,F/(p.x**2+p.y**2)**0.5);
        body.force.x+=Fg.x
        body.force.y+=Fg.y
    }
    function distance2body(b1,b2){
        let s=Vector.sub(b1.position,b2.position);
        return (s.x**2+s.y**2)**0.5;
    }
}
Events.on(runner, "beforeUpdate", ()=>{
  for(let i of Composite.allBodies(engine.world)){
    applyG(i)
  }
});

But when I set a rigid body and set a linear velocity to make it run on a circular track, the linear velocity always seems too small.

Body.setVelocity(ball,{x:(__gravitational__*earth.mass/distance2body(earth.position,ball.position))**0.8,y:0})

When near a celestial body, the calculated linear velocity is approximately 70, but in reality, a linear velocity of 1100 is needed to make it orbit.