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.
Disabling gravity in Matter.js
Asked Answered
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?
Try
engine.world.gravity.y = 0;
where engine
is your engine instance.
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_gravity –
Ferguson
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_gravity –
Huddle You just need to add this to your config:
physics: {
default: "matter",
matter: {
gravity: {x: 0, y: 0},
// ^^^^^^^^^^^^^^^^^^^
}
}
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?
© 2022 - 2024 — McMap. All rights reserved.