[Mod] Raycasted Explosions API/library [explosions]

ryvnf
Member
Posts: 56
Joined: Wed Aug 15, 2018 21:10
GitHub: ryvnf
IRC: ryvnf
In-game: ryvnf

[Mod] Raycasted Explosions API/library [explosions]

by ryvnf » Post

Raycasted Explosions API

Ray-tracing can be used to simulate explosions in voxel games like Minetest. The biggest advantage of using it is the ability to make different materials have different blast resistance. I have been working on an API to create ray-traced explosions in Minetest. This mod is the implementation of that API. The idea is that this mod can be added as an dependency for other mods, which want to simulate explosions.

Video

Here is a short video of the mod in action. The video is of another mod Explosions TNT which uses the API to create TNT:s of different strengths (TNT, C4, NUKE). The explosions mod itself does not come with any node or item definitions, only the API, to keep it small.

Video of the mod

Git repo and ZIP-download

Source-code is hosted at the Gitlab repository.

ZIP-file can be downloaded directly.

Dependencies

This mod has no dependencies.

Documentation

Refer to the README at the repository for how to use the API.

Features
  • Can generate explosions of arbitrary strengths (from tiny explosions to the limit of what your computer can handle)
  • Supports nodes of varying blast resistance (a wall of diamond stronger than a wall of wood)
  • Supports node callbacks to change the behavior on blasts
  • Implements entity damage and knockback
  • Implements blast resistance from existing node groups
  • Stronger nodes can get blown away after blasts
  • Can be used to generate shaped explosions (like an explosion going only in one direction)
  • Uses uniformly-distributed numbers to make explosions unpredictable and "stackable" (8 times TNT will approx. create 8 times the environment destruction)
TODO

The mod isn't 100% perfect. Here is a list of things I would like to get improved in the future. I don't think I have time to improve these points in the near future, and the mod is good enough to release now anyways. But if anyone wants to help, feel free to contribute on Gitlab, or create a fork.
  • Implement entity callbacks. The way I tried (specifying callback when registering entity didn't work)
  • See if it is possible to implement player knockback in some way
  • Make the velocity of flying nodes after large explosions less random
  • Add default node blast resistances for other common mods (like technic for example)
License

The mod is released under LGPL version 3.
Last edited by ryvnf on Tue Dec 04, 2018 22:45, edited 5 times in total.

User avatar
ChimneySwift
Member
Posts: 320
Joined: Fri Sep 22, 2017 06:46
GitHub: ChimneySwift
IRC: ChimneySwift
In-game: ChimneySwift
Location: 127.0.0.1

Re: [Mod] Ray-traced explosion library/API [explosions]

by ChimneySwift » Post

Woah!! That video looked really awesome! Much more realistic than the current TNT. +1
A spoon is basically a tiny bowl with a stick on it

User avatar
TenPlus1
Member
Posts: 3715
Joined: Mon Jul 29, 2013 13:38
In-game: TenPlus1
Contact:

Re: [Mod] Ray-traced explosion library/API [explosions]

by TenPlus1 » Post

Sweet :)

gpcf
Member
Posts: 382
Joined: Fri May 27, 2016 10:48
GitHub: gpcf
In-game: gabriel

Re: [Mod] Ray-traced explosion library/API [explosions]

by gpcf » Post

Is the TNT test mod available anywhere, for quick testing of this library?

ryvnf
Member
Posts: 56
Joined: Wed Aug 15, 2018 21:10
GitHub: ryvnf
IRC: ryvnf
In-game: ryvnf

Re: [Mod] Ray-traced explosion library/API [explosions]

by ryvnf » Post

gpcf wrote:Is the TNT test mod available anywhere, for quick testing of this library?
I haven't made it public yet. I have a few more things I want to add to it.

I want it to be both a mod showcasing the API with different TNT:s of different strengths and shaped charges. It should also serve as a reference mod for people wanting to use the API. So I will make it public when I have an implementation that uses all parts of the API.

User avatar
rubenwardy
Moderator
Posts: 6972
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: [Mod] Ray-traced explosion library/API [explosions]

by rubenwardy » Post

This is very impressive looking! How well does it perform on a bigger explosion? Are you using the engine's raycast API?
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

