liabru / matter-attractors

an attractors plugin for matter.js
MIT License
143 stars 31 forks source link

Wind? #3

Open YoungElPaso opened 7 years ago

YoungElPaso commented 7 years ago

Wondering how you'd simulate wind. Single attractor pulling all bodies towards it? Negative attractor gravity pushing all away? (that seems more like a circular 'hill' with even slopes).

I'm interested in Matter and your plugin to simulate a game of golf, so looking to simulate forces like wind, friction and gravity with 2 dimensional slopes etc.

Thanks for any insights and great plugin :)

sampumon commented 6 years ago

That works, though as wind is just a random-ish force in one direction, I ended up skipping attractors plugin:

// WIND GENERATOR
Matter.Events.on(runner, "beforeUpdate", function(e) {
    // random force to the left (from right edge center, affects torque?)
    let from = Matter.Vector.create(render.options.width, render.options.height/2)
    let force = Matter.Vector.create(Matter.Common.random(-2e-4, 0), 0)

    // all bodies affected by the wind
    myComposite.bodies.forEach(function(body) {
        Matter.Body.applyForce(body, from, force)
    })
})
YoungElPaso commented 6 years ago

Ah, that seems a pragmatic way to do it. Thanks!