Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix weather particle physics. #19596

Merged
merged 1 commit into from Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tickFraction looks like something we might want to eventually calculate and expose at a central, publicly accessible place like Game, so we can reuse it for things like interpolation and don't need to do the math locally in every place that needs it.

Just thinking aloud, not a request for this particular PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are thinking of something like https://love2d.org/wiki/dt delta time?

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