ryvnf
Member
Posts: 56
Joined: Wed Aug 15, 2018 21:10
GitHub: ryvnf
IRC: ryvnf
In-game: ryvnf

Re: [Mod] Ray-traced explosion library/API [explosions]

by ryvnf » Post

rubenwardy wrote:This is very impressive looking! How well does it perform on a bigger explosion? Are you using the engine's raycast API?
Thanks!

It does not use any raycast API. Everything is written in Lua. Is there an API available implemented engine-side usable by mods? That might be a way to increase the performance.

The mod is optimized in such a way that it will trace less rays for smaller explosions, but more rays for larger explosions. The amount of traced rays increases with r^2 (the surface area of a sphere). This is because tracing less rays would create artifacts (like floating nodes, or weird patterns) in the environment. Each ray is traced up to the explosions radius, r. So the total time complexity for tracing explosions or radius r is O(r^3).

While this sounds very bad. An explosion of strength 1600 which has a radius of 16 and 2562 rays will be traced immediately. The delay is barely noticeable. And generally, I don't think explosions larger than that are common, other than some "lol NUKE TNT lets blow everything up" mod.

The default mod doesn't support explosions with strengths above 1600 (it will trace the explosions, but expect them to create artifacts in the environment). This is to keep the base mod relatively lightweight. Supporting larger explosions would require storing more subdivisions of an ico-sphere in memory, which makes the memory of the mod go up several megabytes. I plan on making some kind of extension which extends the mod with more subdivisions, allowing for larger explosions.

ryvnf
Member
Posts: 56
Joined: Wed Aug 15, 2018 21:10
GitHub: ryvnf
IRC: ryvnf
In-game: ryvnf

Re: [Mod] Ray-traced explosion library/API [explosions]

by ryvnf » Post

rubenwardy wrote:This is very impressive looking! How well does it perform on a bigger explosion? Are you using the engine's raycast API?
I read about it and I assume you are talking about the `Raycast` or `minetest.raycast` function.

I have tested the API function, and it has a quite major problem for being usable in this mod. And that is that the ray will never cross diagonals, this causes the amount of nodes a ray passes to be heavily dependent on the direction of the ray. By looking in a casting a ray 32 nodes in a cardinal direction, the ray can pass as few as 32 blocks. But as you cast the ray in a direction which is not cardinal, it will pass more nodes. I could personally get it up to 55 blocks (almost twice as much as the perfect case).

If each crossed nodes have a blast resistance, the ray going 55 nodes will die much faster than the ray going 32 nodes. Even though they are casted with the same distance. This wouldn't be a problem if the API had a way to tell how much distance a ray crosses through a node, which unfortunately it appears to lack.

So for now, the API will stick to having raycasting coded in Lua.

CrazyDave
New member
Posts: 1
Joined: Thu Aug 02, 2018 16:00
GitHub: DBHeise
In-game: CrazyDave

Re: [Mod] Ray-traced explosion library/API [explosions]

by CrazyDave » Post

Awesome! Great idea!

From the video it looks like the blocks are just moved, could it be possible to have the blocks not just moved but made ready to pickup/vacuum?

ryvnf
Member
Posts: 56
Joined: Wed Aug 15, 2018 21:10
GitHub: ryvnf
IRC: ryvnf
In-game: ryvnf

Re: [Mod] Ray-traced explosion library/API [explosions]

by ryvnf » Post

CrazyDave wrote:Awesome! Great idea!

From the video it looks like the blocks are just moved, could it be possible to have the blocks not just moved but made ready to pickup/vacuum?
Yes!

I think I should explain what happens. The mod defines two types of node callbacks.

One callback `on_blast_break' gets called when a ray penetrates a node, the other callback `on_blast_shock' gets called when a ray hits a node but does not have enough strength to go through it.

The default behavior for `on_blast_break' is to remove the node. The default behavior for `on_blast_shock' is to make the node a falling node with a velocity proportional to the remaining ray strength.

This allows people to configure what happens on a per node basis. The mod currently does not spawn any drops from explosions. That is something that I still haven't decided on. I think maybe introducing drops as a default behaivor in `on_blast_break' might be a good idea. Problem is that too many drops might make large explosions quite heavy on the server.

But the node callbacks does allow you to specify a callback function which removes the node and makes it yield a drop. Which could be used to create drop from specific nodes (like minerals).

