liabru / matter-attractors Goto Github PK
View Code? Open in Web Editor NEWan attractors plugin for matter.js
License: MIT License
an attractors plugin for matter.js
License: MIT License
It seems that an attracting body will not attract other bodies added to the world before it.
Demo here: https://codepen.io/lonekorean/pen/cfc51f387d8f32727288f3a59113ceeb
Comment/uncomment line 42/43 to see.
I'm making a game with matter.js where you can create your own solar system, but instead of elliptical orbits like you'd expect where the planets would get close to the star and then swing around it, the planets always keep their orbits completely centered around the star. Is there any way I can fix this?
Note: I have tried to use the built-in Newtonian gravity, but it doesn't work at all with anything I've tried.
Hello!
I'm attempting to achieve and effect where the attraction between bodies varies depending on their distance and mass (like a magnet). I've attempted to use the following function, using the standard vector math formula, but so far have not been able to replicate the effect:
function(bodyA, bodyB) {
let vx = bodyB.position.x - bodyA.position.x;
let vy = bodyB.position.y - bodyA.position.y;
let m = Math.sqrt(vx * vx + vy * vy);
let dx = vx / m;
let dy = vy / m;
return {
x: (dx * (bodyA.mass * bodyB.mass)) / m,
y: (dy * (bodyA.mass * bodyB.mass)) / m
};
}
Probably I don't understand properly which x/y values should be returned. Any assistance would be greatly appreciated! π
I have a spherical area, and I'd like for particles to naturally distribute themselves somewhat evenly inside the area.
Could I possibly use Matter's gravity along with this to somehow make them want to distribute themselves evenly inside the area?
I am trying to use this plugin, but maybe Im not understanding how the function is supposed to work.
I trying to apply a repulsion so I am using the plugin like so:
plugin: {
attractors: [
function(bodyA, bodyB) {
var force = {
x: -(bodyA.position.x - bodyB.position.x) * 1e-5,
y: -(bodyA.position.y - bodyB.position.y) * 1e-5,
};
// apply force to both bodies
Matter.Body.applyForce(bodyA, bodyA.position, Matter.Vector.neg(force));
Matter.Body.applyForce(bodyB, bodyB.position, force);
}
]
}
but the force seems to be greater when the bodies are further from each other. why is this?
The plugin is breaking when running in IE11. You can try it with your example page aswell.
Error is: "Invalid calling object"...
Hi there,
I was playing around with matter and I am very impressed. So first of all, a big thank you for this.
So I tried to use the matter-attractor plugin in a webpack context. Using something like this:
import Matter from 'matter-js';
import MatterAttractor from 'matter-attractors';
Matter.use(MatterAttractor);
But webpack is throwing an error at me:
ERROR Failed to compile with 1 errors
This dependency was not found:
* Matter in ./~/matter-attractors/build/matter-attractors.js
To install it, you can run: npm install --save Matter
So thats a little strange, since the Matter dependency is called matter-js
. I checked the source and saw that under the build/
directory there are some calls to require('Matter')
. I decided to not mess around with that and as the index.js
is using require('matter-js')
my current workaround is something ugly like this:
import Matter from 'matter-js';
Matter.use(require('../../../../node_modules/matter-attractors/index'));
And its working just fine.
Hope you can fix the issue soon.
Thanks in advance. :)
main.js:5301 matter-js: Plugin.use: [email protected] is for matter-js@^0.12.0 but installed on [email protected].
Because static bodies have infinite mass and other propeties, after setting at least 1 body to static this results with completely bugged stage (bodies get infinite prop.)
Actually there should be possibility to add gravity force for static bodies - is it required for body to has infinity?
Anyway I think quick fix like this would be nice anyway because everything just stops working at the moment.
gravity: function(bodyA, bodyB) {
// apply force only if both bodies are not static
if(!bodyA.isStatic && !bodyB.isStatic){
// use Newton's law of gravitation
var bToA = Matter.Vector.sub(bodyB.position, bodyA.position),
distanceSq = Matter.Vector.magnitudeSquared(bToA) || 0.0001,
normal = Matter.Vector.normalise(bToA),
magnitude = -MatterAttractors.Attractors.gravityConstant * (bodyA.mass * bodyB.mass / distanceSq),
force = Matter.Vector.mult(normal, magnitude);
// to apply forces to both bodies
Matter.Body.applyForce(bodyA, bodyA.position, Matter.Vector.neg(force));
Matter.Body.applyForce(bodyB, bodyB.position, force);
}
}
Nvmd.
Hey so I'm working on creating body orbits which pull certain other bodies towards them.
I've managed to create the code that allows some bodies to only exert forces on certain other bodies but right now my orbits don't work properly as the forces are constantly being applied to the attracted ones even after they reach the desired orbit (range away) around the attractor.
Is there a built-in sleep switch for matter-attractors? If not can you maybe point me in the right direction where I could implement such a feature myself within your project? This would really come in handy as the device that I'm coding this for doesn't have a lot of processing power (it's a table top AR projector) and I want to make the app take up the least amount of CPU time possible.
Thanks!
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 :)
Nothing too crucial, but just wanted to note version declaration is not consistent.
Latest package version states 0.1.6
, but the plugin is registered as version 0.1.4
:
matter-attractors/build/matter-attractors.js
Line 107 in c470ed4
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.