index on (no branch): a99e3f3 WIP on master: 6e5d406 Decouple ParticleSimulation into separate class

This commit is contained in:
orosmatthew 2021-12-28 14:32:11 -05:00
parent a99e3f3839
commit 2efdd0edec
4 changed files with 35 additions and 20 deletions

View File

@ -16,7 +16,7 @@ public class Main : Node2D
GD.Randomize();
_particleSimulation = new ParticleSimulation
{
ScreenSize = GetViewportRect().Size
ScreenSize = GetViewportRect().Size * 1.25f
};
_particleSimulation.Initialize();
foreach (var id in _particleSimulation.LastParticlesAdded)

View File

@ -6,3 +6,8 @@
script = ExtResource( 1 )
[node name="ParticlesNodes" type="Node2D" parent="."]
[node name="Camera2D" type="Camera2D" parent="."]
anchor_mode = 0
current = true
zoom = Vector2( 1.25, 1.25 )

View File

@ -7,7 +7,7 @@
script = ExtResource( 1 )
[node name="Sprite" type="Sprite" parent="."]
scale = Vector2( 0.2, 0.2 )
scale = Vector2( 0.25, 0.25 )
texture = ExtResource( 2 )
[node name="Label" type="Label" parent="."]

View File

@ -24,10 +24,10 @@ namespace Particles
private const int MaxParticles = 1000;
private const int MaxParticleTypes = 10;
private const float HealthDelta = 0.002f;
private const float NegativeHealthMultiplier = 1.5f;
private const float HealthDelta = 0.0085f;
private const float NegativeHealthMultiplier = 0.25f;
private const float ParticleCollisionRadius = 13f;
private const float ParticleCollisionRadius = 20f;
private const float ParticleCollisionStrength = 2.5f;
private const float ParticleCollisionResponse = 2f;
@ -62,9 +62,9 @@ namespace Particles
}
var position = particle.Position;
particle.Velocity = particle.Velocity.Clamped(4f);
particle.Velocity = particle.Velocity.Clamped(5f);
position += particle.Velocity;
particle.Velocity *= 0.875f; // friction
particle.Velocity *= 0.855f; // friction
if (position.x > ScreenSize.x)
position.x -= ScreenSize.x;
else if (position.x < 0)
@ -74,12 +74,12 @@ namespace Particles
else if (position.y < 0)
position.y += ScreenSize.y;
particle.AddAverageSpeedValue(particle.Position.DistanceTo(position));
if (particle.AverageSpeed < 0.15f)
/*
if (particle.AverageSpeed < 0.3f)
particle.Health -= HealthDelta * NegativeHealthMultiplier;
else
particle.Health += HealthDelta;
*/
if (deletedParticle == false && particle.Health == 0f)
{
if (GD.Randf() < 0.2f)
@ -100,7 +100,8 @@ namespace Particles
if (_particles.Count < MaxParticles)
{
if (GD.Randf() < 0.3f)
var value = 2f * (1f / (_particles.Count / (float)MaxParticles));
if (GD.Randf() < (0.3f * value))
{
CreateRandomParticle();
}
@ -127,8 +128,8 @@ namespace Particles
foreach (var type1 in _particleTypes)
foreach(var type2 in _particleTypes)
type1.AddRelationship(type2,
new ParticleRelationshipProps(ParticleCollisionRadius, (float)GD.RandRange(22, 50),
(float)GD.RandRange(-0.3, 0.3)));
new ParticleRelationshipProps(ParticleCollisionRadius, (float)GD.RandRange(25, 55),
(float)GD.RandRange(-0.6, 0.7)));
}
private void CreateRandomParticle()
@ -190,11 +191,11 @@ namespace Particles
continue;
var position = GetScreenWrapPosition(particle1.Position, particle2.Position);
var distanceSquared = particle1.Position.DistanceSquaredTo(position);
if (distanceSquared > (50f * 50f))
if (distanceSquared > (55f * 55f))
continue;
var direction = particle1.Position.DirectionTo(position);
if (distanceSquared < (50f * 50f))
if (distanceSquared < (40f * 40f))
closeCount++;
// collision force
@ -202,12 +203,21 @@ namespace Particles
if (distanceSquared < (ParticleCollisionRadius * ParticleCollisionRadius))
{
distance = particle1.Position.DistanceTo(position);
var collisionForce = 1f / ((1f / ParticleCollisionStrength) + Mathf.Pow(Mathf.E, -ParticleCollisionResponse * (distance - ParticleCollisionRadius + 3f))) - 1f / (1f / ParticleCollisionStrength);
//var collisionForce = 1f / (0.6f + Mathf.Pow(Mathf.E, -(distance - ParticleCollisionRadius - 2f))) - 1f / 0.6f;
var collisionForce = 1f / (0.6f + Mathf.Pow(Mathf.E, -(distance - 12f))) - 1f / 0.6f;
particle1.Velocity += direction * collisionForce;
}
// particle relationship force
/*
var props = particle1.Type.GetRelationship(particle2.Type);
if (props.Force == 0f || !(distanceSquared >= props.MinRadius * props.MinRadius) || !(distanceSquared <= props.MaxRadius * props.MaxRadius))
continue;
distance = particle1.Position.DistanceTo(position);
var slope = props.Force / ((props.MaxRadius - props.MinRadius) / 2f);
var particleForce = -slope * Mathf.Abs(distance - (props.MinRadius + props.MaxRadius) / 2f) +
props.Force;
particle1.Velocity += direction * particleForce;
*/
var props = particle1.Type.GetRelationship(particle2.Type);
if (props.Force != 0f && distanceSquared >= props.MinRadius * props.MinRadius &&
distanceSquared <= props.MaxRadius * props.MaxRadius)
@ -243,7 +253,7 @@ namespace Particles
}
}
if (closeCount < 4 || closeCount > 33)
if (closeCount > 50)
particle1.Health -= HealthDelta * NegativeHealthMultiplier;
else
particle1.Health += HealthDelta;