added mouse interaction for #2

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

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