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.
Asked
Active
Viewed 1.1k times
3 Answers
70
Try
engine.world.gravity.y = 0;
where engine
is your engine instance.

sowee15
- 1,197
- 7
- 15
-
3This 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
-
1This 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
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