What and Why
One of the things we see Virtalis software commonly used for is training, the ability to place the latest CAD model in a virtual environment means you can always be training with the latest designs, or put future designs in the hands of users to determine any issues.
Alright, sales pitch over! Today I’m going to take you through a toy example to show how you can use the different features of Visionary Render to simulate real-world effects, which is a vital aspect of some training scenarios.
Let’s say we’re training someone on the basic technique for using a fire extinguisher. The scene I’ve created is very simple, we’ve got a fire blazing away, and an extinguisher pointed at it ready to put it out. The job of the trainee is to get the extinguisher pointed at the right bit of the fire to achieve the maximum extinguishing effect.
A key takeaway from this for me comes from the way the fire and the extinguisher are represented visually, and that’s to put simulation and development effort into what matters. As satisfying as it would be to have flames licking realistically around the edges of the fire, it doesn’t particularly matter to someone learning basic technique. I’m no fireman, so for the sake of argument I’ll outline a couple of things that I imagine are important for our simple training example which shaped my decisions when building this scene:
- The maximum extinguishing effect is achieved when the extinguisher is pointed at the base of the fire
- The visual representation of the extinguisher should correspond to its effectiveness i.e. if the extinguisher visuals are overlapping the fire visuals then the fire should be being extinguished
VisRen’s built-in particle systems provide all I need to satisfy these conditions, and I’m going to use one each for the extinguisher and the fire itself. I can use the properties of the fire extinguisher particle system (shape, particle speed and lifetime) to define the limits of the fire extinguisher visuals, and I can use the emit rate property of the properties of the fire particle system to reduce the size of the fire visual as the extinguisher takes effect.
For this quick example I’ve used some free resources off the internet for the extinguisher model and the flame texture, and the fire extinguisher particles are literally just white squares, but you could make this look a lot more realistic by improving these.
The basis of the fire simulation is the emit rate property of the fire particle system, I want this to update all the time, so I’ll use a TimeStep script to update it. I won’t go into full details of the script, just give a brief description, but it’s attached to this blog if you’re interested.
This script is going to calculate an extinguishing effect which lowers the emit rate, the mechanism for this is discussed a bit more below, but it’s important to note that it’s a two-step process for the sake of efficiency, the effect is only calculated if a test is met that indicating that there should be one at all. I’m also going to add a growth effect which will mean if the extinguisher isn’t being effective the fire will slowly grow back to full strength.
So the particle systems are going to represent the visual effects, and we’re going to update the fire particle rate with a TimeStep script, but how to calculate the actual extinguishing effect to use? There’s a few ways, probably including many that didn’t occur to me, but the main two I considered were using colliders or doing the pointing maths manually in a script. In the end I went with the pointing maths approach, but it’s worth discussing both and explaining how they would each work.
First, the collider approach. The basic idea with this is to represent the extinguisher effect and the fire as objects in VisRen’s collision system using meshes / bounding boxes etc. The collision system would then send events out whenever the two things intersect, and I can use the event scripts to set variables which control the extinguishing effect. I could even have multiple levels to represent the heart of the fire and a have a more intense extinguishing effect nearer the nozzle.
The advantages of this approach is that it’ll work with pretty much any geometry, so if we want to have a complex geometry for either the fire or the extinguisher we can do so. Plus the collision system is running in VisRen anyway, and it should be nice and robust. The downsides are that there’s quite a few scripts to manage, and it’s difficult to make the effect vary continuously.
Pointing Maths Approach
The pointing maths approach is simply to take the position and rotation information of the fire and the extinguisher, and calculate the extinguishing effect with a script based on whether the extinguisher is pointing at the fire and how far away it is. This is a perfect opportunity to use VisRen’s built in vector operations such as vrDot (which calculates dot product) and vrTransformVecByWorldRotation (a wordy one, but very useful, as it allows us to rotate any vector to be relative to an assembly’s rotation).
The maths I’ve used isn’t too complex, I can calculate the vector along which the extinguisher is acting (the effect vector) using vrTransformVecByWorldRotation, and vector subtraction gives me the vector between the positions of the fire and the extinguisher nozzle (I call this the look vector - the vector you look along from the extinguisher position to see the fire). The dot product of these will tell me the angle between them (the off angle) and thus if the extinguisher is pointed sufficiently closely at the fire. Then if it is, I can simply scale the effectiveness according to the length of the vector between the two positions (the off distance).
The advantage of this approach is that it very naturally allows me to scale the effectiveness of the extinguisher based on how well it’s pointed at the fire and how close it is. The disadvantage is that it’s easy to make mistakes with this kind of calculation (or at least, it is for me) and I’m effectively modelling the fire as a single point, whereas in reality fires can have large regions of intensity at their centres.
So that’s it! This scene is quite simple to put together yourself and I’d recommend giving it a go, especially if you’ve never used some of VisRen’s more dynamic features before.
Hopefully this was an interesting example, what do you make of my approach? I’m sure there’s other ways this could be done, or improvements that could be made, and it would be great to hear them.