You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let's look at something more natural and how it might be simulated. Say you have a real-time simulation that is out in the open, and you need to realistically model the effects of wind on your objects.
577
+
578
+
This is one of those cases where a little mathematics goes a long way. Real wind is not random. It has structure across multiple time scales. Meteorologists often think of wind as having four components:
579
+
580
+
. Mean wind (the forecast strength and direction)
581
+
. Long-period variation (30 to 120 second changes)
582
+
. Gusts (2 to 10 second bursts)
583
+
. Turbulence (small, rapid fluctuations)
584
+
585
+
Avoid the temptation of using a random number every frame, you can model each component separately.
586
+
587
+
Say the weather forecast is for a 10 mile an hour wind, from the NNW, gusting to 20 mph. Let's look at the four components.
588
+
589
+
. Mean wind : this should be converted to a vector (speed x direction), and direction is fixed at NNW (337.5 degrees).
590
+
. Long-period variation : Real wind slowly strengthens and weakens. A sine wave works surprisingly well, something like:
591
+
+
592
+
[source,cpp]
593
+
----
594
+
double long_period(double t)
595
+
{
596
+
return 1.5 *
597
+
sin(2.0 * pi * t / 90.0);
598
+
}
599
+
----
600
+
601
+
. Gusts : Gusting to 20 mph according to our forecast. We do not want sudden jumps, instead smoother pulses.
602
+
+
603
+
[source,cpp]
604
+
----
605
+
double gust(double t)
606
+
{
607
+
return exp(-x*x);
608
+
}
609
+
----
610
+
+
611
+
To be natural, each gust might last 4 to 8 seconds.
612
+
613
+
. Turbulence : We need to avoid obvious repetition, which we can manage with several sine waves, though the frequencies are unrelated:
614
+
+
615
+
[source,cpp]
616
+
----
617
+
double turbulence(double t)
618
+
{
619
+
return
620
+
0.4*sin(5.3*t)
621
+
+ 0.3*sin(8.9*t)
622
+
+ 0.2*sin(13.7*t);
623
+
}
624
+
----
625
+
626
+
Total wind speed will now look something like this:
Using the constants available in boost:math[], and the units from boost:units[], a first cut at a wind simulator might look like the following. Here we have three types of object - leaves, paper, tumbleweed - each with certain physical properties that determine how quickly the wind will move them.
Run this program and you will get a lot of numbers!
851
+
852
+
[source,text]
853
+
----
854
+
...
855
+
856
+
Time = 58.00 s Wind = 4.63 m s^-1
857
+
Leaf Position = 6073.07 m Velocity = 155.59 m s^-1
858
+
Paper Position = 3533.71 m Velocity = 70.33 m s^-1
859
+
Tumbleweed Position = 1911.14 m Velocity = 32.32 m s^-1
860
+
861
+
Time = 59.00 s Wind = 4.37 m s^-1
862
+
Leaf Position = 6229.48 m Velocity = 156.41 m s^-1
863
+
Paper Position = 3604.02 m Velocity = 70.31 m s^-1
864
+
Tumbleweed Position = 1943.50 m Velocity = 32.36 m s^-1
865
+
866
+
Time = 60.00 s Wind = 4.06 m s^-1
867
+
Leaf Position = 6386.41 m Velocity = 156.94 m s^-1
868
+
Paper Position = 3674.07 m Velocity = 70.04 m s^-1
869
+
Tumbleweed Position = 1975.71 m Velocity = 32.20 m s^-1
870
+
----
871
+
872
+
For extra realism, treat gusts like moving weather cells instead of global events. Imagine a gust as a "blob" of stronger wind drifting across the landscape. The gust has a position, radius, speed, and intensity. The result is that waves of motion travel naturally through your environment, much like you see when watching a real forest in a breeze or wind gusts crossing a lake. It looks dramatically more realistic than having every object receive the same gust at the same instant, and the underlying mathematics is still quite simple: each object samples the wind field at its own position.
873
+
874
+
== Next Steps
875
+
876
+
It is quite difficult to assess the "naturalness" of a physical process just by looking at numbers. To really assess how well our wind simulator works, we would need to have graphical output (not necessarily a full-featured 3D rendering) where we could visualize the different objects blowing across the terrain, answering the question "does it look right?".
877
+
573
878
It is good practice when designing a simulation of real-world activity to clearly define what is to be simulated and what is not. All simulations are simplifications to an extent, though they do tend to be large and challenging programs to write. A complex simulation might have several processes running on different threads. For a sample of multi-threading code, refer to xref:task-parallel-computation.adoc[].
0 commit comments