• Content Count

  • Joined

  • Last visited

  1. Well I found it out. var sun = this.matter.add.image(400, 200, 'sun', null, { shape: { type: 'circle', radius: 64 }, plugin: { attractors: [Phaser.Physics.Matter.Matter.Plugin.resolve("matter-attractors").Attractors.gravity] } }); I figured it out attractors was an array.
  2. Hi everyone, I'm working on a game prototype involving planet gravitational physics. I got inspired by this example in the Phaser 3 official example archives, feature the following code : var sun = this.matter.add.image(400, 200, 'sun', null, { shape: { type: 'circle', radius: 64 }, plugin: { attractors: [ function (bodyA, bodyB) { return { x: (bodyA.position.x - bodyB.position.x) * 0.000001, y: (bodyA.position.y - bodyB.position.y) * 0.000001 }; } ] } }); Now, I'd like a more Newtonian function for attraction, and it appears Matter-js got already one ready in MatterAttractors.js, featuring the following : /** * Defines some useful common attractor functions that can be used * by pushing them to your body's `body.plugin.attractors` array. * @namespace MatterAttractors.Attractors * @property {number} gravityConstant The gravitational constant used by the gravity attractor. */ Attractors: { gravityConstant: 0.001, /** * An attractor function that applies Newton's law of gravitation. * Use this by pushing `MatterAttractors.Attractors.gravity` to your body's `body.plugin.attractors` array. * The gravitational constant defaults to `0.001` which you can change * at `MatterAttractors.Attractors.gravityConstant`. * @function MatterAttractors.Attractors.gravity * @param {Matter.Body} bodyA The first body. * @param {Matter.Body} bodyB The second body. * @returns {void} No return value. */ gravity: function(bodyA, bodyB) { // 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); } } }; My main problem at this point is that I don't know how to call that gravity function as the callback function for the attractor plugin of my object. Also, I'm pretty new to using Phaser and Javascript is not a language I used much before (but I picked it for technical reasons). I'm learning a lot and it's quite well written. But I'm basically lost whenever it's about giving a list of options to an object, such as shape or plugin.attractors, or even the config var that set up the engine, because I don't know what are the *valid* options available, or their valid values. And when I use invalid options, I don't get much feedback in term of error messages, it just doesn't work. I've tried many things to try to "reach" that function but it never worked. At best my object isn't rendered, at worst I got an error "undefined" element. Oh and I also got a case where my object was rendered but not doing any attraction what so ever. I'd appreciate some help to keep me coding. Thank you.