added mouse interaction for #2
This commit is contained in:
@@ -13,27 +13,27 @@ namespace Particles.ParticleSimulation
|
||||
{
|
||||
// size of simulation space
|
||||
public Vector2 SpaceSize;
|
||||
|
||||
|
||||
// dictionary of particles with particle Id being the key
|
||||
private readonly Dictionary<int, Particle> _particles = new Dictionary<int, Particle>();
|
||||
|
||||
|
||||
private readonly List<ParticleType> _particleTypes = new List<ParticleType>();
|
||||
|
||||
|
||||
// task list if multi-threaded
|
||||
#if MULTITHREADED
|
||||
private readonly List<Task> _tasks = new List<Task>();
|
||||
#endif
|
||||
|
||||
#if MULTITHREADED
|
||||
private readonly List<Task> _tasks = new List<Task>();
|
||||
#endif
|
||||
|
||||
// updated on every simulation update
|
||||
public List<int> LastParticlesAdded { get; private set; } = new List<int>();
|
||||
public List<int> LastParticlesRemoved { get; private set; } = new List<int>();
|
||||
|
||||
// counts up for each particle added
|
||||
private int _idCount;
|
||||
|
||||
|
||||
private int _maxParticles;
|
||||
private const int MaxParticleTypes = 10;
|
||||
|
||||
|
||||
private const float HealthDelta = 0.005f;
|
||||
private const float NegativeHealthMultiplier = 2f;
|
||||
private const float PositiveHealthMultiplier = 4f;
|
||||
@@ -51,21 +51,21 @@ namespace Particles.ParticleSimulation
|
||||
{
|
||||
LastParticlesRemoved.Clear();
|
||||
LastParticlesAdded.Clear();
|
||||
|
||||
|
||||
// update all particles
|
||||
#if MULTITHREADED
|
||||
_tasks.Clear();
|
||||
foreach (var id in _particles.Keys)
|
||||
_tasks.Add(Task.Factory.StartNew(UpdateParticle, id));
|
||||
Task.WaitAll(_tasks.ToArray());
|
||||
#else
|
||||
#if MULTITHREADED
|
||||
_tasks.Clear();
|
||||
foreach (var id in _particles.Keys)
|
||||
_tasks.Add(Task.Factory.StartNew(UpdateParticle, id));
|
||||
Task.WaitAll(_tasks.ToArray());
|
||||
#else
|
||||
foreach (var id in _particles.Keys)
|
||||
UpdateParticle(id);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
// used to ensure only one particle is moved per update
|
||||
var movedParticle = false;
|
||||
|
||||
|
||||
foreach (var particle in _particles.Select(p => p.Value))
|
||||
{
|
||||
particle.WasTeleportedLast = false;
|
||||
@@ -80,10 +80,11 @@ namespace Particles.ParticleSimulation
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
var position = particle.Position;
|
||||
particle.Velocity = particle.Velocity.Clamped(5f);
|
||||
position += particle.Velocity;
|
||||
particle.Velocity *= 0.855f; // friction
|
||||
particle.Velocity *= 0.855f; // friction
|
||||
if (position.x > SpaceSize.x)
|
||||
{
|
||||
position.x -= SpaceSize.x;
|
||||
@@ -105,6 +106,7 @@ namespace Particles.ParticleSimulation
|
||||
position.y += SpaceSize.y;
|
||||
particle.WasTeleportedLast = true;
|
||||
}
|
||||
|
||||
/*
|
||||
particle.AddAverageSpeedValue(particle.Velocity.Length());
|
||||
|
||||
@@ -164,8 +166,8 @@ namespace Particles.ParticleSimulation
|
||||
foreach (var type1 in _particleTypes)
|
||||
foreach (var type2 in _particleTypes)
|
||||
type1.AddRelationship(type2,
|
||||
new ParticleRelationshipProps(ParticleCollisionRadius, (float) GD.RandRange(25, 55),
|
||||
(float)GD.RandRange(-0.675, 0.7)));
|
||||
new ParticleRelationshipProps(ParticleCollisionRadius, (float) GD.RandRange(25, 55),
|
||||
(float) GD.RandRange(-0.675, 0.7)));
|
||||
}
|
||||
|
||||
private void CreateRandomParticle()
|
||||
@@ -194,7 +196,7 @@ namespace Particles.ParticleSimulation
|
||||
(float) GD.RandRange(0, SpaceSize.y));
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
private Vector2 GetScreenWrapPosition(Vector2 p1, Vector2 p2)
|
||||
{
|
||||
var newPosition = p2;
|
||||
@@ -208,15 +210,15 @@ namespace Particles.ParticleSimulation
|
||||
newPosition.y = p2.y + SpaceSize.y;
|
||||
return newPosition;
|
||||
}
|
||||
|
||||
|
||||
public Particle GetParticle(int id)
|
||||
{
|
||||
return _particles[id];
|
||||
}
|
||||
|
||||
|
||||
private void UpdateParticle(object i)
|
||||
{
|
||||
var id = (int)i;
|
||||
var id = (int) i;
|
||||
var particle1 = _particles[id];
|
||||
var closeCount = 0;
|
||||
foreach (var p2 in _particles)
|
||||
@@ -231,11 +233,11 @@ namespace Particles.ParticleSimulation
|
||||
|
||||
if (distanceSquared < (35f * 35f))
|
||||
closeCount++;
|
||||
|
||||
|
||||
// collision force
|
||||
float distance;
|
||||
Vector2 direction;
|
||||
|
||||
|
||||
if (distanceSquared < (ParticleCollisionRadius * ParticleCollisionRadius))
|
||||
{
|
||||
direction = particle1.Position.DirectionTo(position);
|
||||
@@ -243,7 +245,7 @@ namespace Particles.ParticleSimulation
|
||||
var collisionForce = 1f / (0.35f + Mathf.Pow(Mathf.E, -1.15f * (distance - 12f))) - 1f / 0.35f;
|
||||
particle1.Velocity += direction * collisionForce;
|
||||
}
|
||||
|
||||
|
||||
// particle relationship force
|
||||
var props = particle1.Type.GetRelationship(particle2.Type);
|
||||
if (props.Force != 0f && distanceSquared >= props.MinRadius * props.MinRadius &&
|
||||
@@ -257,34 +259,49 @@ namespace Particles.ParticleSimulation
|
||||
{
|
||||
if (distance <= mid)
|
||||
{
|
||||
particleForce = 1f / ((1f / props.Force) + Mathf.Pow(Mathf.E, -4 * (distance - props.MinRadius - 1f)));
|
||||
particleForce = 1f / ((1f / props.Force) +
|
||||
Mathf.Pow(Mathf.E, -4 * (distance - props.MinRadius - 1f)));
|
||||
}
|
||||
else
|
||||
{
|
||||
particleForce = 1f / ((1f / props.Force) + Mathf.Pow(Mathf.E, 4 * (distance - props.MaxRadius + 1f)));
|
||||
particleForce = 1f / ((1f / props.Force) +
|
||||
Mathf.Pow(Mathf.E, 4 * (distance - props.MaxRadius + 1f)));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (distance <= mid)
|
||||
{
|
||||
particleForce = -1f / ((-1f / props.Force) + Mathf.Pow(Mathf.E, -4 * (distance - props.MinRadius - 1f)));
|
||||
particleForce = -1f / ((-1f / props.Force) +
|
||||
Mathf.Pow(Mathf.E, -4 * (distance - props.MinRadius - 1f)));
|
||||
}
|
||||
else
|
||||
{
|
||||
particleForce = -1f / ((-1f / props.Force) + Mathf.Pow(Mathf.E, 4 * (distance - props.MaxRadius + 1f)));
|
||||
particleForce = -1f / ((-1f / props.Force) +
|
||||
Mathf.Pow(Mathf.E, 4 * (distance - props.MaxRadius + 1f)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
particle1.Velocity += direction * particleForce;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (closeCount > 70)
|
||||
particle1.Health -= HealthDelta * NegativeHealthMultiplier;
|
||||
else
|
||||
particle1.Health += HealthDelta * PositiveHealthMultiplier;
|
||||
}
|
||||
|
||||
public void SetInteractionCircle(Vector2 position, float radius, Vector2 velocity)
|
||||
{
|
||||
foreach (var p in _particles.Select(i => i.Value))
|
||||
{
|
||||
if (position.DistanceTo(p.Position) <= radius)
|
||||
{
|
||||
p.Velocity += velocity;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user