Disabling gravity in Matter.js
Asked Answered
R

3

30

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.

Rutledge answered 6/4, 2015 at 6:48 Comment(0)
L
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?

Leclaire answered 24/5, 2023 at 7:7 Comment(0)
K
72

Try

engine.world.gravity.y = 0;

where engine is your engine instance.

Knell answered 22/4, 2015 at 1:18 Comment(3)
This is the correct answer, and should be marked as such.Lir
As a reference, here are the official docs for the gravity properties: brm.io/matter-js/docs/classes/World.html#property_gravityFerguson
This is now deprecated and has been replaced with engine.gravity.y = 0 as of v0.17.0. New docs location: brm.io/matter-js/docs/classes/Engine.html#property_gravityHuddle
G
2

You just need to add this to your config:

physics: {
    default: "matter",
    matter: {
        gravity: {x: 0, y: 0},
        // ^^^^^^^^^^^^^^^^^^^
    }
}
Gravois answered 30/3, 2021 at 17:36 Comment(0)
L
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?

Leclaire answered 24/5, 2023 at 7:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.