Skip to content

Commit

Permalink
Fix weather particle physics.
Browse files Browse the repository at this point in the history
The trait documentation specified that the speed
and offset values are px/tick, but they have actually
always been treated as px/render.

Fix the update logic and rescale the map definitions
to account for the fixed behaviour.
  • Loading branch information
pchote authored and reaperrr committed Aug 20, 2021
1 parent d2257f9 commit 864cc4b
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 31 deletions.
19 changes: 13 additions & 6 deletions OpenRA.Mods.Common/Traits/World/WeatherOverlay.cs
Expand Up @@ -27,10 +27,10 @@ public class WeatherOverlayInfo : TraitInfo, ILobbyCustomRulesIgnore
public readonly bool ChangingWindLevel = true;

[Desc("The levels of wind intensity (particles x-axis movement in px/tick).")]
public readonly int[] WindLevels = { -5, -3, -2, 0, 2, 3, 5 };
public readonly int[] WindLevels = { -12, -7, -5, 0, 5, 7, 12 };

[Desc("Works only if ChangingWindLevel is enabled. Min. and max. ticks needed to change the WindLevel.")]
public readonly int[] WindTick = { 150, 750 };
public readonly int[] WindTick = { 150, 550 };

[Desc("Hard or soft fading between the WindLevels.")]
public readonly bool InstantWindChanges = false;
Expand All @@ -45,13 +45,13 @@ public class WeatherOverlayInfo : TraitInfo, ILobbyCustomRulesIgnore
public readonly int[] ScatterDirection = { -1, 1 };

[Desc("Min. and max. speed at which particles fall in px/tick.")]
public readonly float[] Gravity = { 1.00f, 2.00f };
public readonly float[] Gravity = { 2.5f, 5f };

[Desc("The current offset value for the swing movement. SwingOffset min. and max. value in px/tick.")]
public readonly float[] SwingOffset = { 1.0f, 1.5f };
public readonly float[] SwingOffset = { 2.5f, 3.5f };

[Desc("The value that particles swing to the side each update. SwingSpeed min. and max. value in px/tick.")]
public readonly float[] SwingSpeed = { 0.001f, 0.025f };
public readonly float[] SwingSpeed = { 0.0025f, 0.06f };

[Desc("The value range that can be swung to the left or right. SwingAmplitude min. and max. value in px/tick.")]
public readonly float[] SwingAmplitude = { 1.0f, 1.5f };
Expand Down Expand Up @@ -140,6 +140,7 @@ public Particle(in Particle source, float2 pos, int swingDirection, float swingO
long windUpdateCountdown;
Particle[] particles;
Size viewportSize;
long lastRender;

public WeatherOverlay(World world, WeatherOverlayInfo info)
{
Expand Down Expand Up @@ -197,6 +198,12 @@ void IRenderAboveWorld.RenderAboveWorld(Actor self, WorldRenderer wr)
var viewport = new Rectangle(center - new int2(viewportSize) / 2, viewportSize);
var wcr = Game.Renderer.WorldRgbaColorRenderer;

// SwingSpeed is defined in px/tick so we must account for the fraction of a tick that elapsed since the last render.
// The scale is capped at 1 tick to avoid unexpected behaviour at game start, if RunTime overflows, or if the game stalls.
var runtime = Game.RunTime;
var tickFraction = Math.Min((runtime - lastRender) * 1f / world.Timestep, 1);
lastRender = runtime;

for (var i = 0; i < particles.Length; i++)
{
// Simulate wind and gravity effects on the particle
Expand All @@ -208,7 +215,7 @@ void IRenderAboveWorld.RenderAboveWorld(Actor self, WorldRenderer wr)
swingDirection *= -1;

var swingOffset = p.SwingOffset + p.SwingDirection * p.SwingSpeed;
var pos = p.Pos + new float2(p.DirectionScatterX + p.SwingOffset + windStrength, p.Gravity);
var pos = p.Pos + tickFraction * new float2(p.DirectionScatterX + p.SwingOffset + windStrength, p.Gravity);
particles[i] = p = new Particle(p, pos, swingDirection, swingOffset);
}

Expand Down
4 changes: 1 addition & 3 deletions mods/cnc/maps/gdi06/rules.yaml
Expand Up @@ -6,12 +6,10 @@ World:
StartingMusic: rain
WeatherOverlay:
ChangingWindLevel: true
WindLevels: -5, -3, -2, 0, 2, 3, 5
WindTick: 150, 550
InstantWindChanges: false
UseSquares: false
ScatterDirection: 0, 0
Gravity: 8.00, 12.00
Gravity: 15, 25
SwingOffset: 0, 0
SwingSpeed: 0, 0
SwingAmplitude: 0, 0
Expand Down
6 changes: 0 additions & 6 deletions mods/ra/maps/a-nuclear-winter/rules.yaml
@@ -1,16 +1,10 @@
World:
WeatherOverlay:
ChangingWindLevel: true
WindLevels: -5, -3, -2, 0, 2, 3, 5, 6
WindTick: 150, 550
InstantWindChanges: false
UseSquares: true
ParticleSize: 2, 3
ScatterDirection: -1, 1
Gravity: 1.00, 2.00
SwingOffset: 1.0, 1.5
SwingSpeed: 0.001, 0.025
SwingAmplitude: 1.0, 1.5
ParticleColors: ECECEC, E4E4E4, D0D0D0, BCBCBC
LineTailAlphaValue: 0
GlobalLightingPaletteEffect:
Expand Down
2 changes: 1 addition & 1 deletion mods/ra/maps/fort-lonestar/rules.yaml
Expand Up @@ -10,7 +10,7 @@ World:
WindTick: 150, 550
UseSquares: false
ScatterDirection: 0, 0
Gravity: 8.00, 12.00
Gravity: 15, 25
SwingOffset: 0, 0
SwingSpeed: 0, 0
SwingAmplitude: 0, 0
Expand Down
7 changes: 0 additions & 7 deletions mods/ra/maps/shattered-mountain/rules.yaml
@@ -1,17 +1,10 @@
World:
WeatherOverlay:
ChangingWindLevel: true
WindLevels: -5, -3, -2, 0, 2, 3, 5, 6
WindTick: 150, 550
InstantWindChanges: false
UseSquares: true
ParticleSize: 2, 3
ParticleDensityFactor: 8
ScatterDirection: -1, 1
Gravity: 1.00, 2.00
SwingOffset: 1.0, 1.5
SwingSpeed: 0.001, 0.025
SwingAmplitude: 1.0, 1.5
ParticleColors: ECECEC, E4E4E4, D0D0D0, BCBCBC
LineTailAlphaValue: 0
GlobalLightingPaletteEffect:
Expand Down
8 changes: 0 additions & 8 deletions mods/ra/maps/snow-town/rules.yaml
@@ -1,16 +1,8 @@
World:
WeatherOverlay:
ChangingWindLevel: true
WindLevels: -5, -3, -2, 0, 2, 3, 5
WindTick: 150, 550
InstantWindChanges: false
UseSquares: true
ParticleSize: 1, 3
ScatterDirection: -1, 1
Gravity: 1.00, 2.00
SwingOffset: 1.0, 1.5
SwingSpeed: 0.001, 0.025
SwingAmplitude: 1.0, 1.5
ParticleColors: ECECEC, E4E4E4, D0D0D0, BCBCBC
LineTailAlphaValue: 0
GlobalLightingPaletteEffect:
Expand Down

0 comments on commit 864cc4b

Please sign in to comment.