WIP on master: 6e5d406 Decouple ParticleSimulation into separate class

This commit is contained in:
orosmatthew 2021-12-28 01:55:28 -05:00
commit a99e3f3839
2 changed files with 46 additions and 17 deletions

View File

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

View File

@ -1,4 +1,5 @@
using Godot;
using System;
using Godot;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
@ -21,11 +22,15 @@ namespace Particles
private int _idCount = 0;
private const int MaxParticles = 1200;
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 ParticleCollisionRadius = 13f;
private const float ParticleCollisionStrength = 2.5f;
private const float ParticleCollisionResponse = 2f;
public void Initialize()
{
for (var i = 0; i < MaxParticleTypes; i++)
@ -57,9 +62,9 @@ namespace Particles
}
var position = particle.Position;
particle.Velocity = particle.Velocity.Clamped(3f * 4f);
particle.Velocity = particle.Velocity.Clamped(4f);
position += particle.Velocity;
particle.Velocity *= 0.855f; // friction
particle.Velocity *= 0.875f; // friction
if (position.x > ScreenSize.x)
position.x -= ScreenSize.x;
else if (position.x < 0)
@ -70,7 +75,7 @@ namespace Particles
position.y += ScreenSize.y;
particle.AddAverageSpeedValue(particle.Position.DistanceTo(position));
if (particle.AverageSpeed < 0.3f)
if (particle.AverageSpeed < 0.15f)
particle.Health -= HealthDelta * NegativeHealthMultiplier;
else
particle.Health += HealthDelta;
@ -95,7 +100,7 @@ namespace Particles
if (_particles.Count < MaxParticles)
{
if (GD.Randf() < 0.2f)
if (GD.Randf() < 0.3f)
{
CreateRandomParticle();
}
@ -122,8 +127,8 @@ namespace Particles
foreach (var type1 in _particleTypes)
foreach(var type2 in _particleTypes)
type1.AddRelationship(type2,
new ParticleRelationshipProps(16, (float)GD.RandRange(20, 60),
(float)GD.RandRange(-0.35 * 2, 0.35 * 2)));
new ParticleRelationshipProps(ParticleCollisionRadius, (float)GD.RandRange(22, 50),
(float)GD.RandRange(-0.3, 0.3)));
}
private void CreateRandomParticle()
@ -185,19 +190,20 @@ namespace Particles
continue;
var position = GetScreenWrapPosition(particle1.Position, particle2.Position);
var distanceSquared = particle1.Position.DistanceSquaredTo(position);
if (distanceSquared > (60f * 60f))
if (distanceSquared > (50f * 50f))
continue;
var direction = particle1.Position.DirectionTo(position);
if (distanceSquared < (70f * 70f))
if (distanceSquared < (50f * 50f))
closeCount++;
// collision force
float distance;
if (distanceSquared < (16f * 16f))
if (distanceSquared < (ParticleCollisionRadius * ParticleCollisionRadius))
{
distance = particle1.Position.DistanceTo(position);
var collisionForce = 1f / (0.3f + Mathf.Pow(Mathf.E, -(distance - 11f))) - 1f / 0.3f;
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;
particle1.Velocity += direction * collisionForce;
}
@ -207,9 +213,32 @@ namespace Particles
distanceSquared <= props.MaxRadius * props.MaxRadius)
{
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;
var mid = (props.MinRadius + props.MaxRadius) / 2f;
float particleForce;
if (props.Force > 0)
{
if (distance <= mid)
{
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)));
}
}
else
{
if (distance <= mid)
{
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)));
}
}
particle1.Velocity += direction * particleForce;
}
}