added mouse interaction for #2

This commit is contained in:
orosmatthew 2022-02-07 12:30:45 -05:00
parent bf7ef8bd76
commit 1a463f8edf
7 changed files with 136 additions and 38 deletions

View File

@ -3,7 +3,6 @@
[ext_resource path="res://ParticleSimulation/ParticleNode.cs" type="Script" id=1]
[ext_resource path="res://textures/particle_noborder.png" type="Texture" id=2]
[node name="ParticleNode" type="Node2D"]
script = ExtResource( 1 )

View File

@ -21,8 +21,13 @@ public class ParticleSimulationScene : Node2D
private ParticleSimulation _particleSimulation;
public float PhysicsInterpolationFraction;
private bool _wasInteractPrevEnabled;
private Vector2 _prevMousePos;
public void Initialize(int seed, int nParticles, float zoom)
{
_wasInteractPrevEnabled = false;
_prevMousePos = new Vector2();
_maxZoom = zoom;
_camera = GetNode<Camera2D>("Camera2D");
_cameraTween = GetNode<Tween>("CameraTween");
@ -51,6 +56,16 @@ public class ParticleSimulationScene : Node2D
if (Input.IsActionJustPressed("reset"))
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;
if (Input.IsActionJustReleased("zoom_in"))
@ -136,6 +151,27 @@ public class ParticleSimulationScene : Node2D
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();
foreach (var id in _particleSimulation.LastParticlesRemoved)
{

View File

@ -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://textures/interaction_circle.png" type="Texture" id=2]
[node name="ParticleSimulationScene" type="Node2D"]
script = ExtResource( 1 )
@ -13,3 +14,8 @@ zoom = Vector2( 1.35, 1.35 )
smoothing_speed = 100.0
[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 )

View File

@ -20,9 +20,9 @@ namespace Particles.ParticleSimulation
private readonly List<ParticleType> _particleTypes = new List<ParticleType>();
// task list if multi-threaded
#if MULTITHREADED
#if MULTITHREADED
private readonly List<Task> _tasks = new List<Task>();
#endif
#endif
// updated on every simulation update
public List<int> LastParticlesAdded { get; private set; } = new List<int>();
@ -53,15 +53,15 @@ namespace Particles.ParticleSimulation
LastParticlesAdded.Clear();
// update all particles
#if MULTITHREADED
#if MULTITHREADED
_tasks.Clear();
foreach (var id in _particles.Keys)
_tasks.Add(Task.Factory.StartNew(UpdateParticle, id));
Task.WaitAll(_tasks.ToArray());
#else
#else
foreach (var id in _particles.Keys)
UpdateParticle(id);
#endif
#endif
// used to ensure only one particle is moved per update
var movedParticle = false;
@ -80,6 +80,7 @@ namespace Particles.ParticleSimulation
continue;
}
}
var position = particle.Position;
particle.Velocity = particle.Velocity.Clamped(5f);
position += particle.Velocity;
@ -105,6 +106,7 @@ namespace Particles.ParticleSimulation
position.y += SpaceSize.y;
particle.WasTeleportedLast = true;
}
/*
particle.AddAverageSpeedValue(particle.Velocity.Length());
@ -165,7 +167,7 @@ namespace Particles.ParticleSimulation
foreach (var type2 in _particleTypes)
type1.AddRelationship(type2,
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()
@ -216,7 +218,7 @@ namespace Particles.ParticleSimulation
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)
@ -257,22 +259,26 @@ 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)));
}
}
@ -286,5 +292,16 @@ namespace Particles.ParticleSimulation
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;
}
}
}
}
}

View File

@ -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)
]
}
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]

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View 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