schteppe / p2.js

JavaScript 2D physics library
Other
2.63k stars 329 forks source link

[FEATURE] Gravitational points #306

Closed moonheart08 closed 6 years ago

moonheart08 commented 6 years ago

I.e. a point in the world that pulls on other objects. Why? For things like, say, a black hole that wanders around the world and destroys objects that are pulled into it.

schteppe commented 6 years ago

You mean something like this?

world.on('postStep', function(){
    body.force[0] -= 100 * body.position[0];
    body.force[1] -= 100 * body.position[1];
    if(p2.vec2.length(body.position) < 2){
        // destroy logic
    }
});
moonheart08 commented 6 years ago

@schteppe Similar, yes, but with support for multiple points.

jtenner commented 6 years ago

Yeah. Just loop over every point mass, then add the forces for each one yourself.

moonheart08 commented 6 years ago

Yea, i see the point. Thanks. Gonna close.