added multithreading
This commit is contained in:
parent
6cdbf2d9e5
commit
1d487a5462
86
Main.cs
86
Main.cs
@ -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,49 +74,52 @@ 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)
|
||||||
{
|
{
|
||||||
foreach (var p2 in _particles)
|
var position = p.Position;
|
||||||
{
|
p.Velocity = p.Velocity.Clamped(3f * 2f);
|
||||||
if (p1 == p2)
|
position += p.Velocity;
|
||||||
continue;
|
p.Velocity *= 0.85f;
|
||||||
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;
|
|
||||||
position.x = Mathf.Clamp(position.x, ParticleScreenPadding, viewportRect.Size.x - ParticleScreenPadding);
|
position.x = Mathf.Clamp(position.x, ParticleScreenPadding, viewportRect.Size.x - ParticleScreenPadding);
|
||||||
position.y = Mathf.Clamp(position.y, ParticleScreenPadding, viewportRect.Size.y - 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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]
|
||||||
|
Loading…
Reference in New Issue
Block a user