29

Is there any way to disable gravity in Matter.js? I want my objects (bodies) to keep their physical abilities (like reaction to collision, restitution, friction etc), but I'd like to prevent the reaction to device's gyro and orientation change.

gaitat
  • 12,449
  • 4
  • 52
  • 76
Igal
  • 5,833
  • 20
  • 74
  • 132

3 Answers3

70

Try

engine.world.gravity.y = 0;

where engine is your engine instance.

sowee15
  • 1,197
  • 7
  • 15
  • 3
    This is the correct answer, and should be marked as such. – forgivenson Mar 26 '17 at 01:29
  • As a reference, here are the official docs for the gravity properties: https://brm.io/matter-js/docs/classes/World.html#property_gravity – Michael Kargl Feb 01 '20 at 17:43
  • 1
    This is now deprecated and has been replaced with `engine.gravity.y = 0` [as of v0.17.0](https://github.com/liabru/matter-js/blob/master/CHANGELOG.md#0170-2021-04-11). New docs location: https://brm.io/matter-js/docs/classes/Engine.html#property_gravity – Eron Salling Sep 24 '21 at 17:49
2

You just need to add this to your config:

physics: {
    default: "matter",
    matter: {
        gravity: {x: 0, y: 0},
        // ^^^^^^^^^^^^^^^^^^^
    }
}
ggorlen
  • 44,755
  • 7
  • 76
  • 106
Alexander
  • 21
  • 1
1

You can also set it on the Engine constructor as:

const engine = Engine.create({ gravity: { y: 0 } })

like any other property. Tested on Matter.js 0.19.0 by modifying this hello world: How to make minimal example of matter.js work?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985