ryvnf
Member
Posts: 56
Joined: Wed Aug 15, 2018 21:10
GitHub: ryvnf
IRC: ryvnf
In-game: ryvnf

Re: [Mod] Ray-traced explosion library/API [explosions]

by ryvnf » Post

I am currently working on entity damage and knockback. This requires doubling the raycast radius (if one wants damage to be twice the radius), which makes everything around 16x slower, if one wants the damage to be consistent around the radius of the explosion. (otherwise there will be special "holes" were rays miss, if player or entity stands in the "hole", it will not take any damage from the explosion).

As I would like everything to be as efficient as possible have worked to optimize the raycasting code.

I have managed to make it take around 31 % of the time it used to trace an explosion.

This was primary achieved by removing as much table creation (primary for vectors) as possible, and removing function calls. This comes with a big cost of readability, but I think it is worth it. The big bottleneck currently is the updating of entities (the flying nodes of the explosion), where there as far as I know is no way to create an entity, or updating its velocity without creating vectors. So there probably isn't much that can be done there.

One more thing I want to fix is that I notice the default undirected explosion shape will sometimes create artifacts if the explosion is detonated above ground. I think this might be solvable by changing switching the shape (currently the mod uses icospheres of various subdivisions exported from Blender).

So the mod isn't quite ready for public use yet. But it is getting there. I won't have much time to work on it the coming two weeks, but after that I hope to finish it and release it.

ryvnf
Member
Posts: 56
Joined: Wed Aug 15, 2018 21:10
GitHub: ryvnf
IRC: ryvnf
In-game: ryvnf

Re: [Mod] Ray-traced explosion library/API [explosions]

by ryvnf » Post

I have now decided to release the mod :D The release version is in the gitlab-repo.

I have worked quite a lot on it the last month. Most of it has been experimentation with various ways to update each ray depending on the blast resistance. The hard thing has been to make the explosions somewhat realistic while still making a blast containing multiple TNT damage the environment proportional to the amount of TNT in the blast. In the end I found a solution to this, using uniformly random factors assigned to each node when calculating their blast resistance.

The API has also been changed. It now only contains one function `explosions.explode` which combine the previous `explosions.explode` and `explosions.explode_shaped`. Everything is well documented. The mod also now computes default spherical explosion shapes at run-time, allowing for explosions of any strength.

I will first post record a nice video of my test mod, then I will create a post in the released mods section. And release the TNT mod used to test the mod. So stay tuned up! Should come up in a week.

I'm very happy I finally managed to get the explosions working the way I wanted. :)
Last edited by ryvnf on Sun Sep 23, 2018 21:37, edited 1 time in total.

User avatar
texmex
Member
Posts: 1753
Joined: Mon Jul 11, 2016 21:08
GitHub: tacotexmex
In-game: tacotexmex

Re: [Mod] Ray-traced explosion library/API [explosions]

by texmex » Post

Congratulations on finalizing the mod! I’m looking forward to trying it.

Byakuren
Member
Posts: 818
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri
In-game: Raymoo + Clownpiece

Re: [Mod] Ray-traced explosion library/API [explosions]

by Byakuren » Post

For entity damage couldn't you calculate a non-sampled approximation based on entity size and distance from the explosion? Then you wouldn:'t have to add more rays, most of which won't be hitting any entities.
Every time a mod API is left undocumented, a koala dies.

User avatar
Hybrid Dog
Member
Posts: 2828
Joined: Thu Nov 01, 2012 12:46
GitHub: HybridDog

Re: [Mod] Ray-traced explosion library/API [explosions]

by Hybrid Dog » Post

This looks interesting.
For your interest, I currently use perlin noise to calculate explosion holes, here's an example:
Image
There's the code: https://github.com/HybridDog/vector_ext ... t.lua#L441
Attachments
bl.jpg
bl.jpg (207.37 KiB) Viewed 2369 times

‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪

ryvnf
Member
Posts: 56
Joined: Wed Aug 15, 2018 21:10
GitHub: ryvnf
IRC: ryvnf
In-game: ryvnf

Re: [Mod] Ray-traced explosion library/API [explosions]

by ryvnf » Post

Hello! I'm sorry I haven't made a TNT test mod public, to show you what the API can do.

