# How i can set gravity for one object so that it acts by attraction on another object?

**URL:** <https://phaser.discourse.group/t/how-i-can-set-gravity-for-one-object-so-that-it-acts-by-attraction-on-another-object/9759>\
**Category:** Phaser 3\
**Created:** [June 9, 2021, 6:19pm UTC](https://phaser.discourse.group/t/how-i-can-set-gravity-for-one-object-so-that-it-acts-by-attraction-on-another-object/9759 "2021-06-09T18:19:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![FiveTony](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/fivetony/32/5265_2.png) [@FiveTony](https://phaser.discourse.group/u/FiveTony)\
**Post date:** [June 9, 2021, 6:19pm UTC](https://phaser.discourse.group/t/how-i-can-set-gravity-for-one-object-so-that-it-acts-by-attraction-on-another-object/9759/1 "2021-06-09T18:19:53Z")

</div>

Hello everyone! Can you please tell me if I can somehow set gravity for one object so that it acts by attraction on another object? as I understand it, createGravityWell won’t work for me

![image](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/f/fb826aab6aa64dcf99f6bc200d0046a0d8bc3676.jpeg)

---

<div class="post-metadata">

**Author:** ![Quinten](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/quinten/32/5279_2.png) [@Quinten](https://phaser.discourse.group/u/Quinten)\
**Post date:** [June 10, 2021, 7:56am UTC](https://phaser.discourse.group/t/how-i-can-set-gravity-for-one-object-so-that-it-acts-by-attraction-on-another-object/9759/2 "2021-06-10T07:56:46Z")

</div>

If you are using matter physics, you can add a constraint between 2 objects.

> **[Phaser - Examples - Basic Constraint](https://phaser.io/examples/v3/view/physics/matterjs/basic-constraint)**
>
> Desktop and Mobile HTML5 game framework. A fast, free and fun open source framework for Canvas and WebGL powered browser games.

The constraint has a length property which you can update.

```javascript
let myConstraint = this.matter.add.constraint(ballA, ballB, 100, 0.2);

myConstraint.length = myConstraint.length - 1; // somewhere in an update function

```

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [June 10, 2021, 8:22am UTC](https://phaser.discourse.group/t/how-i-can-set-gravity-for-one-object-so-that-it-acts-by-attraction-on-another-object/9759/3 "2021-06-10T08:22:22Z")

</div>

Matter actually has an attractors plugin.

[Example](http://labs.phaser.io/edit.html?src=src%5Cphysics%5Cmatterjs%5Cattractors.js).

---

<div class="post-metadata">

**Author:** ![FiveTony](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/fivetony/32/5265_2.png) [@FiveTony](https://phaser.discourse.group/u/FiveTony)\
**Post date:** [June 10, 2021, 8:27am UTC](https://phaser.discourse.group/t/how-i-can-set-gravity-for-one-object-so-that-it-acts-by-attraction-on-another-object/9759/4 "2021-06-10T08:27:06Z")

</div>

this is a great example and advice! however, how do I use matter physics if my game already uses arcade physics?

 ![image](https://global.discourse-cdn.com/free1/uploads/phaser1/original/2X/8/8acece12628c49551de8a0c3693058441b683ca5.png)

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [June 10, 2021, 8:32am UTC](https://phaser.discourse.group/t/how-i-can-set-gravity-for-one-object-so-that-it-acts-by-attraction-on-another-object/9759/5 "2021-06-10T08:32:36Z")

</div>

> [@Can't get arcade physics to work alongside matter physics](https://phaser.discourse.group/t/cant-get-arcade-physics-to-work-alongside-matter-physics/9692/4):
>
> class GameScene extends Phaser.Scene { constructor () { super({ key: 'game', physics: { arcade: { debug: true, gravity: { y: 0 } }, matter: { debug: true, gravity: { y: 0 }, debugShowBody: true, debugBodyColor: 0x0000ff } } }); } } var config = { // … scene: [BootScene, GameScene] };

Or just change everything to matter.

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [June 10, 2021, 4:54pm UTC](https://phaser.discourse.group/t/how-i-can-set-gravity-for-one-object-so-that-it-acts-by-attraction-on-another-object/9759/6 "2021-06-10T16:54:11Z")

</div>

https://codepen.io/samme/embed/preview/LYWJrBZ?default-tabs=js%2Cresult&height=300&host=https%3A%2F%2Fcodepen.io&slug-hash=LYWJrBZ
