Tuesday, August 29, 2017

Bouncy Ball Physics

Recently, I've watched some videos of physics simulations in other game engines. This got me interested in experimenting with physics and collisions of large number of objects in 3DWorld. 3DWorld supports full physics for spherical objects, including gravity, elastic and inelastic collisions, momentum, friction forces, air resistance, etc. All "large" (non-particle) spheres collide with all objects in the scene, including each other. Spheres are simpler to work with than other object shapes due to their rotational invariance and other factors. Each frame runs multiple physics simulation + collision detection steps on all active (moving) objects. The active object test significantly reduces computation time for common cases where most of the objects have stopped moving. In theory, the collision detection should prevent objects from intersecting each other and the scene.

I added a config file option that reduces weapon fire delay to 0, which allows me to throw a ball every frame. The maximum number of active balls is set to 20,000. At ~100 FPS, it only takes a few minutes to generate 20K balls. Here is a video of me throwing some 10K balls into the office building scene in realtime. See the balls fall and bounce in parabolic trajectories. Note that I've disabled player collision detection to prevent the ball recoil from pushing me around wildly.



Collision detection works for dynamic as well as static scene objects. I can push balls around on the ground using the player model or by pushing a movable object such as a crate into them. They'll go up and down inside an elevator, and fall when pushed over a ledge. I can place objects on a glass surface or in a glass box, shatter the glass, and watch them fall. Even though the balls are stopped, the breaking glass will re-activate their physics and put them back into motion.

Balls will also stack when added to a container so that they're non-overlapping, and will spread out to fill in the gaps just like real world spheres. I have a colored glass box on the third floor of the office building scene that I use for lighting experiments. It's fairly large, so it takes a while to fill with balls. I marked the glass as shatterable in the config file so that I can break it with weapon fire. Here is the video showing the glass plate and glass box experiments. Note that the lighting is static/baked into the scene because the glass box isn't supposed to be destroyable, which is why the lighting looks odd near the end of the video.



This is a high resolution screenshot showing the balls stacked in the glass box. See how closely together they're packed. The collision force of the glass on the balls keeps them inside the volume. It takes several seconds for the simulation to converge to this solution and the balls to stop moving. When the glass is broken, the balls will spill out to form a single layer on the floor.

1000 bouncy balls in a colored glass enclosure. Collision forces keep the balls from intersecting each other and the glass.

There are a number of different ways to apply forces to objects in 3DWorld. Weapon explosions produce force radiating outward from a central point, which affects dynamic objects. Here I use a rocket launcher (named "Seek and Destroy") to create explosions that push the balls around the scene. The following video shows the effect of these explosions on the physics state. This also shows off my neat explosion shockwave postprocessing shader effect. The shader distorts the image by simulating a spherical region of compressed gas with higher index of refraction forming a lens.



The rocket launcher is a crazy weapon to use with zero fire delay! I wouldn't recommend this in real gameplay, it's usually instant death. The small random rocket firing error causes two rockets to occasionally collide in mid-air - they're also dynamic objects. This results in an explosion and chain reaction that sometimes propagates back to the player along the chain of close proximity in-flight rockets, resulting in a high amount of area damage. Oops. Better stick to bullets, or some other non-explosive weapon.

Wednesday, August 16, 2017

Screenshot Gallery

I've been busy with other things and haven't worked on 3DWorld much lately. It's been a while since my last blog post. I've mostly been making minor bug fixes, feature additions, and improvements. I don't have enough to say about any particular topic to create a separate post. Instead of writing another wall of text, I'll just show some interesting screenshots. Most of these are new, but a few were captured some time ago and never posted anywhere.

First up, I've added ambient occlusion to tiled terrain scenes. Well, I've had ambient occlusion for a few years now, but this version works with procedurally generated terrain as well as terrain read from heightmaps. And it uses height values computed on the GPU, which is faster. The runtime penalty of ambient occlusion is much lower now, so why not use it? It certainly adds depth to the scene.

Terrain generated using domain warping and precomputed ambient occlusion to darken the deep ravines.

3DWorld gives the user control over temperature, vegetation, atmosphere, and many other physics parameters. These variables can also be set automatically by traveling to other planets through universe mode. Not all terrain is covered with green grass, trees, and water. Here are some other biomes.

Barren moon terrain with no clouds, atmosphere, or water, only small ice caps on the mountain peaks.

I even implemented a lava mode for the water shaders so that volcanic planets can be shown.

Hot lava/rocky planet with strong wind and dense, low clouds.

I've been experimenting with volumetric spotlights that cast glowing particle cones in a dark room. This is a simple and efficient technique that draws a cone using a custom shader rather than expensive GPU ray marching. I found this blog post that explains the technique. It looks okay, but I'm not sure where to use this effect in 3DWorld. There aren't a lot of spotlights in dark, smoky rooms. I'll keep it around for future use.

Experimental volumetric fog effect for spotlights in dark basements.

This is one of my favorite 3DWorld screenshots. It was taken a few months ago. I tried to get all of the different universe objects in the same screenshot: sun, stars, nebula, planet, rings, and asteroids. There's some nice contrast between the yellow tinted foreground and purple tinted background.

One of my favorite universe screenshot images, though it's kind of dark. Stars, nebula, asteroids, and planet with rings.

Finally, here is an older screenshot showing the moment after a cluster grenade has exploded. There are a huge number of particles here, somewhere around 10,000. Each one has physics including gravity and collision detection, and all of them receive dynamic light + shadows. The triangle particles also emit their own light. Smoke and fire are drawn in many depth sorted layers with low alpha to produce a volumetric effect, which is dynamically lit with sun shadows. The light of the fires is what tints the otherwise gray-black smoke a yellowish color.

Cluster grenade explosion screenshot with ~100 light emitting particles, ~10,000 colliding physics particles, smoke, and fire.

That's it for this post. I'll put up more content when I have something new to show.