I just happened to get a lot on my hands right now. I promise I will release a showcase TNT-mod for the API soon. The release version is up in case anyone wants to test it by creating their own mod. Otherwise you'll have to wait a bit more, I'm sorry.

ryvnf
Member
Posts: 56
Joined: Wed Aug 15, 2018 21:10
GitHub: ryvnf
IRC: ryvnf
In-game: ryvnf

Re: [Mod] Ray-traced explosion library/API [explosions]

by ryvnf » Post

Byakuren wrote:For entity damage couldn't you calculate a non-sampled approximation based on entity size and distance from the explosion? Then you wouldn:'t have to add more rays, most of which won't be hitting any entities.
Entity damage is proportional to the strength of the ray which hits the entity. The strength of the ray decreases according to this formula>

base_strength / distance ^ 2

Which nicely approximates the area of which the entity is affected. Entity damage is one of the things I noticed is a bit buggy (I can't seem to get the callback function working), which has delayed the release of the mod. The callback function is supposed to make it possible to change how much damage entity takes, or add other behaviors.
Last edited by ryvnf on Tue Oct 09, 2018 19:26, edited 1 time in total.

ryvnf
Member
Posts: 56
Joined: Wed Aug 15, 2018 21:10
GitHub: ryvnf
IRC: ryvnf
In-game: ryvnf

Re: [Mod] Ray-traced explosion library/API [explosions]

by ryvnf » Post

Hybrid Dog wrote:This looks interesting.
For your interest, I currently use perlin noise to calculate explosion holes, here's an example:
Image
There's the code: https://github.com/HybridDog/vector_ext ... t.lua#L441
Interesting. I can see that it leads to more random and interesting explosion-crates than using the "remove everything in radius approach".

Byakuren
Member
Posts: 818
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri
In-game: Raymoo + Clownpiece

Re: [Mod] Ray-traced explosion library/API [explosions]

by Byakuren » Post

What if you used perlin noise to assign varying strengths to each direction? Probably 3D noise since that may be the easiest way to make the strength continuous.
Every time a mod API is left undocumented, a koala dies.

User avatar
Hybrid Dog
Member
Posts: 2828
Joined: Thu Nov 01, 2012 12:46
GitHub: HybridDog

Re: [Mod] Ray-traced explosion library/API [explosions]

by Hybrid Dog » Post

I think there are algorithms which suit better for calculating strength vectors, maybe a surface of a sphere can be calculated while adding random offsets (the noise), i.e. combining diamond-square and Catmull–Clark subdivision surface.

‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪

ryvnf
Member
Posts: 56
Joined: Wed Aug 15, 2018 21:10
GitHub: ryvnf
IRC: ryvnf
In-game: ryvnf

Re: [Mod] Ray-traced explosion library/API [explosions]

by ryvnf » Post

As I have implemented the algorithm, it is very hard to add any kind of randomness to the ray strength.

This is because multiple rays will go through a single node, and for each ray it will check if it can penetrate the node. This means that off all rays, it is the ray with the most strength that will affect the node, which results in that the effective strength of the explosion approaches the maximum random value as the number of rays increases.

There are other ways to solve this which allow for random ray strengths. Like calculating the accumulated strength of multiple rays and use that to determine if the explosion affects the node or not. Or having checking the ray which is closest to the explosion. Both these methods does require more processing though, which is why I did it the simple way.

The way I added randomness was to assign each node a uniformly distributed random value between 0-2. This blast resistance gets multiplied by this value when making the comparison. This results in more "rough" explosions (which might not be desirable), but has the advantage that the explosion destruction will increase proportional to the number of TNT (even when they are stacked on top of each other). This what the mod currently uses.

ryvnf
Member
Posts: 56
Joined: Wed Aug 15, 2018 21:10
GitHub: ryvnf
IRC: ryvnf
In-game: ryvnf

Re: [Mod] Ray-traced explosion library/API [explosions]

by ryvnf » Post

I have added the TNT-mod to test this API.

It implements 3 different TNT types: TNT, C4 and NUKE. Which are TNTs of different blast strengths.

https://gitlab.com/ryvnf/explosions_tnt

I will make a mod release about it and this API later when I have time. Probably including some video showcasing the mod.

User avatar
LMD
Member
Posts: 1386
Joined: Sat Apr 08, 2017 08:16
GitHub: appgurueu
IRC: appguru[eu]
In-game: LMD
Location: Germany
Contact:

Re: [Mod] Raycasted Explosions API/library [explosions]

by LMD » Post

Consider calculating blast resistances when possible, based on node groups like choppy etc.
Maybe take a look at KGM's tnta mod for that.
Great work on the API !
My stuff: Projects - Mods - Website

User avatar
Wuzzy
Member
Posts: 4786
Joined: Mon Sep 24, 2012 15:01
GitHub: Wuzzy2
IRC: Wuzzy
In-game: Wuzzy
Contact:

Re: [Mod] Raycasted Explosions API/library [explosions]

by Wuzzy » Post

This sure sounds amazing! I have no idea how it would work for reals, but I should write a review when I have tested it.
From the first look, this looks like someone has done solid work, since the thread is actually readable and there is a solid API documentation on release day. This alone makes this look very promising.

There is a good chance this mod will find its way into MineClone 2.

Is this methodology somehow copied or based on how Minecraft makes explosions? If not, are there any serious differences (if you are familiar with Minecraft)?

Does this mod have any dependencies? Because this is important if I want to add it into MCL2 for obvious reasons. ;-)

