added multithreading

This commit is contained in:
orosmatthew 2021-12-26 23:08:40 -05:00
parent 6cdbf2d9e5
commit 1d487a5462
2 changed files with 50 additions and 43 deletions

54
Main.cs
View File

@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using Godot; using Godot;
public class Main : Node2D public class Main : Node2D
@ -13,7 +14,7 @@ public class Main : Node2D
{ {
GD.Randomize(); GD.Randomize();
InitializeParticleTypes(10); InitializeParticleTypes(10);
InitializeParticles(200); InitializeParticles(700);
_particles = GetNode<Node2D>("Particles").GetChildren().Cast<Particle>().ToList(); _particles = GetNode<Node2D>("Particles").GetChildren().Cast<Particle>().ToList();
} }
@ -41,7 +42,7 @@ public class Main : Node2D
foreach (var type1 in _particleTypes) foreach (var type1 in _particleTypes)
foreach (var type2 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) private void InitializeParticles(int nParticles)
@ -73,11 +74,30 @@ public class Main : Node2D
private void UpdateParticles() private void UpdateParticles()
{ {
var tasks = new Task[_particles.Count];
var viewportRect = GetViewportRect(); 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)
{ {
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);
p.Position = position;
}
}
private void UpdateParticle(object particle)
{
var p1 = (Particle) particle;
foreach (var p2 in _particles) foreach (var p2 in _particles)
{ {
var viewportRect = GetViewportRect();
if (p1 == p2) if (p1 == p2)
continue; continue;
var distance = p1.Position.DistanceTo(p2.Position); var distance = p1.Position.DistanceTo(p2.Position);
@ -88,34 +108,18 @@ public class Main : Node2D
// collision force // collision force
if (distance < 20) if (distance < 20)
{ {
const float collisionStretch = 1f; var collisionForce = 1f / (0.6f + Mathf.Pow(Mathf.E, -(distance - 12f))) - 1f / 0.6f;
const float collisionOffset = 13f; p1.Velocity += direction * collisionForce;
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 // particle relationship force
var props = p1.Type.GetRelationship(p2.Type); var props = p1.Type.GetRelationship(p2.Type);
if (props.Force != 0f && distance >= props.MinRadius && distance <= props.MaxRadius) if (props.Force == 0f || !(distance >= props.MinRadius) || !(distance <= props.MaxRadius))
{ continue;
var slope = props.Force / ((props.MaxRadius - props.MinRadius) / 2f); var slope = props.Force / ((props.MaxRadius - props.MinRadius) / 2f);
var rawParticleForce = -slope * Mathf.Abs(distance - (props.MinRadius + props.MaxRadius) / 2f) + var particleForce = -slope * Mathf.Abs(distance - (props.MinRadius + props.MaxRadius) / 2f) +
props.Force; props.Force;
p1.Velocity += direction * rawParticleForce; p1.Velocity += direction * particleForce;
}
p1.Velocity = p1.Velocity.Clamped(3f);
}
var position = p1.Position;
position += p1.Velocity;
p1.Velocity *= 0.95f;
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;
} }
} }
} }

View File

@ -16,8 +16,10 @@ config/icon="res://icon.png"
[display] [display]
window/size/width=1600 window/size/width=1920
window/size/height=900 window/size/height=1080
window/size/resizable=false
window/size/borderless=true
[input] [input]
@ -34,6 +36,7 @@ reset={
[physics] [physics]
common/physics_fps=30
common/enable_pause_aware_picking=true common/enable_pause_aware_picking=true
[rendering] [rendering]