Water droplet demo
19 Dec 2024Introduction
Visual effects like water droplets are mesmerizing, and they showcase how simple algorithms can produce complex, beautiful animations. In this article, I’ll walk you through creating a water droplet effect using VGA mode 13h.
We’ll rely on some of the code that we developed in the VGA routines from Watcom C article for VGA setup and utility functions, focusing on how to implement the effect itself.
The effect that we’re looking to produce should look something like this:
The Idea
The water droplet effect simulates circular ripples spreading out from random points on the screen. Here’s the high-level approach:
- Drops: Represent each drop with a structure containing its position, energy, and ripple generation.
- Drawing Ripples: Use trigonometry to create circular patterns for each ripple generation.
- Blur Effect: Smooth the buffer to simulate water’s fluid motion.
- Palette: Set a blue-themed palette to enhance the watery feel.
Setting the Water Palette
First, we set a blue-tinted gradient palette. Each color gradually transitions from dark blue to bright blue.
Representing Drops
Each drop is represented by a structure that tracks:
(x, y)
: The origin of the drop.e
: Energy, which fades with time.g
: Current ripple generation.
Creating and Advancing Drops
Drops are reset with random positions, maximum energy, and zero ripple generation:
Each frame, we reduce the drop’s energy and increment its generation. When energy is exhausted, the drop stops producing ripples:
Drawing Ripples
Ripples are drawn using polar coordinates. We calculate x
and y
offsets using cosine and sine functions for each
angle and scale by the current generation.
The colour that is rendered to the buffer is additive. We take the current colour at the pixel position, and add to it giving the droplets a sense of collision when they overlap.
Simulating Fluid Motion
A blur effect smooths the ripples, blending them into neighboring pixels for a more fluid appearance. This is done by averaging surrounding pixels.
Main Loop
The main loop handles:
- Adding new drops randomly.
- Advancing and drawing existing drops.
- Applying the blur effect.
- Rendering the buffer to the VGA screen.
Conclusion
This water droplet effect combines simple algorithms with creative use of VGA mode 13h to create a visually stunning effect. By leveraging circular ripples, energy fading, and a blur filter, we replicate the mesmerizing motion of water.
You can find the complete code on GitHub as a gist.
Try it out, tweak the parameters, and share your own effects! There’s a lot of joy in creating beautiful visuals with minimal resources.