ryvnf
Member
Posts: 56
Joined: Wed Aug 15, 2018 21:10
GitHub: ryvnf
IRC: ryvnf
In-game: ryvnf

Re: [Mod] Raycasted Explosions API/library [explosions]

by ryvnf » Post

When designing this, I experimented and came up with a new algorithm.

I wanted the implementation to have 3 properties:

(1) Efficient - should work for large explosions, even on older hardware
(2) Stackable - the total environment destruction should be proportional to the number of TNT's in the explosion
(3) Create "nice" explosions - the destruction should look good

There is a trade-off between (1) and (3). If one wants "nicer" explosions, you can simply sample more positions along each ray, but that costs performance. Currently it samples only 1 node distance per step, while Minecraft samples each 0.3 node distance per step.

---

The Minecraft algorithm (as described on The Minecraft Wiki is the following:

1. A cube around the explosion is divided into a 16×16×16 grid, and rays are created from the center to each outer point of this grid, meaning that there are a total of 1352 rays.
2. Each ray is given an intensity, calculated as (0.7 + [a random value from 0 to 0.6]) × [power].
3. For every 0.3 blocks along the ray, the intensity of the ray decays/is attenuated by 0.3×0.75 (0.225), and the block it passes through absorbs/reduces it by ([blast resistance/5]+0.3)×0.3.
4. The ray destroys all blocks that could not end the ray at any checkpoint.

While the algorithm used here is more like:

1. A sphere around the explosion with radius `cube_root(strength)' and rays are created by the nodes which make up the surface area of the sphere (this gets saved and reused for performance)
2. Each ray is given the specified `strength' (no randomization here)
3. Rays advance 1 node distance per step in their direction, each node a ray crosses gets given a random integer in range 0-2 (shared between all rays)
4. This number gets multiplied by blast resistance and compared to ray strength
5. If ray strength less than the number the ray dies, and the node gets added to blocks "shocked", otherwise the node gets added to the nodes "breaked"
6. The blast resistance times distance squared of the node ray passed through get subtracted from the ray strength

Step 1 differs only because it is more efficient doing it this way. The other steps has an impact on the result, one is that this mod makes TNT's stack better, compared to Minecraft.

---

This internal algorithm can be revised and changed in the future, to produce "nicer explosions". This is the beauty of it being an API. If someone happens to come up with a better algorithm (according to the properties listed above), I would be glad to merge it.

So the short answer is that this is different from Minecraft in the implementation, but otherwise quite similar. It definitely more like Minecraft than not using ray-casting at all.

I think changing the implementation to be more like Minecraft is possible, if someone is willing to do it. That might be used to create a fork which could be used in Mineclone (if the goal is to be as similar as possible to Minecraft). If 100% compatibility isn't priority it should probably be possible to use in its current state (maybe after disabling some features like the flying nodes). I would be very glad if this mod gets put into a game!

It does not have any dependencies.
Last edited by ryvnf on Mon Oct 29, 2018 20:05, edited 3 times in total.

Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests