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://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 )

View File

@ -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)
{ {

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://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 )

View File

@ -80,6 +80,7 @@ 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;
@ -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());
@ -257,22 +259,26 @@ 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)));
} }
} }
@ -286,5 +292,16 @@ namespace Particles.ParticleSimulation
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;
}
}
}
} }
} }

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) "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]

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