# How to make objects 'fall' towards each other?

**URL:** <https://forum.defold.com/t/how-to-make-objects-fall-towards-each-other/67877>\
**Category:** Questions\
**Created:** [March 22, 2021, 9:56am UTC](https://forum.defold.com/t/how-to-make-objects-fall-towards-each-other/67877 "2021-03-22T09:56:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frankie\_Freeland](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/frankie_freeland/32/24308_2.png) [@Frankie\_Freeland](https://forum.defold.com/u/Frankie_Freeland)\
**Post date:** [March 22, 2021, 9:56am UTC](https://forum.defold.com/t/how-to-make-objects-fall-towards-each-other/67877/1 "2021-03-22T09:56:43Z")

</div>

Hi I’m new with using defold and I’ve been messing around with the physics in the game, however it seems like there is not much control over the direction of gravity. I’ve been trying to make it so objects will fall towards one centre object, much like they would when falling down. Eg: if I were to spawn an object to the side, or below this centre object it would ‘fall’ sideways or upwards towards it. Is there any possible way to do this simply, I’m not very experienced so any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [March 22, 2021, 10:14am UTC](https://forum.defold.com/t/how-to-make-objects-fall-towards-each-other/67877/2 "2021-03-22T10:14:39Z")

</div>

There is no way to set the gravity for individual game objects, so I think your best option is to use apply\_force every frame, to make each game object move towards the target game object in the middle.

> **[apply\_force - API reference (Collision object)](https://defold.com/ref/beta/physics/#apply_force)**
>
> Post this message to a collision-object-component to apply the specified force on the collision object. The collision object must be dynamic. | Defold is a free and open game engine used for development of console, desktop, mobile and web games.

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [March 22, 2021, 10:44am UTC](https://forum.defold.com/t/how-to-make-objects-fall-towards-each-other/67877/3 "2021-03-22T10:44:36Z")

</div>

@totebo is correct. There is no way to set gravity at runtime (yet). And even if it did, it would be applied to _all_ objects. (Physics engines usually treat the gravity as a global force that is applied each frame)

As suggested, you are better off calculating this yourself.  
Set the gravity to 0, and calculate a force to be applied for each object in the update function.

---

<div class="post-metadata">

**Author:** ![Frankie\_Freeland](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/frankie_freeland/32/24308_2.png) [@Frankie\_Freeland](https://forum.defold.com/u/Frankie_Freeland)\
**Post date:** [March 23, 2021, 6:59pm UTC](https://forum.defold.com/t/how-to-make-objects-fall-towards-each-other/67877/4 "2021-03-23T18:59:04Z")

</div>

I’m not sure you understand, I want objects to fall towards the target object no matter the targets position or the spawned in objects position, as if it where a planet.

---

<div class="post-metadata">

**Author:** ![Pawel](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pawel/32/24623_2.png) [@Pawel](https://forum.defold.com/u/Pawel)\
**Post date:** [March 23, 2021, 7:21pm UTC](https://forum.defold.com/t/how-to-make-objects-fall-towards-each-other/67877/5 "2021-03-23T19:21:17Z")

</div>

Hmm, just thinking out loud, but isn’t such effect possible with joints?

---

<div class="post-metadata">

**Author:** ![Potota](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/potota/32/33404_2.png) [@Potota](https://forum.defold.com/u/Potota)\
**Post date:** [March 23, 2021, 7:45pm UTC](https://forum.defold.com/t/how-to-make-objects-fall-towards-each-other/67877/6 "2021-03-23T19:45:11Z")

</div>

Assuming you’re not using dynamic collision objects, you can do something like this (untested!):

```lua
-- Tweak this to suit your game
local GRAVITY = 10

function init(self)
	self.velocity = vmath.vector3()
end

function update(self, dt)
	local current_position = go.get_position()
	
	-- Get the unit vector (vector of length 1) representing the direction from the player to the planet
	local direction = vmath.normalize(go.get_position("planet") - current_position)
	
        -- Apply gravity to the vector to get acceleration in the proper direction
        -- Then add this acceleration vector to our current velocity
	self.velocity = self.velocity + (direction * GRAVITY)

        -- Now all we have to do is add our velocity to our current position to get our next position
	go.set_position(current_position + (self.velocity * dt))
end

```

---

<div class="post-metadata">

**Author:** ![Frankie\_Freeland](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/frankie_freeland/32/24308_2.png) [@Frankie\_Freeland](https://forum.defold.com/u/Frankie_Freeland)\
**Post date:** [March 24, 2021, 7:42am UTC](https://forum.defold.com/t/how-to-make-objects-fall-towards-each-other/67877/7 "2021-03-24T07:42:30Z")

</div>

This seemed to work with a bit of tweaking, Thank you!

---

<div class="post-metadata">

**Author:** ![ross.grams](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@ross.grams](https://forum.defold.com/u/ross.grams)\
**Post date:** [March 24, 2021, 12:38pm UTC](https://forum.defold.com/t/how-to-make-objects-fall-towards-each-other/67877/8 "2021-03-24T12:38:54Z")

</div>

One little tweak to that: You should also multiply `GRAVITY` by `dt` in update so the acceleration isn’t different for different framerates.
