Code Monkey home page Code Monkey logo

matter-attractors's Introduction

matter-attractors

An attractors plugin for matter.js

Build Status

This plugin makes it easy to apply continual forces on bodies. It's possible to simulate effects such as wind, gravity and magnetism.

Demo

See the demo.

matter-attractors

Install

Get the matter-attractors.js file directly or get it via npm:

npm install matter-attractors

Dependencies

Usage

Matter.use('matter-attractors');
// or
Matter.use(MatterAttractors);

See Using Plugins for more information.

Custom attractors

Attractors are just functions that are pushed to body.plugin.attractors. An attractor function accepts two bodies bodyA and bodyB, where bodyA is always the attracting body and bodyB is the body being attracted.

The attractor will be called against every other body in the engine in the place of bodyB, on every engine update. If a force is returned, it will be applied to bodyB only.

Basic usage

An example of a body that attracts other bodies to it:

var body = Matter.Bodies.circle(0, 0, 10, {
  plugin: {
    attractors: [
      function(bodyA, bodyB) {
        return {
          x: (bodyA.position.x - bodyB.position.x) * 1e-6,
          y: (bodyA.position.y - bodyB.position.y) * 1e-6,
        };
      }
    ]
  }
);

It's possible here to use collision filters too if needed, by using Detector.canCollide and returning null to skip the pair.

Advance usage

In advance usage, e.g. where forces apply to both bodies, instead of returning the force it can instead be applied manually to both bodies inside the function using Body.applyForce.

var body = Matter.Bodies.circle(0, 0, 10, {
  plugin: {
    attractors: [
      function(bodyA, bodyB) {
        var force = {
          x: (bodyA.position.x - bodyB.position.x) * 1e-6,
          y: (bodyA.position.y - bodyB.position.y) * 1e-6,
        };

        // apply force to both bodies
        Body.applyForce(bodyA, bodyA.position, Matter.Vector.neg(force));
        Body.applyForce(bodyB, bodyB.position, force);
      }
    ]
  }
);

Built in attractors

There are some attractors you can push to body.plugin.attractors:

  • MatterAttractors.Attractors.gravity - uses Newton's gravitational laws to apply an attractive force on both bodies

Documentation

See the API docs.

Examples

Check out the examples or try them out first:

matter-attractors's People

Contributors

liabru avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.