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

86
Main.cs
View File

@ -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<Node2D>("Particles").GetChildren().Cast<Particle>().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;
}
}
}

View File

@ -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]