added mouse interaction for #2
This commit is contained in:
parent
bf7ef8bd76
commit
1a463f8edf
@ -3,7 +3,6 @@
|
|||||||
[ext_resource path="res://ParticleSimulation/ParticleNode.cs" type="Script" id=1]
|
[ext_resource path="res://ParticleSimulation/ParticleNode.cs" type="Script" id=1]
|
||||||
[ext_resource path="res://textures/particle_noborder.png" type="Texture" id=2]
|
[ext_resource path="res://textures/particle_noborder.png" type="Texture" id=2]
|
||||||
|
|
||||||
|
|
||||||
[node name="ParticleNode" type="Node2D"]
|
[node name="ParticleNode" type="Node2D"]
|
||||||
script = ExtResource( 1 )
|
script = ExtResource( 1 )
|
||||||
|
|
||||||
|
@ -21,8 +21,13 @@ public class ParticleSimulationScene : Node2D
|
|||||||
private ParticleSimulation _particleSimulation;
|
private ParticleSimulation _particleSimulation;
|
||||||
public float PhysicsInterpolationFraction;
|
public float PhysicsInterpolationFraction;
|
||||||
|
|
||||||
|
private bool _wasInteractPrevEnabled;
|
||||||
|
private Vector2 _prevMousePos;
|
||||||
|
|
||||||
public void Initialize(int seed, int nParticles, float zoom)
|
public void Initialize(int seed, int nParticles, float zoom)
|
||||||
{
|
{
|
||||||
|
_wasInteractPrevEnabled = false;
|
||||||
|
_prevMousePos = new Vector2();
|
||||||
_maxZoom = zoom;
|
_maxZoom = zoom;
|
||||||
_camera = GetNode<Camera2D>("Camera2D");
|
_camera = GetNode<Camera2D>("Camera2D");
|
||||||
_cameraTween = GetNode<Tween>("CameraTween");
|
_cameraTween = GetNode<Tween>("CameraTween");
|
||||||
@ -51,6 +56,16 @@ public class ParticleSimulationScene : Node2D
|
|||||||
if (Input.IsActionJustPressed("reset"))
|
if (Input.IsActionJustPressed("reset"))
|
||||||
GetParent<Main>().RestartSimulation();
|
GetParent<Main>().RestartSimulation();
|
||||||
|
|
||||||
|
if (Input.IsActionPressed("enable_interaction"))
|
||||||
|
{
|
||||||
|
GetNode<Sprite>("InteractionCircleSprite").Show();
|
||||||
|
GetNode<Sprite>("InteractionCircleSprite").Position = GetGlobalMousePosition();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
GetNode<Sprite>("InteractionCircleSprite").Hide();
|
||||||
|
}
|
||||||
|
|
||||||
var shouldTweenStop = false;
|
var shouldTweenStop = false;
|
||||||
|
|
||||||
if (Input.IsActionJustReleased("zoom_in"))
|
if (Input.IsActionJustReleased("zoom_in"))
|
||||||
@ -136,6 +151,27 @@ public class ParticleSimulationScene : Node2D
|
|||||||
|
|
||||||
public override void _PhysicsProcess(float delta)
|
public override void _PhysicsProcess(float delta)
|
||||||
{
|
{
|
||||||
|
if (Input.IsActionPressed("enable_interaction"))
|
||||||
|
{
|
||||||
|
if (_wasInteractPrevEnabled)
|
||||||
|
{
|
||||||
|
var mouseVel = GetGlobalMousePosition() - _prevMousePos;
|
||||||
|
mouseVel /= 5f;
|
||||||
|
|
||||||
|
_prevMousePos = GetGlobalMousePosition();
|
||||||
|
_particleSimulation.SetInteractionCircle(GetGlobalMousePosition() + (_spaceSize / 2.0f), 70f, mouseVel);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_wasInteractPrevEnabled = true;
|
||||||
|
_prevMousePos = GetGlobalMousePosition();
|
||||||
|
_particleSimulation.SetInteractionCircle(GetGlobalMousePosition() + (_spaceSize / 2.0f), 70f, Vector2.Zero);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_wasInteractPrevEnabled = false;
|
||||||
|
}
|
||||||
_particleSimulation.Update();
|
_particleSimulation.Update();
|
||||||
foreach (var id in _particleSimulation.LastParticlesRemoved)
|
foreach (var id in _particleSimulation.LastParticlesRemoved)
|
||||||
{
|
{
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
[gd_scene load_steps=2 format=2]
|
[gd_scene load_steps=3 format=2]
|
||||||
|
|
||||||
[ext_resource path="res://ParticleSimulation/ParticleSimulationScene.cs" type="Script" id=1]
|
[ext_resource path="res://ParticleSimulation/ParticleSimulationScene.cs" type="Script" id=1]
|
||||||
|
[ext_resource path="res://textures/interaction_circle.png" type="Texture" id=2]
|
||||||
|
|
||||||
[node name="ParticleSimulationScene" type="Node2D"]
|
[node name="ParticleSimulationScene" type="Node2D"]
|
||||||
script = ExtResource( 1 )
|
script = ExtResource( 1 )
|
||||||
@ -13,3 +14,8 @@ zoom = Vector2( 1.35, 1.35 )
|
|||||||
smoothing_speed = 100.0
|
smoothing_speed = 100.0
|
||||||
|
|
||||||
[node name="CameraTween" type="Tween" parent="."]
|
[node name="CameraTween" type="Tween" parent="."]
|
||||||
|
|
||||||
|
[node name="InteractionCircleSprite" type="Sprite" parent="."]
|
||||||
|
modulate = Color( 1, 1, 1, 0.313726 )
|
||||||
|
scale = Vector2( 0.75, 0.75 )
|
||||||
|
texture = ExtResource( 2 )
|
||||||
|
@ -13,27 +13,27 @@ namespace Particles.ParticleSimulation
|
|||||||
{
|
{
|
||||||
// size of simulation space
|
// size of simulation space
|
||||||
public Vector2 SpaceSize;
|
public Vector2 SpaceSize;
|
||||||
|
|
||||||
// dictionary of particles with particle Id being the key
|
// dictionary of particles with particle Id being the key
|
||||||
private readonly Dictionary<int, Particle> _particles = new Dictionary<int, Particle>();
|
private readonly Dictionary<int, Particle> _particles = new Dictionary<int, Particle>();
|
||||||
|
|
||||||
private readonly List<ParticleType> _particleTypes = new List<ParticleType>();
|
private readonly List<ParticleType> _particleTypes = new List<ParticleType>();
|
||||||
|
|
||||||
// task list if multi-threaded
|
// task list if multi-threaded
|
||||||
#if MULTITHREADED
|
#if MULTITHREADED
|
||||||
private readonly List<Task> _tasks = new List<Task>();
|
private readonly List<Task> _tasks = new List<Task>();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// updated on every simulation update
|
// updated on every simulation update
|
||||||
public List<int> LastParticlesAdded { get; private set; } = new List<int>();
|
public List<int> LastParticlesAdded { get; private set; } = new List<int>();
|
||||||
public List<int> LastParticlesRemoved { get; private set; } = new List<int>();
|
public List<int> LastParticlesRemoved { get; private set; } = new List<int>();
|
||||||
|
|
||||||
// counts up for each particle added
|
// counts up for each particle added
|
||||||
private int _idCount;
|
private int _idCount;
|
||||||
|
|
||||||
private int _maxParticles;
|
private int _maxParticles;
|
||||||
private const int MaxParticleTypes = 10;
|
private const int MaxParticleTypes = 10;
|
||||||
|
|
||||||
private const float HealthDelta = 0.005f;
|
private const float HealthDelta = 0.005f;
|
||||||
private const float NegativeHealthMultiplier = 2f;
|
private const float NegativeHealthMultiplier = 2f;
|
||||||
private const float PositiveHealthMultiplier = 4f;
|
private const float PositiveHealthMultiplier = 4f;
|
||||||
@ -51,21 +51,21 @@ namespace Particles.ParticleSimulation
|
|||||||
{
|
{
|
||||||
LastParticlesRemoved.Clear();
|
LastParticlesRemoved.Clear();
|
||||||
LastParticlesAdded.Clear();
|
LastParticlesAdded.Clear();
|
||||||
|
|
||||||
// update all particles
|
// update all particles
|
||||||
#if MULTITHREADED
|
#if MULTITHREADED
|
||||||
_tasks.Clear();
|
_tasks.Clear();
|
||||||
foreach (var id in _particles.Keys)
|
foreach (var id in _particles.Keys)
|
||||||
_tasks.Add(Task.Factory.StartNew(UpdateParticle, id));
|
_tasks.Add(Task.Factory.StartNew(UpdateParticle, id));
|
||||||
Task.WaitAll(_tasks.ToArray());
|
Task.WaitAll(_tasks.ToArray());
|
||||||
#else
|
#else
|
||||||
foreach (var id in _particles.Keys)
|
foreach (var id in _particles.Keys)
|
||||||
UpdateParticle(id);
|
UpdateParticle(id);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// used to ensure only one particle is moved per update
|
// used to ensure only one particle is moved per update
|
||||||
var movedParticle = false;
|
var movedParticle = false;
|
||||||
|
|
||||||
foreach (var particle in _particles.Select(p => p.Value))
|
foreach (var particle in _particles.Select(p => p.Value))
|
||||||
{
|
{
|
||||||
particle.WasTeleportedLast = false;
|
particle.WasTeleportedLast = false;
|
||||||
@ -80,10 +80,11 @@ namespace Particles.ParticleSimulation
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var position = particle.Position;
|
var position = particle.Position;
|
||||||
particle.Velocity = particle.Velocity.Clamped(5f);
|
particle.Velocity = particle.Velocity.Clamped(5f);
|
||||||
position += particle.Velocity;
|
position += particle.Velocity;
|
||||||
particle.Velocity *= 0.855f; // friction
|
particle.Velocity *= 0.855f; // friction
|
||||||
if (position.x > SpaceSize.x)
|
if (position.x > SpaceSize.x)
|
||||||
{
|
{
|
||||||
position.x -= SpaceSize.x;
|
position.x -= SpaceSize.x;
|
||||||
@ -105,6 +106,7 @@ namespace Particles.ParticleSimulation
|
|||||||
position.y += SpaceSize.y;
|
position.y += SpaceSize.y;
|
||||||
particle.WasTeleportedLast = true;
|
particle.WasTeleportedLast = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
particle.AddAverageSpeedValue(particle.Velocity.Length());
|
particle.AddAverageSpeedValue(particle.Velocity.Length());
|
||||||
|
|
||||||
@ -164,8 +166,8 @@ namespace Particles.ParticleSimulation
|
|||||||
foreach (var type1 in _particleTypes)
|
foreach (var type1 in _particleTypes)
|
||||||
foreach (var type2 in _particleTypes)
|
foreach (var type2 in _particleTypes)
|
||||||
type1.AddRelationship(type2,
|
type1.AddRelationship(type2,
|
||||||
new ParticleRelationshipProps(ParticleCollisionRadius, (float) GD.RandRange(25, 55),
|
new ParticleRelationshipProps(ParticleCollisionRadius, (float) GD.RandRange(25, 55),
|
||||||
(float)GD.RandRange(-0.675, 0.7)));
|
(float) GD.RandRange(-0.675, 0.7)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CreateRandomParticle()
|
private void CreateRandomParticle()
|
||||||
@ -194,7 +196,7 @@ namespace Particles.ParticleSimulation
|
|||||||
(float) GD.RandRange(0, SpaceSize.y));
|
(float) GD.RandRange(0, SpaceSize.y));
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Vector2 GetScreenWrapPosition(Vector2 p1, Vector2 p2)
|
private Vector2 GetScreenWrapPosition(Vector2 p1, Vector2 p2)
|
||||||
{
|
{
|
||||||
var newPosition = p2;
|
var newPosition = p2;
|
||||||
@ -208,15 +210,15 @@ namespace Particles.ParticleSimulation
|
|||||||
newPosition.y = p2.y + SpaceSize.y;
|
newPosition.y = p2.y + SpaceSize.y;
|
||||||
return newPosition;
|
return newPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Particle GetParticle(int id)
|
public Particle GetParticle(int id)
|
||||||
{
|
{
|
||||||
return _particles[id];
|
return _particles[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateParticle(object i)
|
private void UpdateParticle(object i)
|
||||||
{
|
{
|
||||||
var id = (int)i;
|
var id = (int) i;
|
||||||
var particle1 = _particles[id];
|
var particle1 = _particles[id];
|
||||||
var closeCount = 0;
|
var closeCount = 0;
|
||||||
foreach (var p2 in _particles)
|
foreach (var p2 in _particles)
|
||||||
@ -231,11 +233,11 @@ namespace Particles.ParticleSimulation
|
|||||||
|
|
||||||
if (distanceSquared < (35f * 35f))
|
if (distanceSquared < (35f * 35f))
|
||||||
closeCount++;
|
closeCount++;
|
||||||
|
|
||||||
// collision force
|
// collision force
|
||||||
float distance;
|
float distance;
|
||||||
Vector2 direction;
|
Vector2 direction;
|
||||||
|
|
||||||
if (distanceSquared < (ParticleCollisionRadius * ParticleCollisionRadius))
|
if (distanceSquared < (ParticleCollisionRadius * ParticleCollisionRadius))
|
||||||
{
|
{
|
||||||
direction = particle1.Position.DirectionTo(position);
|
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;
|
var collisionForce = 1f / (0.35f + Mathf.Pow(Mathf.E, -1.15f * (distance - 12f))) - 1f / 0.35f;
|
||||||
particle1.Velocity += direction * collisionForce;
|
particle1.Velocity += direction * collisionForce;
|
||||||
}
|
}
|
||||||
|
|
||||||
// particle relationship force
|
// particle relationship force
|
||||||
var props = particle1.Type.GetRelationship(particle2.Type);
|
var props = particle1.Type.GetRelationship(particle2.Type);
|
||||||
if (props.Force != 0f && distanceSquared >= props.MinRadius * props.MinRadius &&
|
if (props.Force != 0f && distanceSquared >= props.MinRadius * props.MinRadius &&
|
||||||
@ -257,34 +259,49 @@ namespace Particles.ParticleSimulation
|
|||||||
{
|
{
|
||||||
if (distance <= mid)
|
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
|
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
|
else
|
||||||
{
|
{
|
||||||
if (distance <= mid)
|
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
|
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;
|
particle1.Velocity += direction * particleForce;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (closeCount > 70)
|
if (closeCount > 70)
|
||||||
particle1.Health -= HealthDelta * NegativeHealthMultiplier;
|
particle1.Health -= HealthDelta * NegativeHealthMultiplier;
|
||||||
else
|
else
|
||||||
particle1.Health += HealthDelta * PositiveHealthMultiplier;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,6 +73,11 @@ key_zoom_out={
|
|||||||
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":81,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
|
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":81,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
enable_interaction={
|
||||||
|
"deadzone": 0.5,
|
||||||
|
"events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":1,"pressed":false,"doubleclick":false,"script":null)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
[mono]
|
[mono]
|
||||||
|
|
||||||
|
BIN
textures/interaction_circle.png
Normal file
BIN
textures/interaction_circle.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
35
textures/interaction_circle.png.import
Normal file
35
textures/interaction_circle.png.import
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
[remap]
|
||||||
|
|
||||||
|
importer="texture"
|
||||||
|
type="StreamTexture"
|
||||||
|
path="res://.import/interaction_circle.png-10aab2660a95f68c88825c76c59d9be8.stex"
|
||||||
|
metadata={
|
||||||
|
"vram_texture": false
|
||||||
|
}
|
||||||
|
|
||||||
|
[deps]
|
||||||
|
|
||||||
|
source_file="res://textures/interaction_circle.png"
|
||||||
|
dest_files=[ "res://.import/interaction_circle.png-10aab2660a95f68c88825c76c59d9be8.stex" ]
|
||||||
|
|
||||||
|
[params]
|
||||||
|
|
||||||
|
compress/mode=0
|
||||||
|
compress/lossy_quality=0.7
|
||||||
|
compress/hdr_mode=0
|
||||||
|
compress/bptc_ldr=0
|
||||||
|
compress/normal_map=0
|
||||||
|
flags/repeat=0
|
||||||
|
flags/filter=true
|
||||||
|
flags/mipmaps=false
|
||||||
|
flags/anisotropic=false
|
||||||
|
flags/srgb=2
|
||||||
|
process/fix_alpha_border=true
|
||||||
|
process/premult_alpha=false
|
||||||
|
process/HDR_as_SRGB=false
|
||||||
|
process/invert_color=false
|
||||||
|
process/normal_map_invert_y=false
|
||||||
|
stream=false
|
||||||
|
size_limit=0
|
||||||
|
detect_3d=true
|
||||||
|
svg/scale=1.0
|
Loading…
Reference in New Issue
Block a user