# An interesting physics-based game

**URL:** <https://discourse.julialang.org/t/an-interesting-physics-based-game/124682>\
**Category:** Offtopic\
**Tags:** physics, games\
**Created:** [January 11, 2025, 6:41pm UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682 "2025-01-11T18:41:35Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [January 11, 2025, 6:41pm UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/1 "2025-01-11T18:41:35Z")

</div>

I recently had some time off and built this game that I think turned out well (enough):

> **[Homeward Bound - Master Gravitational Slingshots](https://homeward-bound-game.vercel.app/)**
>
> Navigate your spacecraft through complex gravitational fields in this physics-based puzzle game featuring realistic n-body gravitational simulations and slingshot mechanics.

I suspect some folks in this community might get a kick out of it. Feel free to provide any feedback!

---

<div class="post-metadata">

**Author:** ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)\
**Post date:** [January 11, 2025, 7:40pm UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/2 "2025-01-11T19:40:32Z")

</div>

Love it 😊

---

<div class="post-metadata">

**Author:** ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)\
**Post date:** [January 11, 2025, 8:06pm UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/3 "2025-01-11T20:06:13Z")

</div>

I like this. However, it seems each level is generated randomly, right? Some levels seem not to have solutions, within the allowed velocity and angle ranges.

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [January 11, 2025, 8:42pm UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/4 "2025-01-11T20:42:24Z")

</div>

> [@linwaytin](#):
>
> I like this. However, it seems each level is generated randomly, right? Some levels seem not to have solutions, within the allowed velocity and angle ranges.

That’s correct. Sometimes, there is no solution and you just have to start over. I started thinking about ways to ensure a solution exists, but I didn’t get around to it. I’m open to suggestions!

---

<div class="post-metadata">

**Author:** ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)\
**Post date:** [January 11, 2025, 9:14pm UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/5 "2025-01-11T21:14:26Z")

</div>

If the velocity and the angle are the only two parameters determining the trajectory, a stupid method is checking all possible trajectories for a given gravitational field.

But I do not how to deal with the possible collisions with planets.

---

<div class="post-metadata">

**Author:** ![jakobjpeters](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobjpeters/32/207797_2.png) [@jakobjpeters](https://discourse.julialang.org/u/jakobjpeters)\
**Post date:** [January 11, 2025, 9:26pm UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/6 "2025-01-11T21:26:14Z")

</div>

It was fun! An optional challenge could be to limit the length of the trajectory trace 🙂

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [January 13, 2025, 8:56pm UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/7 "2025-01-13T20:56:27Z")

</div>

Neat.

On my mobile I cannot get past level 1 though.

If I fail, I can retry, but if I succeed the UFO simply stays at the planet and nothing happens.

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [January 13, 2025, 10:02pm UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/8 "2025-01-13T22:02:00Z")

</div>

Hmmm…I’m not sure what’s going on there but the game is definitely not mobile-friendly right now. It’s using a fixed canvas size (800 x 600) because that made everything easier. It likely only works as intended on large screens 🫤

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 13, 2025, 10:11pm UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/9 "2025-01-13T22:11:06Z")

</div>

It also works on Android phones, but requires some fiddly unzooming to fit the canvas to the screen. Pin the top and pan the canvas to reach the bottom, then zoom out with two fingers.

PS:  
By the way, can you tell us more about what goes on under the hood?

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [January 13, 2025, 10:47pm UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/10 "2025-01-13T22:47:49Z")

</div>

> [@rafael.guerra](#):
>
> PS:  
> By the way, can you tell us more about what goes on under the hood?

On your PC (doesn’t work on my Android device) you can hover over the information icon (🛈) and it will tell you more about the game. The same info is also on the GitHub repo:

> **[GitHub - mthelm85/homeward-bound-game](https://github.com/mthelm85/homeward-bound-game)**
>
> Contribute to mthelm85/homeward-bound-game development by creating an account on GitHub.

All of the action takes place in `src/game/scenes/MainScene.js`

In a nutshell:

The code calculates how strongly each planet would pull on the spacecraft using Newton’s law of gravitation F = \frac{G m\_1 m\_2}{r^2}. The masses of the planets are assigned somewhat arbitrarily (it’s their size in pixels multiplied by 100). Then, the code uses the Runge-Kutta 4th order method (RK4) to calculate the spacecraft’s motion under these gravitational forces. It does this at each time step in the game (I have the time step set to 0.2 seconds) to predict the path and speed of the spacecraft.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 13, 2025, 11:42pm UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/11 "2025-01-13T23:42:22Z")

</div>

> [@mthelm85](#):
>
> The masses of the planets are assigned somewhat arbitrarily (it’s their size in pixels multiplied by 100)

EDIT: After looking at the code, it seems that the planet size used is its diameter ~ `2r` while for a constant planet density the mass is proportional to ~ `r³`. Shouldn’t the masses be scaled with ~ `r³`?

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [January 14, 2025, 3:28pm UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/12 "2025-01-14T15:28:57Z")

</div>

> [@rafael.guerra](#):
>
> Shouldn’t the masses be scaled with ~ `r³`?

😁 I tried that, as well as many variations, but many of them caused strange behaviors. I found that simply doing `size * 100` resulted in the best game play experience, so I went with it.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 14, 2025, 3:52pm UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/13 "2025-01-14T15:52:22Z")

</div>

That is what we call: massaging the physics 🙂

---

<div class="post-metadata">

**Author:** ![Pitipat\_Wongsittikan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pitipat_wongsittikan/32/203982_2.png) [@Pitipat\_Wongsittikan](https://discourse.julialang.org/u/Pitipat_Wongsittikan)\
**Post date:** [January 16, 2025, 7:54am UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/14 "2025-01-16T07:54:23Z")

</div>

I encounter this too, but it was after a good time 🙂 Love it!

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [January 18, 2025, 11:43am UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/15 "2025-01-18T11:43:28Z")

</div>

> [@rafael.guerra](#):
>
> Shouldn’t the masses be scaled with ~ `r³`?

Only if the density is constant.

> [@mthelm85](#):
>
> I found that simply doing `size * 100` resulted in the best game play experience

Linear scaling is actually the correct one for black holes.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 18, 2025, 11:46am UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/16 "2025-01-18T11:46:50Z")

</div>

> [@greatpet](#):
>
> Only if the density is constant.

I wrote the obvious already.

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [January 18, 2025, 12:05pm UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/17 "2025-01-18T12:05:40Z")

</div>

Ahh, totally missed that!

---

<div class="post-metadata">

**Author:** ![aquadry](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquadry/32/214939_2.png) [@aquadry](https://discourse.julialang.org/u/aquadry)\
**Post date:** [January 26, 2025, 11:21pm UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/18 "2025-01-26T23:21:55Z")

</div>

Congra, awesome. 👏  
Gona get a look to source code.

---

<div class="post-metadata">

**Author:** ![Kyjor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kyjor/32/48132_2.png) [@Kyjor](https://discourse.julialang.org/u/Kyjor)\
**Post date:** [January 28, 2025, 12:45pm UTC](https://discourse.julialang.org/t/an-interesting-physics-based-game/124682/19 "2025-01-28T12:45:19Z")

</div>

This is pretty cool! I played a few levels, and it seems pretty easy. I think the best way to ensure a solution exists is to just handcraft some levels! That all depends on how much you are trying to work on this though 😄
