dropping2.gif
 

A simple game and sandbox environment based on simulating the collision responses of balls

Link to the code on GitHub.

The program is based on OneLoneCoder's olc::PixelGameEngine as well as his videos on collision handling of balls with some deviations.

The goal is simple - the player tries to maximize the score by hitting (and destroying) as many obstacles as possible each turn by shooting balls along a chosen trajectory. After a turn, new obstacles spawn from below and the entire grid moves upwards; the game is over when any obstacle reaches the top (taking inspiration from Tetris).

If the player presses “s”, a background simulation indicates the predicted score, which hints at what I wanted to implement next: An AI to perfect the way the game is played. To that end, the code is ripe for an overhaul and several improvements. For one, the simulation speed should be enhanced, and the physics-system needs to be copied and modified - this is a perfect candidate for class encapsulation and operator overloading.

The sandbox mode is illustrated below:

dropping_sandbox.gif

Sandbox Mode

One issue I had to work around was the predictability: As you can see on the left-hand side, each shot ball should follow a perfectly predictable trajectory. The problem I encountered was that, when the physics engine allows for objects to overlap from one frame to the next, the collision response can vary ever so slightly - and these errors compound. To mitigate this issue, I decoupled the resolution of the physics engine from the current framerate, meaning that updates to the system’s properties are made at fixed timesteps. This makes the trajectory uniform and perfectly predictable/replicable, but brings with it some issues on its own right (namely, the objects will no longer move in perfect realtime - the physics engine “overshoots” the actual elapsed time since the last frame). A perfect solution would compute the exact time of collision, which is a feature yet to implement.

Note that conservation of momentum is not satisfied here because there are immovable objects - those for which there is no change in momentum after a collision. In sandbox mode, some physical properties like gravity, drag and elasticity can be altered.

See the next picture:

dropping_sandbox_gravity.gif

Disturbing a system without gravity..

..and then reintroducing gravitational force

Zurück
Zurück

CppSweeper (C++)

Weiter
Weiter

PyChess (Python)