diff --git a/Main.cs b/Main.cs index f7607ff..f30e01b 100644 --- a/Main.cs +++ b/Main.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using Godot; public class Main : Node2D @@ -13,7 +14,7 @@ public class Main : Node2D { GD.Randomize(); InitializeParticleTypes(10); - InitializeParticles(200); + InitializeParticles(700); _particles = GetNode("Particles").GetChildren().Cast().ToList(); } @@ -41,7 +42,7 @@ public class Main : Node2D foreach (var type1 in _particleTypes) foreach (var type2 in _particleTypes) - type1.AddRelationship(type2, 20, (float) GD.RandRange(25, 60), (float) GD.RandRange(-0.33, 0.35)); + type1.AddRelationship(type2, 20, (float) GD.RandRange(25, 60), (float) GD.RandRange(-0.33 * 2f, 0.35 * 2f)); } private void InitializeParticles(int nParticles) @@ -73,49 +74,52 @@ public class Main : Node2D private void UpdateParticles() { + var tasks = new Task[_particles.Count]; var viewportRect = GetViewportRect(); - foreach (var p1 in _particles) + for (var i = 0; i < _particles.Count; i++) tasks[i] = Task.Factory.StartNew(UpdateParticle, _particles[i]); + + Task.WaitAll(tasks); + + foreach (var p in _particles) { - foreach (var p2 in _particles) - { - if (p1 == p2) - continue; - var distance = p1.Position.DistanceTo(p2.Position); - if (distance > 60f) - continue; - var direction = p1.Position.DirectionTo(p2.Position); - - // collision force - if (distance < 20) - { - const float collisionStretch = 1f; - const float collisionOffset = 13f; - const float collisionFlatten = 1f; - var rawCollisionForce = -Mathf.Pow(Mathf.E, -(collisionStretch * distance - collisionOffset)) / - collisionFlatten; - var clampedCollisionForce = Mathf.Clamp(rawCollisionForce, -1f, 1f); - p1.Velocity += direction * clampedCollisionForce; - } - // particle relationship force - - var props = p1.Type.GetRelationship(p2.Type); - if (props.Force != 0f && distance >= props.MinRadius && distance <= props.MaxRadius) - { - var slope = props.Force / ((props.MaxRadius - props.MinRadius) / 2f); - var rawParticleForce = -slope * Mathf.Abs(distance - (props.MinRadius + props.MaxRadius) / 2f) + - props.Force; - p1.Velocity += direction * rawParticleForce; - } - - p1.Velocity = p1.Velocity.Clamped(3f); - } - - var position = p1.Position; - position += p1.Velocity; - p1.Velocity *= 0.95f; + var position = p.Position; + p.Velocity = p.Velocity.Clamped(3f * 2f); + position += p.Velocity; + p.Velocity *= 0.85f; position.x = Mathf.Clamp(position.x, ParticleScreenPadding, viewportRect.Size.x - ParticleScreenPadding); position.y = Mathf.Clamp(position.y, ParticleScreenPadding, viewportRect.Size.y - ParticleScreenPadding); - p1.Position = position; + p.Position = position; + } + } + + private void UpdateParticle(object particle) + { + var p1 = (Particle) particle; + foreach (var p2 in _particles) + { + var viewportRect = GetViewportRect(); + if (p1 == p2) + continue; + var distance = p1.Position.DistanceTo(p2.Position); + if (distance > 60f) + continue; + var direction = p1.Position.DirectionTo(p2.Position); + + // collision force + if (distance < 20) + { + var collisionForce = 1f / (0.6f + Mathf.Pow(Mathf.E, -(distance - 12f))) - 1f / 0.6f; + p1.Velocity += direction * collisionForce; + } + // particle relationship force + + var props = p1.Type.GetRelationship(p2.Type); + if (props.Force == 0f || !(distance >= props.MinRadius) || !(distance <= props.MaxRadius)) + continue; + var slope = props.Force / ((props.MaxRadius - props.MinRadius) / 2f); + var particleForce = -slope * Mathf.Abs(distance - (props.MinRadius + props.MaxRadius) / 2f) + + props.Force; + p1.Velocity += direction * particleForce; } } } \ No newline at end of file diff --git a/project.godot b/project.godot index af2a5df..b83d025 100644 --- a/project.godot +++ b/project.godot @@ -16,8 +16,10 @@ config/icon="res://icon.png" [display] -window/size/width=1600 -window/size/height=900 +window/size/width=1920 +window/size/height=1080 +window/size/resizable=false +window/size/borderless=true [input] @@ -34,6 +36,7 @@ reset={ [physics] +common/physics_fps=30 common/enable_pause_aware_picking=true [rendering]