DEV Community

Cover image for Solving Every Gravity Puzzle Level Before a Player Sees It
Nesim Razon
Nesim Razon

Posted on Originally published at nesimapps.com

Solving Every Gravity Puzzle Level Before a Player Sees It

In a gravity puzzle the player pulls back a slingshot and planets bend the flight. One level that cannot be won, or a hint that points slightly wrong, and the player stops trusting the game. This is how I built a solver, a level generator and a hint audit for Slinglings, with the real numbers from the code and the tests.

One physics step, used everywhere

Everything rests on one rule: the game must never disagree with itself. Each step, the pull of every planet is added to the courier's velocity, then the courier moves by that velocity. The step is fixed at 1/120 of a second, so a flight is a list of identical small steps and the same input always draws the same path. A flight is cut off after 960 steps, which is 8 seconds.

  • Update: vx += ax * DT, then x += vx * DT, with DT = 1/120 s.
  • Pull: G * mass / (distance squared + 3 squared) to the power 1.5, times the offset to the planet. The added 3 stops the pull from exploding at close range.
  • Limit: 960 steps = 8 seconds, then the flight counts as a timeout.

The aim preview, the real flight and the solver all call this same simulation. When the solver says a shot wins, the phone replays exactly the same steps and the courier really arrives. There is no second, approximate model that could drift away.

The solver: try every shot on a grid

A shot has two numbers, an angle and a power. The solver sweeps the angle through 360 degrees in fixed steps and uses 12 power levels from 0.18 to 1.0. Each shot runs to its end, and the ones that finish in the home box are kept.

  • 2 degree step: 180 angles x 12 powers = 2,160 shots per level.
  • 3 degree step (the generator's choice): 120 x 12 = 1,440 shots.
  • Couriers with a tap ability in mid-air: multiply by 31 tap moments (every 14 steps, from step 0 to 420).

The result is a list of winning shots, not just yes or no. Dividing winners by all shots gives the win rate. A level where a tenth of the grid wins is too easy. The generator rejects layouts above 8 percent for the first three levels of a world and above 12 percent for the rest.

The generator: random layout, then a proof

The generator puts the start at the top, the home box at the bottom and a few objects in between, with spacing rules so nothing overlaps. Then it asks the solver questions, and a layout that fails any answer is thrown away.

  • At least one winning shot exists.
  • Aiming straight at the home box, within 3 degrees at any power, must not win. Otherwise the planets are decoration.
  • At least 3 winning shots must really use the level's special object. The check removes the object, for example a portal, a pipe or a ring, and replays the shot. If it still wins, the object was not needed.
  • The widest winning shot must reach the minimum window for that level.
  • Stars are placed on the paths of winning shots, and a flight that collects all of them must exist with a window of at least 40 percent of the minimum, and never under 1 degree.

Because stars are placed after the solver found the paths, a three-star flight is always possible.

The relaxation ladder

Some specs are too strict for random layouts. For each level the generator tries 400 random candidates and keeps the best of 3 that pass. If none pass, it moves one rung down a ladder of five. For late levels and bosses the rungs are:

  • Rung 0: the original spec.
  • Rung 1: minimum window x 0.8, allowed win rate x 1.5, maximum window x 1.6.
  • Rung 2: window x 0.65, win rate up to 25 percent, tap ability no longer required.
  • Rung 3: a plain single goal, window x 0.55, win rate up to 30 percent.
  • Rung 4: window x 0.45, win rate up to 35 percent, straight aim allowed, no minimum object count.

Earlier levels use a different ladder that drops objects instead: the last one first, then keeping two, then one. Each attempt uses its own seed (level id x 31 + rung), so the pack can be rebuilt exactly. In the final pack, 17 levels ended up with a single goal instead of their planned goal type.

The same ladder hid a bug. Worlds 1 and 2 have only two kinds of objects, but the boss level asked for at least three objects, so it could never be generated. The fix adds a bumper when the world has fewer than three kinds.

The winning window

A level with a solution is not yet fair. If the only winning angle is 0.2 degrees wide, no finger can hit it. So the solver measures the window: from a winning shot, at the same power, it steps the angle by 0.1 degrees to each side until the shot stops winning, up to 12 degrees. The total is the window.

The minimum window shrinks as the game goes on, from the first world to the sixth and later:

  • Levels 1 to 3: 7.0 degrees falling to 4.5.
  • Levels 4 to 7: 5.0 falling to 3.0.
  • Levels 9 to 12: 3.5 falling to 2.5.
  • Levels 14 to 17: 2.6 falling to 1.8.
  • Levels 18 and 19: 2.3 falling to 1.7. Boss level 20: 3.0.

Tap timing has a window too, measured in seconds. The minimum starts at 0.5 seconds in the first levels and ends at 0.25.

A hint that must be right

Every level stores a hint shot. My first version stored the shot the solver found on its 2 degree grid. That shot does win, but it can sit right at the edge of its window. A hint that wins only when the player copies it with perfect precision feels like a lie.

The audit test measured it. Before the fix, a hint still won within 0.75 degrees on both sides in 78 percent of levels, and within 1.6 degrees, the old margin of the green ring, in only 59 percent.

The fix is centering. The solver walks the angle in 0.25 degree steps to both edges of the window, takes the middle, rounds it to the 0.25 grid and checks that it still wins. For tap couriers it first centers the tap moment inside its own window. It costs about 100 simulations per level.

  • Within 0.75 degrees: 78 percent before, 100 percent after.
  • Within 1.6 degrees: 59 percent before, 88 percent after.
  • The generator now stores the centered shot, so the pack ships it from the start.

In the game, an open hint also has a magnet: when the finger gets within 3 degrees and one power step of the hint, the aim snaps to the solver's exact shot.

Auditing the whole pack

The pack has 617 levels: 12 worlds with 21 levels each (20 plus a bonus) make 252, and 365 daily levels make the rest. Generation runs in order, world 1 first, on many threads. Then the audit test replays every stored hint. On the final 564 single-shot levels it reports:

  • The hint still wins: 564 of 564. All three stars too: 564 of 564.
  • Within 0.75 degrees on both sides: 562 of 564.
  • Within 1.6 degrees on both sides: 517 of 564, which is 92 percent.
  • Window width: median 6.00 degrees, narrowest 1.25.
  • The 53 multi-shot levels are counted separately.

The 47 levels below the 1.6 degree line have a window narrower than that on at least one side of the hint. They are the tight ones, and they are the ones the magnet helps most.

If you want to feel the result, Slinglings: Cosmic Mail is on Google Play, and every level in it passed the checks above.

FAQ

How do you prove a physics puzzle level can be won?

Simulate it. A solver runs a grid of shots, 12 power levels and every few degrees of angle, with the same fixed-step physics the game uses, and keeps the shots that reach the goal.

Why does the physics need a fixed time step?

So that the same shot always gives the same path, on every phone and every frame rate. That makes the solver's answer valid in the real game.

What is a winning window?

The total angle range, at one power, in which a shot still wins. A wider window means the level is more forgiving for a finger.

Why center the hint in the window?

A hint on the edge of the window can lose after a tiny aiming error. In the middle it keeps winning when the player is a little off.

What happens if the generator cannot make a hard level?

It moves down a ladder of five rungs, loosening the limits step by step, and tries again with a new seed.


If you want to see this in action, Slinglings is free on Google Play and needs no account. More notes like this one live on my site.

Top comments (0)