2024-05-31 10:06:49 -03:00
|
|
|
|
using System.Windows.Controls;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using System.Windows.Shapes;
|
2024-05-31 10:06:49 -03:00
|
|
|
|
|
2024-05-25 09:38:36 -03:00
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using nkast.Aether.Physics2D.Dynamics;
|
|
|
|
|
using nkast.Aether.Physics2D.Common;
|
|
|
|
|
using nkast.Aether.Physics2D.Collision.Shapes;
|
2024-05-26 06:50:25 -03:00
|
|
|
|
using nkast.Aether.Physics2D.Dynamics.Contacts;
|
|
|
|
|
using nkast.Aether.Physics2D.Dynamics.Joints;
|
2024-05-31 10:06:49 -03:00
|
|
|
|
|
2024-05-25 09:38:36 -03:00
|
|
|
|
|
|
|
|
|
namespace CtrEditor.Simulacion
|
|
|
|
|
{
|
|
|
|
|
public class simBase
|
|
|
|
|
{
|
|
|
|
|
public Body Body { get; protected set; }
|
|
|
|
|
public World _world;
|
|
|
|
|
|
|
|
|
|
public void RemoverBody()
|
|
|
|
|
{
|
2024-05-30 13:48:37 -03:00
|
|
|
|
if (Body != null && _world.BodyList.Count>0 && _world.BodyList.Contains(Body))
|
2024-05-25 09:38:36 -03:00
|
|
|
|
{
|
|
|
|
|
_world.Remove(Body);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-31 10:06:49 -03:00
|
|
|
|
public static float GradosARadianes(float grados)
|
|
|
|
|
{
|
|
|
|
|
return (float)(grados * (Math.PI / 180));
|
|
|
|
|
}
|
|
|
|
|
public static float RadianesAGrados(float radianes)
|
|
|
|
|
{
|
|
|
|
|
return (float)(radianes * (180 / Math.PI));
|
|
|
|
|
}
|
2024-05-25 09:38:36 -03:00
|
|
|
|
public void SetPosition(float x, float y)
|
|
|
|
|
{
|
|
|
|
|
Body.SetTransform(new Vector2(x, y), Body.Rotation);
|
|
|
|
|
}
|
|
|
|
|
public void SetPosition(Vector2 centro)
|
|
|
|
|
{
|
2024-05-30 13:48:37 -03:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Body.SetTransform(centro, Body.Rotation);
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
2024-05-25 09:38:36 -03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class simCurve : simBase
|
|
|
|
|
{
|
|
|
|
|
private float _innerRadius;
|
|
|
|
|
private float _outerRadius;
|
|
|
|
|
private float _startAngle;
|
|
|
|
|
private float _endAngle;
|
|
|
|
|
public float Speed { get; set; } // Velocidad para efectos de cinta transportadora
|
2024-05-26 06:50:25 -03:00
|
|
|
|
private List<Action> _deferredActions;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
|
2024-05-26 06:50:25 -03:00
|
|
|
|
public simCurve(World world, List<Action> deferredActions, float innerRadius, float outerRadius, float startAngle, float endAngle, Vector2 position)
|
2024-05-25 09:38:36 -03:00
|
|
|
|
{
|
|
|
|
|
_world = world;
|
2024-05-26 06:50:25 -03:00
|
|
|
|
_deferredActions = deferredActions;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
_innerRadius = innerRadius;
|
|
|
|
|
_outerRadius = outerRadius;
|
2024-05-31 10:06:49 -03:00
|
|
|
|
_startAngle = GradosARadianes(startAngle);
|
|
|
|
|
_endAngle = GradosARadianes(endAngle);
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Create(position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Create(float innerRadius, float outerRadius, float startAngle, float endAngle, Vector2 position)
|
|
|
|
|
{
|
|
|
|
|
if (_world == null) return;
|
|
|
|
|
_innerRadius = innerRadius;
|
|
|
|
|
_outerRadius = outerRadius;
|
2024-05-31 10:06:49 -03:00
|
|
|
|
_startAngle = GradosARadianes(startAngle);
|
|
|
|
|
_endAngle = GradosARadianes(endAngle);
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Create(position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Create(Vector2 position)
|
|
|
|
|
{
|
|
|
|
|
RemoverBody();
|
|
|
|
|
|
|
|
|
|
// Crear la geometría del sensor de curva
|
|
|
|
|
List<Vertices> segments = CreateCurveVertices(_innerRadius, _outerRadius, _startAngle, _endAngle);
|
2024-05-30 13:48:37 -03:00
|
|
|
|
Body = _world.CreateBody();
|
2024-05-25 09:38:36 -03:00
|
|
|
|
foreach (var segment in segments)
|
|
|
|
|
{
|
|
|
|
|
var shape = new PolygonShape(segment, 1f);
|
|
|
|
|
var fixture = Body.CreateFixture(shape);
|
|
|
|
|
fixture.IsSensor = true;
|
2024-05-30 13:48:37 -03:00
|
|
|
|
}
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Body.Position = position;
|
|
|
|
|
Body.BodyType = BodyType.Static;
|
|
|
|
|
Body.Tag = this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetSpeed(float speed)
|
|
|
|
|
{
|
|
|
|
|
Speed = speed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<Vertices> CreateCurveVertices(float innerRadius, float outerRadius, float startAngle, float endAngle)
|
|
|
|
|
{
|
|
|
|
|
List<Vertices> verticesList = new List<Vertices>();
|
|
|
|
|
int segments = 32;
|
|
|
|
|
float angleStep = (endAngle - startAngle) / segments;
|
|
|
|
|
|
|
|
|
|
Vertices innerVertices = new Vertices();
|
|
|
|
|
Vertices outerVertices = new Vertices();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i <= segments; i++)
|
|
|
|
|
{
|
|
|
|
|
float angle = startAngle + i * angleStep;
|
|
|
|
|
innerVertices.Add(new Vector2(innerRadius * (float)Math.Cos(angle), innerRadius * (float)Math.Sin(angle)));
|
|
|
|
|
outerVertices.Add(new Vector2(outerRadius * (float)Math.Cos(angle), outerRadius * (float)Math.Sin(angle)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
outerVertices.Reverse();
|
|
|
|
|
innerVertices.AddRange(outerVertices);
|
|
|
|
|
verticesList.Add(innerVertices);
|
|
|
|
|
|
|
|
|
|
return verticesList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ApplyCurveEffect(Fixture bottle)
|
|
|
|
|
{
|
|
|
|
|
Vector2 centerToBottle = bottle.Body.Position - Body.Position;
|
|
|
|
|
float distanceToCenter = centerToBottle.Length();
|
|
|
|
|
|
|
|
|
|
if (distanceToCenter >= _innerRadius && distanceToCenter <= _outerRadius)
|
|
|
|
|
{
|
|
|
|
|
// Calcular la velocidad tangencial
|
|
|
|
|
float speedMetersPerSecond = Speed / 60.0f;
|
|
|
|
|
float angularVelocity = speedMetersPerSecond / distanceToCenter;
|
|
|
|
|
|
|
|
|
|
// Vector tangente (perpendicular al radio)
|
|
|
|
|
Vector2 tangent = new Vector2(-centerToBottle.Y, centerToBottle.X);
|
|
|
|
|
tangent.Normalize();
|
|
|
|
|
|
|
|
|
|
// Velocidad deseada
|
|
|
|
|
Vector2 desiredVelocity = tangent * angularVelocity * distanceToCenter;
|
|
|
|
|
bottle.Body.LinearVelocity = desiredVelocity;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class simDescarte : simBase
|
|
|
|
|
{
|
|
|
|
|
private float _radius;
|
2024-05-26 06:50:25 -03:00
|
|
|
|
private List<Action> _deferredActions;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
|
2024-05-26 06:50:25 -03:00
|
|
|
|
public simDescarte(World world, List<Action> deferredActions, float diameter, Vector2 position)
|
2024-05-25 09:38:36 -03:00
|
|
|
|
{
|
|
|
|
|
_world = world;
|
2024-05-26 06:50:25 -03:00
|
|
|
|
_deferredActions = deferredActions;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
_radius = diameter / 2;
|
|
|
|
|
Create(position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetDiameter(float diameter)
|
|
|
|
|
{
|
|
|
|
|
_radius = diameter / 2;
|
|
|
|
|
Create(Body.Position); // Recrear el círculo con el nuevo tamaño
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Create(Vector2 position)
|
|
|
|
|
{
|
|
|
|
|
RemoverBody();
|
|
|
|
|
Body = _world.CreateCircle(_radius, 1f, position);
|
|
|
|
|
|
|
|
|
|
Body.FixtureList[0].IsSensor = true;
|
|
|
|
|
Body.BodyType = BodyType.Static;
|
|
|
|
|
Body.Tag = this; // Importante para la identificación durante la colisión
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class simTransporte : simBase
|
|
|
|
|
{
|
|
|
|
|
public float Speed { get; set; } // Velocidad para efectos de cinta transportadora
|
2024-05-30 13:48:37 -03:00
|
|
|
|
public float Friction { get; set; } // Friccion para efectos de cinta transportadora
|
2024-05-25 09:38:36 -03:00
|
|
|
|
public float DistanceGuide2Guide { get; set; }
|
2024-05-26 06:50:25 -03:00
|
|
|
|
public bool isBrake { get; set; }
|
2024-05-25 09:38:36 -03:00
|
|
|
|
public bool TransportWithGuides = false;
|
2024-05-26 06:50:25 -03:00
|
|
|
|
private List<Action> _deferredActions;
|
2024-05-30 13:48:37 -03:00
|
|
|
|
public float Width { get; set; }
|
|
|
|
|
public float Height { get; set; }
|
2024-05-25 09:38:36 -03:00
|
|
|
|
|
2024-05-26 06:50:25 -03:00
|
|
|
|
public simTransporte(World world, List<Action> deferredActions, float width, float height, Vector2 position, float angle = 0)
|
2024-05-25 09:38:36 -03:00
|
|
|
|
{
|
|
|
|
|
_world = world;
|
2024-05-26 06:50:25 -03:00
|
|
|
|
_deferredActions = deferredActions;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Create(width, height, position, angle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float Angle
|
|
|
|
|
{
|
2024-05-31 10:06:49 -03:00
|
|
|
|
get { return RadianesAGrados(Body.Rotation); }
|
|
|
|
|
set { Body.Rotation = GradosARadianes(value); }
|
2024-05-25 09:38:36 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public new void SetPosition(float x, float y)
|
|
|
|
|
{
|
|
|
|
|
Body.Position = new Vector2(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetSpeed(float speed)
|
|
|
|
|
{
|
|
|
|
|
Speed = speed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetDimensions(float width, float height)
|
|
|
|
|
{
|
|
|
|
|
Body.Remove(Body.FixtureList[0]);
|
|
|
|
|
|
|
|
|
|
var newShape = new PolygonShape(PolygonTools.CreateRectangle(width / 2, height / 2), 1f);
|
|
|
|
|
Body.CreateFixture(newShape);
|
2024-05-30 13:48:37 -03:00
|
|
|
|
Width = width;
|
|
|
|
|
Height = height;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
}
|
|
|
|
|
public void Create(float width, float height, Vector2 position, float angle = 0)
|
|
|
|
|
{
|
|
|
|
|
RemoverBody();
|
2024-05-30 13:48:37 -03:00
|
|
|
|
Width = width;
|
|
|
|
|
Height = height;
|
|
|
|
|
Friction = 0.1f;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Body = _world.CreateRectangle( width, height, 1f, position);
|
|
|
|
|
Body.FixtureList[0].IsSensor = true;
|
|
|
|
|
Body.BodyType = BodyType.Static;
|
2024-05-31 10:06:49 -03:00
|
|
|
|
Body.Rotation = GradosARadianes(angle);
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Body.Tag = this; // Importante para la identificación durante la colisión
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class simBarrera : simBase
|
|
|
|
|
{
|
2024-05-30 13:48:37 -03:00
|
|
|
|
public float Distancia;
|
2024-05-27 05:34:20 -03:00
|
|
|
|
public int LuzCortada;
|
2024-05-30 13:48:37 -03:00
|
|
|
|
public bool LuzCortadaNeck;
|
|
|
|
|
public bool DetectNeck;
|
|
|
|
|
public List<simBotella> ListSimBotellaContact;
|
|
|
|
|
float _height;
|
|
|
|
|
|
2024-05-26 06:50:25 -03:00
|
|
|
|
private List<Action> _deferredActions;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
|
2024-05-30 13:48:37 -03:00
|
|
|
|
public simBarrera(World world, List<Action> deferredActions, float width, float height, Vector2 position, float angle = 0, bool detectectNeck = false)
|
2024-05-25 09:38:36 -03:00
|
|
|
|
{
|
|
|
|
|
_world = world;
|
2024-05-30 13:48:37 -03:00
|
|
|
|
_height = height;
|
|
|
|
|
DetectNeck = detectectNeck;
|
2024-05-26 06:50:25 -03:00
|
|
|
|
_deferredActions = deferredActions;
|
2024-05-30 13:48:37 -03:00
|
|
|
|
ListSimBotellaContact = new List<simBotella>();
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Create(width, height, position, angle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float Angle
|
|
|
|
|
{
|
2024-05-31 10:06:49 -03:00
|
|
|
|
get { return RadianesAGrados(Body.Rotation); }
|
|
|
|
|
set { Body.Rotation = GradosARadianes(value); }
|
2024-05-25 09:38:36 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public new void SetPosition(float x, float y)
|
|
|
|
|
{
|
|
|
|
|
Body.Position = new Vector2(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetDimensions(float width, float height)
|
|
|
|
|
{
|
|
|
|
|
Body.Remove(Body.FixtureList[0]);
|
|
|
|
|
|
|
|
|
|
var newShape = new PolygonShape(PolygonTools.CreateRectangle(width / 2, height / 2), 1f);
|
|
|
|
|
Body.CreateFixture(newShape);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 13:48:37 -03:00
|
|
|
|
public void Create(float width, float height, Vector2 position, float angle = 0, bool detectectNeck = false)
|
2024-05-25 09:38:36 -03:00
|
|
|
|
{
|
|
|
|
|
RemoverBody();
|
2024-05-30 13:48:37 -03:00
|
|
|
|
_height = height;
|
|
|
|
|
DetectNeck = detectectNeck;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Body = _world.CreateRectangle( width, height, 1f, position);
|
|
|
|
|
Body.FixtureList[0].IsSensor = true;
|
|
|
|
|
Body.BodyType = BodyType.Static;
|
2024-05-31 10:06:49 -03:00
|
|
|
|
Body.Rotation = GradosARadianes(angle);
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Body.Tag = this; // Importante para la identificación durante la colisión
|
2024-05-27 05:34:20 -03:00
|
|
|
|
LuzCortada = 0;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
}
|
2024-05-30 13:48:37 -03:00
|
|
|
|
|
|
|
|
|
public void CheckIfNecksIsTouching()
|
|
|
|
|
{
|
|
|
|
|
if (LuzCortada == 0) return;
|
|
|
|
|
|
|
|
|
|
foreach (var botella in ListSimBotellaContact)
|
|
|
|
|
{
|
|
|
|
|
// Obtener el centro de la barrera
|
|
|
|
|
Vector2 sensorCenter = Body.Position;
|
|
|
|
|
|
|
|
|
|
// Calcular el vector de la línea horizontal centrada de la barrera
|
|
|
|
|
float halfHeight = _height / 2;
|
|
|
|
|
float cos = (float)Math.Cos(Body.Rotation);
|
|
|
|
|
float sin = (float)Math.Sin(Body.Rotation);
|
|
|
|
|
|
|
|
|
|
// Calcular los puntos inicial y final de la línea horizontal centrada y rotada
|
|
|
|
|
Vector2 lineStart = sensorCenter + new Vector2(-halfHeight * cos, halfHeight * sin);
|
|
|
|
|
Vector2 lineEnd = sensorCenter + new Vector2(halfHeight * cos, -halfHeight * sin);
|
|
|
|
|
|
|
|
|
|
// Proyectar el centro de la botella sobre la línea horizontal
|
|
|
|
|
Vector2 fixtureCenter = botella.Body.Position;
|
|
|
|
|
Vector2 closestPoint = ProjectPointOntoLine(fixtureCenter, lineStart, lineEnd);
|
|
|
|
|
|
|
|
|
|
// Calcular la distancia entre el punto más cercano y el centro del cuello de la botella
|
|
|
|
|
float distance;
|
|
|
|
|
Vector2.Distance(ref closestPoint,ref fixtureCenter, out distance);
|
|
|
|
|
Distancia = distance;
|
|
|
|
|
if (distance <= botella._neckRadius)
|
|
|
|
|
{
|
|
|
|
|
LuzCortadaNeck = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
LuzCortadaNeck = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Vector2 ProjectPointOntoLine(Vector2 point, Vector2 lineStart, Vector2 lineEnd)
|
|
|
|
|
{
|
|
|
|
|
Vector2 lineDirection = lineEnd - lineStart;
|
|
|
|
|
lineDirection.Normalize();
|
|
|
|
|
|
|
|
|
|
Vector2 pointToLineStart = point - lineStart;
|
|
|
|
|
float projectionLength;
|
|
|
|
|
Vector2.Dot(ref pointToLineStart, ref lineDirection, out projectionLength);
|
|
|
|
|
|
|
|
|
|
return lineStart + projectionLength * lineDirection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-25 09:38:36 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class simGuia : simBase
|
|
|
|
|
{
|
2024-05-26 06:50:25 -03:00
|
|
|
|
private List<Action> _deferredActions;
|
|
|
|
|
public simGuia(World world, List<Action> deferredActions, Vector2 start, Vector2 end)
|
2024-05-25 09:38:36 -03:00
|
|
|
|
{
|
|
|
|
|
_world = world;
|
2024-05-26 06:50:25 -03:00
|
|
|
|
_deferredActions = deferredActions;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Create(start, end);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Create(Vector2 start, Vector2 end)
|
|
|
|
|
{
|
|
|
|
|
RemoverBody();
|
|
|
|
|
Body = _world.CreateEdge( start, end);
|
|
|
|
|
Body.BodyType = BodyType.Static;
|
|
|
|
|
Body.Tag = this; // Importante para la identificación durante la colisión
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateVertices(Vector2 newStart, Vector2 newEnd)
|
|
|
|
|
{
|
|
|
|
|
Create(newStart, newEnd); // Recrear la línea con nuevos vértices
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class simBotella : simBase
|
|
|
|
|
{
|
2024-05-30 13:48:37 -03:00
|
|
|
|
public float Radius;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
private float _mass;
|
|
|
|
|
public bool Descartar = false;
|
|
|
|
|
|
2024-05-26 06:50:25 -03:00
|
|
|
|
public int isOnTransports;
|
2024-05-30 13:48:37 -03:00
|
|
|
|
public List<simBase> ListOnTransports;
|
2024-05-26 06:50:25 -03:00
|
|
|
|
public bool isRestricted;
|
|
|
|
|
public bool isNoMoreRestricted;
|
|
|
|
|
public float OriginalMass;
|
|
|
|
|
public simTransporte ConveyorRestrictedTo;
|
|
|
|
|
public Fixture axisRestrictedBy;
|
2024-05-30 13:48:37 -03:00
|
|
|
|
|
|
|
|
|
public float OverlapPercentage;
|
|
|
|
|
|
|
|
|
|
public float _neckRadius;
|
2024-05-26 06:50:25 -03:00
|
|
|
|
|
|
|
|
|
PrismaticJoint _activeJoint;
|
|
|
|
|
private List<Action> _deferredActions;
|
|
|
|
|
|
2024-05-30 13:48:37 -03:00
|
|
|
|
public simBotella(World world, List<Action> deferredActions, float diameter, Vector2 position, float mass,float neckRadius = 0)
|
2024-05-25 09:38:36 -03:00
|
|
|
|
{
|
|
|
|
|
_world = world;
|
2024-05-30 13:48:37 -03:00
|
|
|
|
ListOnTransports = new List<simBase>();
|
2024-05-26 06:50:25 -03:00
|
|
|
|
_deferredActions = deferredActions;
|
2024-05-30 13:48:37 -03:00
|
|
|
|
Radius = diameter / 2;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
_mass = mass;
|
2024-05-26 06:50:25 -03:00
|
|
|
|
_activeJoint = null;
|
2024-05-30 13:48:37 -03:00
|
|
|
|
|
|
|
|
|
if (neckRadius<=0)
|
|
|
|
|
neckRadius = diameter / 4;
|
|
|
|
|
|
|
|
|
|
_neckRadius = neckRadius;
|
|
|
|
|
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Create(position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float CenterX
|
|
|
|
|
{
|
|
|
|
|
get { return Body.Position.X; }
|
|
|
|
|
set { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float CenterY
|
|
|
|
|
{
|
|
|
|
|
get { return Body.Position.Y; }
|
|
|
|
|
set { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vector2 Center
|
|
|
|
|
{
|
|
|
|
|
get { return Body.Position; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float Mass
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_mass <= 0)
|
|
|
|
|
_mass = 1;
|
|
|
|
|
return _mass;
|
|
|
|
|
}
|
|
|
|
|
set { _mass = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Create(Vector2 position)
|
|
|
|
|
{
|
|
|
|
|
RemoverBody();
|
2024-05-26 06:50:25 -03:00
|
|
|
|
isOnTransports = 0;
|
|
|
|
|
_activeJoint = null;
|
2024-05-30 13:48:37 -03:00
|
|
|
|
|
|
|
|
|
Body = _world.CreateCircle( Radius, 1f, position);
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Body.BodyType = BodyType.Dynamic;
|
|
|
|
|
|
|
|
|
|
// Restablecer manejador de eventos de colisión
|
|
|
|
|
Body.OnCollision += HandleCollision;
|
2024-05-26 06:50:25 -03:00
|
|
|
|
Body.OnSeparation += HandleOnSeparation;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
|
|
|
|
|
Body.Tag = this; // Importante para la identificación durante la colisión
|
|
|
|
|
|
|
|
|
|
// Configurar la fricción
|
2024-05-30 13:48:37 -03:00
|
|
|
|
Body.SetFriction(0.2f);
|
2024-05-25 09:38:36 -03:00
|
|
|
|
|
|
|
|
|
// Configurar amortiguamiento
|
2024-05-31 19:34:58 -03:00
|
|
|
|
Body.LinearDamping = 3f; // Ajustar para controlar la reducción de la velocidad lineal
|
2024-05-26 06:50:25 -03:00
|
|
|
|
Body.AngularDamping = 1f; // Ajustar para controlar la reducción de la velocidad angular
|
2024-05-30 13:48:37 -03:00
|
|
|
|
Body.SetRestitution(0f); // Baja restitución para menos rebote
|
2024-05-25 09:38:36 -03:00
|
|
|
|
|
2024-05-26 06:50:25 -03:00
|
|
|
|
Body.SleepingAllowed = false;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Body.IsBullet = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetDiameter(float diameter)
|
|
|
|
|
{
|
2024-05-30 13:48:37 -03:00
|
|
|
|
Radius = diameter / 2;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Create(Body.Position); // Recrear el círculo con el nuevo tamaño
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetMass(float mass)
|
|
|
|
|
{
|
|
|
|
|
Mass = mass;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-26 06:50:25 -03:00
|
|
|
|
private bool HandleCollision(Fixture fixtureA, Fixture fixtureB, Contact contact)
|
2024-05-25 09:38:36 -03:00
|
|
|
|
{
|
|
|
|
|
if (fixtureB.Body.Tag is simBarrera Sensor)
|
|
|
|
|
{
|
2024-05-27 05:34:20 -03:00
|
|
|
|
Sensor.LuzCortada += 1;
|
2024-05-30 13:48:37 -03:00
|
|
|
|
Sensor.ListSimBotellaContact.Add(this);
|
2024-05-25 09:38:36 -03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (fixtureB.Body.Tag is simCurve curve)
|
|
|
|
|
{
|
2024-05-30 13:48:37 -03:00
|
|
|
|
isOnTransports += 1;
|
|
|
|
|
ListOnTransports.Add(curve);
|
2024-05-25 09:38:36 -03:00
|
|
|
|
return true; // No aplicar respuestas físicas
|
|
|
|
|
}
|
|
|
|
|
else if (fixtureB.Body.Tag is simDescarte)
|
|
|
|
|
{
|
|
|
|
|
Descartar = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (fixtureB.Body.Tag is simTransporte)
|
|
|
|
|
{
|
|
|
|
|
simTransporte conveyor = fixtureB.Body.Tag as simTransporte;
|
|
|
|
|
|
2024-05-26 06:50:25 -03:00
|
|
|
|
isOnTransports += 1;
|
2024-05-27 05:34:20 -03:00
|
|
|
|
ListOnTransports.Add(conveyor);
|
2024-05-26 06:50:25 -03:00
|
|
|
|
|
2024-05-27 05:34:20 -03:00
|
|
|
|
// Aplicar el efecto del transportador usando el porcentaje calculado
|
|
|
|
|
if (conveyor.TransportWithGuides && conveyor.isBrake)
|
2024-05-25 09:38:36 -03:00
|
|
|
|
{
|
2024-05-27 05:34:20 -03:00
|
|
|
|
ConveyorRestrictedTo = conveyor;
|
|
|
|
|
axisRestrictedBy = fixtureB;
|
|
|
|
|
OriginalMass = Body.Mass;
|
|
|
|
|
isRestricted = true;
|
|
|
|
|
isNoMoreRestricted = false;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
}
|
|
|
|
|
return true; // No aplicar respuestas físicas
|
|
|
|
|
}
|
|
|
|
|
return true; // No aplicar respuestas físicas
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 13:48:37 -03:00
|
|
|
|
private float CalculateOverlapPercentage(simTransporte conveyor)
|
2024-05-26 06:50:25 -03:00
|
|
|
|
{
|
2024-05-30 13:48:37 -03:00
|
|
|
|
CircleShape circleShape = Body.FixtureList[0].Shape as CircleShape;
|
|
|
|
|
PolygonShape polygonShape = conveyor.Body.FixtureList[0].Shape as PolygonShape;
|
2024-05-26 06:50:25 -03:00
|
|
|
|
|
2024-05-30 13:48:37 -03:00
|
|
|
|
// Obtener centro y radio del círculo
|
|
|
|
|
Vector2 centroCirculo = Body.Position;
|
|
|
|
|
float radio = circleShape.Radius;
|
2024-05-26 06:50:25 -03:00
|
|
|
|
|
2024-05-30 13:48:37 -03:00
|
|
|
|
// Obtener los vértices del polígono (rectángulo)
|
|
|
|
|
Vector2[] vertices = new Vector2[polygonShape.Vertices.Count];
|
|
|
|
|
float cos = (float)Math.Cos(conveyor.Body.Rotation);
|
|
|
|
|
float sin = (float)Math.Sin(conveyor.Body.Rotation);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < polygonShape.Vertices.Count; i++)
|
2024-05-26 06:50:25 -03:00
|
|
|
|
{
|
2024-05-30 13:48:37 -03:00
|
|
|
|
Vector2 vertex = polygonShape.Vertices[i];
|
|
|
|
|
float rotatedX = vertex.X * cos - vertex.Y * sin + conveyor.Body.Position.X;
|
|
|
|
|
float rotatedY = vertex.X * sin + vertex.Y * cos + conveyor.Body.Position.Y;
|
|
|
|
|
vertices[i] = new Vector2(rotatedX, rotatedY);
|
2024-05-26 06:50:25 -03:00
|
|
|
|
}
|
2024-05-30 13:48:37 -03:00
|
|
|
|
|
|
|
|
|
// Calcular el porcentaje de la superficie compartida
|
|
|
|
|
return InterseccionCirculoRectangulo.CalcularSuperficieCompartida(vertices, centroCirculo, radio);
|
2024-05-26 06:50:25 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleOnSeparation(Fixture sender, Fixture fixtureB, Contact contact)
|
|
|
|
|
{
|
2024-05-27 05:34:20 -03:00
|
|
|
|
if (isOnTransports > 0 && fixtureB.Body.Tag is simTransporte)
|
|
|
|
|
{
|
|
|
|
|
if (fixtureB.Body.Tag is simTransporte transport)
|
|
|
|
|
ListOnTransports.Remove(transport);
|
2024-05-26 06:50:25 -03:00
|
|
|
|
isOnTransports -= 1;
|
2024-05-27 05:34:20 -03:00
|
|
|
|
}
|
2024-05-30 13:48:37 -03:00
|
|
|
|
if (isOnTransports > 0 && fixtureB.Body.Tag is simCurve)
|
|
|
|
|
{
|
|
|
|
|
if (fixtureB.Body.Tag is simCurve transport)
|
|
|
|
|
ListOnTransports.Remove(transport);
|
|
|
|
|
isOnTransports -= 1;
|
|
|
|
|
}
|
2024-05-26 06:50:25 -03:00
|
|
|
|
if (isRestricted && fixtureB == axisRestrictedBy)
|
|
|
|
|
{
|
|
|
|
|
isRestricted = false;
|
|
|
|
|
isNoMoreRestricted = true;
|
|
|
|
|
}
|
2024-05-27 05:34:20 -03:00
|
|
|
|
if (fixtureB.Body.Tag is simBarrera Sensor)
|
2024-05-30 13:48:37 -03:00
|
|
|
|
{
|
2024-05-27 05:34:20 -03:00
|
|
|
|
Sensor.LuzCortada -= 1;
|
2024-05-30 13:48:37 -03:00
|
|
|
|
Sensor.ListSimBotellaContact.Remove(this);
|
|
|
|
|
}
|
2024-05-26 06:50:25 -03:00
|
|
|
|
}
|
2024-05-30 13:48:37 -03:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Aplica la fuerza de traccion a la botellas segun los
|
|
|
|
|
/// transportes sobre los que se encuentra
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="deltaTime_s"></param>
|
|
|
|
|
public void ApplyConveyorSpeed(float deltaTime_s)
|
2024-05-27 05:34:20 -03:00
|
|
|
|
{
|
2024-05-30 13:48:37 -03:00
|
|
|
|
foreach (var transporte in ListOnTransports)
|
2024-05-27 05:34:20 -03:00
|
|
|
|
{
|
2024-05-30 13:48:37 -03:00
|
|
|
|
if (transporte is simTransporte conveyorRect)
|
|
|
|
|
if (ApplyConveyorEffect(deltaTime_s, conveyorRect))
|
|
|
|
|
break;
|
|
|
|
|
if (transporte is simCurve conveyorCurve)
|
|
|
|
|
conveyorCurve.ApplyCurveEffect(Body.FixtureList[0]);
|
2024-05-27 05:34:20 -03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 13:48:37 -03:00
|
|
|
|
private bool ApplyConveyorEffect(float deltaTime_s, simTransporte conveyor)
|
2024-05-25 09:38:36 -03:00
|
|
|
|
{
|
2024-05-30 13:48:37 -03:00
|
|
|
|
// Calcular el porcentaje de superficie sobrepuesta
|
|
|
|
|
float overlapPercentage = CalculateOverlapedArea(this, conveyor); // CalculateOverlapPercentage(conveyor);
|
|
|
|
|
OverlapPercentage = overlapPercentage;
|
|
|
|
|
|
|
|
|
|
// Calcular la velocidad deseada del transporte
|
2024-05-25 09:38:36 -03:00
|
|
|
|
float speedMetersPerSecond = conveyor.Speed / 60.0f;
|
|
|
|
|
Vector2 desiredVelocity = new Vector2((float)Math.Cos(conveyor.Body.Rotation), (float)Math.Sin(conveyor.Body.Rotation)) * speedMetersPerSecond;
|
2024-05-30 13:48:37 -03:00
|
|
|
|
|
|
|
|
|
// Obtener la velocidad actual de la botella
|
|
|
|
|
Vector2 currentVelocity = Body.LinearVelocity;
|
|
|
|
|
|
|
|
|
|
// Calcular la diferencia de velocidad deseada
|
|
|
|
|
Vector2 velocityDifference = desiredVelocity - currentVelocity;
|
|
|
|
|
|
|
|
|
|
// Calcular la fuerza de fricción necesaria - 0 : ya esta en velocidad - 1 : esta detenido
|
|
|
|
|
float proporcionalVelocityNeeded = 1-CalculateProportion(currentVelocity, desiredVelocity);
|
|
|
|
|
float frictionCoefficient;
|
|
|
|
|
|
|
|
|
|
switch (proporcionalVelocityNeeded) {
|
|
|
|
|
case > 0.3f:
|
|
|
|
|
frictionCoefficient = conveyor.Friction * overlapPercentage;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
frictionCoefficient = proporcionalVelocityNeeded * overlapPercentage;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isRestricted && conveyor == ConveyorRestrictedTo && overlapPercentage > 0.5f)
|
|
|
|
|
{
|
|
|
|
|
Body.LinearVelocity = desiredVelocity;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
// Aplicar la fuerza de fricción en función del porcentaje de superficie sobrepuesta
|
|
|
|
|
Body.LinearVelocity += frictionCoefficient * desiredVelocity;
|
|
|
|
|
//Body.ApplyForce(frictionForce * overlapPercentage);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float CalculateOverlapedArea(simBotella botella, simTransporte conveyor)
|
|
|
|
|
{
|
|
|
|
|
//float areaBotella = (float) ((botella.Radius * botella.Radius * Math.PI) / (Math.PI / 4)); // Math.PI/4 porque es un cuadrado en vez de un circulo
|
|
|
|
|
//float area = OverlapedArea.CalculateOverlapedArea(botella.Body, conveyor.Body);
|
|
|
|
|
//if (areaBotella == 0) return 0;
|
|
|
|
|
//return area/areaBotella;
|
|
|
|
|
return OverlapedAreaFast.CalculateOverlapedArea(botella, conveyor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float CalculateProportion(Vector2 currentVelocity, Vector2 desiredVelocity)
|
|
|
|
|
{
|
|
|
|
|
// Calcular la proyección escalar de v2 sobre v1
|
|
|
|
|
float dotProduct;
|
|
|
|
|
Vector2.Dot(ref desiredVelocity, ref currentVelocity, out dotProduct);
|
|
|
|
|
float magnitudeV1Squared = desiredVelocity.LengthSquared();
|
|
|
|
|
|
|
|
|
|
// Si la magnitud de v1 es 0, la proporción no está definida
|
|
|
|
|
if (magnitudeV1Squared == 0)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calcular la proporción
|
|
|
|
|
float proportion = dotProduct / magnitudeV1Squared;
|
|
|
|
|
return proportion;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-26 06:50:25 -03:00
|
|
|
|
public void CenterFixtureOnConveyor()
|
2024-05-25 09:38:36 -03:00
|
|
|
|
{
|
2024-05-26 06:50:25 -03:00
|
|
|
|
if (!isRestricted || ConveyorRestrictedTo == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2024-05-25 09:38:36 -03:00
|
|
|
|
// Obtener el centro del conveyor
|
2024-05-26 06:50:25 -03:00
|
|
|
|
Vector2 conveyorCenter = ConveyorRestrictedTo.Body.Position;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
|
|
|
|
|
// Calcular el vector de la línea horizontal centrada de conveyor
|
2024-05-26 06:50:25 -03:00
|
|
|
|
float halfDistance = ConveyorRestrictedTo.DistanceGuide2Guide / 2;
|
|
|
|
|
float cos = (float)Math.Cos(ConveyorRestrictedTo.Body.Rotation);
|
|
|
|
|
float sin = (float)Math.Sin(ConveyorRestrictedTo.Body.Rotation);
|
2024-05-25 09:38:36 -03:00
|
|
|
|
|
|
|
|
|
Vector2 offset = new Vector2(halfDistance * cos, halfDistance * sin);
|
|
|
|
|
|
|
|
|
|
// Línea horizontal centrada de conveyor en el espacio del mundo
|
|
|
|
|
Vector2 lineStart = conveyorCenter - offset;
|
|
|
|
|
Vector2 lineEnd = conveyorCenter + offset;
|
|
|
|
|
|
|
|
|
|
// Proyectar el centro de fixtureA sobre la línea horizontal
|
2024-05-26 06:50:25 -03:00
|
|
|
|
Vector2 fixtureCenter = Body.Position;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Vector2 closestPoint = ProjectPointOntoLine(fixtureCenter, lineStart, lineEnd);
|
|
|
|
|
|
|
|
|
|
// Mover fixtureA al punto más cercano en la línea horizontal
|
2024-05-26 06:50:25 -03:00
|
|
|
|
Body.Position = closestPoint;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Vector2 ProjectPointOntoLine(Vector2 point, Vector2 lineStart, Vector2 lineEnd)
|
|
|
|
|
{
|
|
|
|
|
Vector2 lineDirection = lineEnd - lineStart;
|
|
|
|
|
lineDirection.Normalize();
|
|
|
|
|
|
|
|
|
|
Vector2 pointToLineStart = point - lineStart;
|
|
|
|
|
Vector2.Dot(ref pointToLineStart,ref lineDirection, out float projectionLength);
|
|
|
|
|
|
|
|
|
|
return lineStart + projectionLength * lineDirection;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SimulationManagerFP
|
|
|
|
|
{
|
|
|
|
|
private World world;
|
|
|
|
|
private Canvas simulationCanvas;
|
|
|
|
|
public List<simBase> Cuerpos;
|
2024-05-26 06:50:25 -03:00
|
|
|
|
public List<Action> _deferredActions;
|
2024-05-27 05:34:20 -03:00
|
|
|
|
SolverIterations Iterations;
|
2024-05-25 09:38:36 -03:00
|
|
|
|
|
|
|
|
|
private Stopwatch stopwatch;
|
|
|
|
|
private double stopwatch_last;
|
|
|
|
|
|
|
|
|
|
public Canvas DebugCanvas { get => simulationCanvas; set => simulationCanvas = value; }
|
|
|
|
|
|
|
|
|
|
public SimulationManagerFP()
|
|
|
|
|
{
|
|
|
|
|
world = new World(new Vector2(0, 0)); // Vector2.Zero
|
2024-05-27 05:34:20 -03:00
|
|
|
|
|
|
|
|
|
SolverIterations Iterations = new SolverIterations();
|
|
|
|
|
Iterations.PositionIterations = 1;
|
|
|
|
|
Iterations.VelocityIterations = 1;
|
|
|
|
|
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Cuerpos = new List<simBase>();
|
2024-05-26 06:50:25 -03:00
|
|
|
|
_deferredActions = new List<Action>();
|
2024-05-25 09:38:36 -03:00
|
|
|
|
stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
if (world.BodyList.Count > 0)
|
|
|
|
|
world.Clear();
|
|
|
|
|
if (Cuerpos.Count > 0)
|
|
|
|
|
Cuerpos.Clear();
|
|
|
|
|
}
|
2024-05-26 06:50:25 -03:00
|
|
|
|
// ******************************************************************************************************************************************
|
|
|
|
|
// ******************************************************************************************************************************************
|
2024-05-25 09:38:36 -03:00
|
|
|
|
|
|
|
|
|
public void Step()
|
|
|
|
|
{
|
|
|
|
|
// Detener el cronómetro y obtener el tiempo transcurrido en milisegundos
|
|
|
|
|
float elapsedMilliseconds = (float)(stopwatch.Elapsed.TotalMilliseconds - stopwatch_last);
|
|
|
|
|
stopwatch_last = stopwatch.Elapsed.TotalMilliseconds;
|
|
|
|
|
|
|
|
|
|
// Pasar el tiempo transcurrido al método Step
|
|
|
|
|
world.Step(elapsedMilliseconds / 1000.0f);
|
2024-05-27 05:34:20 -03:00
|
|
|
|
|
2024-05-26 06:50:25 -03:00
|
|
|
|
foreach (var cuerpo in Cuerpos)
|
|
|
|
|
{
|
|
|
|
|
if (cuerpo is simBotella botella)
|
|
|
|
|
{
|
2024-05-30 13:48:37 -03:00
|
|
|
|
botella.ApplyConveyorSpeed(elapsedMilliseconds/1000);
|
2024-05-27 05:34:20 -03:00
|
|
|
|
|
2024-05-26 06:50:25 -03:00
|
|
|
|
if (botella.isRestricted)
|
|
|
|
|
{
|
2024-05-30 13:48:37 -03:00
|
|
|
|
botella.CenterFixtureOnConveyor();
|
2024-05-26 06:50:25 -03:00
|
|
|
|
botella.Body.Inertia = 0;
|
2024-05-30 13:48:37 -03:00
|
|
|
|
botella.Body.Mass = 100;
|
|
|
|
|
}
|
|
|
|
|
else if (botella.isNoMoreRestricted)
|
2024-05-26 06:50:25 -03:00
|
|
|
|
{
|
|
|
|
|
botella.isNoMoreRestricted = false;
|
|
|
|
|
botella.Body.Mass = botella.OriginalMass;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-30 13:48:37 -03:00
|
|
|
|
else if (cuerpo is simBarrera barrera)
|
|
|
|
|
barrera.CheckIfNecksIsTouching();
|
2024-05-26 06:50:25 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Procesa las acciones diferidas
|
|
|
|
|
foreach (var action in _deferredActions)
|
|
|
|
|
{
|
|
|
|
|
action();
|
|
|
|
|
}
|
|
|
|
|
_deferredActions.Clear();
|
2024-05-25 09:38:36 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Remove(simBase Objeto)
|
|
|
|
|
{
|
|
|
|
|
if (Objeto != null)
|
|
|
|
|
{
|
|
|
|
|
Objeto.RemoverBody();
|
|
|
|
|
Cuerpos.Remove(Objeto);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public simCurve AddCurve(float innerRadius, float outerRadius, float startAngle, float endAngle, Vector2 position)
|
|
|
|
|
{
|
2024-05-26 06:50:25 -03:00
|
|
|
|
simCurve curva = new simCurve(world, _deferredActions, innerRadius, outerRadius, startAngle, endAngle, position);
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Cuerpos.Add(curva);
|
|
|
|
|
return curva;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public simBotella AddCircle(float diameter, Vector2 position, float mass)
|
|
|
|
|
{
|
2024-05-26 06:50:25 -03:00
|
|
|
|
simBotella circle = new simBotella(world, _deferredActions, diameter, position, mass);
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Cuerpos.Add(circle);
|
|
|
|
|
return circle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public simTransporte AddRectangle(float width, float height, Vector2 position, float angle)
|
|
|
|
|
{
|
2024-05-26 06:50:25 -03:00
|
|
|
|
simTransporte rectangle = new simTransporte(world, _deferredActions, width, height, position, angle);
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Cuerpos.Add(rectangle);
|
|
|
|
|
return rectangle;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 13:48:37 -03:00
|
|
|
|
public simBarrera AddBarrera(float width, float height, Vector2 position, float angle, bool detectarCuello)
|
2024-05-25 09:38:36 -03:00
|
|
|
|
{
|
2024-05-30 13:48:37 -03:00
|
|
|
|
simBarrera rectangle = new simBarrera(world, _deferredActions, width, height, position, angle, detectarCuello);
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Cuerpos.Add(rectangle);
|
|
|
|
|
return rectangle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public simGuia AddLine(Vector2 start, Vector2 end)
|
|
|
|
|
{
|
2024-05-26 06:50:25 -03:00
|
|
|
|
simGuia line = new simGuia(world, _deferredActions, start, end);
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Cuerpos.Add(line);
|
|
|
|
|
return line;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public simDescarte AddDescarte(float diameter, Vector2 position)
|
|
|
|
|
{
|
2024-05-26 06:50:25 -03:00
|
|
|
|
simDescarte descarte = new simDescarte(world, _deferredActions, diameter, position);
|
2024-05-25 09:38:36 -03:00
|
|
|
|
Cuerpos.Add(descarte);
|
|
|
|
|
return descarte;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Debug_DrawInitialBodies()
|
|
|
|
|
{
|
|
|
|
|
Debug_ClearSimulationShapes();
|
|
|
|
|
world.Step(0.01f); // Para actualizar la BodyList
|
|
|
|
|
foreach (Body body in world.BodyList)
|
|
|
|
|
{
|
|
|
|
|
foreach (Fixture fixture in body.FixtureList)
|
|
|
|
|
{
|
|
|
|
|
DrawShape(fixture);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Debug_ClearSimulationShapes()
|
|
|
|
|
{
|
|
|
|
|
var simulationShapes = simulationCanvas.Children.OfType<System.Windows.Shapes.Shape>().Where(s => s.Tag as string == "Simulation").ToList();
|
|
|
|
|
foreach (var shape in simulationShapes)
|
|
|
|
|
{
|
|
|
|
|
simulationCanvas.Children.Remove(shape);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DrawShape(Fixture fixture)
|
|
|
|
|
{
|
|
|
|
|
System.Windows.Shapes.Shape shape;
|
|
|
|
|
switch (fixture.Shape.ShapeType)
|
|
|
|
|
{
|
|
|
|
|
case ShapeType.Circle:
|
|
|
|
|
shape = DrawCircle(fixture);
|
|
|
|
|
break;
|
|
|
|
|
case ShapeType.Polygon:
|
|
|
|
|
shape = DrawPolygon(fixture);
|
|
|
|
|
break;
|
|
|
|
|
case ShapeType.Edge:
|
|
|
|
|
shape = DrawEdge(fixture);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
shape.Tag = "Simulation"; // Marcar para simulación
|
|
|
|
|
Canvas.SetZIndex(shape, 20);
|
|
|
|
|
simulationCanvas.Children.Add(shape);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float p(float x)
|
|
|
|
|
{
|
|
|
|
|
float c = PixelToMeter.Instance.calc.MetersToPixels(x);
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private System.Windows.Shapes.Shape DrawEdge(Fixture fixture)
|
|
|
|
|
{
|
|
|
|
|
EdgeShape edge = fixture.Shape as EdgeShape;
|
|
|
|
|
Line line = new Line
|
|
|
|
|
{
|
|
|
|
|
X1 = p(edge.Vertex1.X + fixture.Body.Position.X), // Aplicar escala y posición
|
|
|
|
|
Y1 = p(edge.Vertex1.Y + fixture.Body.Position.Y),
|
|
|
|
|
X2 = p(edge.Vertex2.X + fixture.Body.Position.X),
|
|
|
|
|
Y2 = p(edge.Vertex2.Y + fixture.Body.Position.Y),
|
|
|
|
|
Stroke = Brushes.Black,
|
|
|
|
|
StrokeThickness = 2
|
|
|
|
|
};
|
|
|
|
|
return line;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private System.Windows.Shapes.Shape DrawCircle(Fixture fixture)
|
|
|
|
|
{
|
|
|
|
|
CircleShape circle = fixture.Shape as CircleShape;
|
|
|
|
|
Ellipse ellipse = new Ellipse
|
|
|
|
|
{
|
|
|
|
|
Width = p(circle.Radius * 2), // Escalado para visualización
|
|
|
|
|
Height = p(circle.Radius * 2), // Escalado para visualización
|
|
|
|
|
Stroke = Brushes.Black,
|
|
|
|
|
StrokeThickness = 2
|
|
|
|
|
};
|
|
|
|
|
Canvas.SetLeft(ellipse, p(fixture.Body.Position.X - circle.Radius));
|
|
|
|
|
Canvas.SetTop(ellipse, p(fixture.Body.Position.Y - circle.Radius));
|
|
|
|
|
return ellipse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private System.Windows.Shapes.Shape DrawPolygon(Fixture fixture)
|
|
|
|
|
{
|
|
|
|
|
Polygon polygon = new Polygon { Stroke = Brushes.Black, StrokeThickness = 2 };
|
|
|
|
|
PolygonShape polyShape = fixture.Shape as PolygonShape;
|
|
|
|
|
|
|
|
|
|
float cos = (float)Math.Cos(fixture.Body.Rotation);
|
|
|
|
|
float sin = (float)Math.Sin(fixture.Body.Rotation);
|
|
|
|
|
|
|
|
|
|
foreach (Vector2 vertex in polyShape.Vertices)
|
|
|
|
|
{
|
|
|
|
|
float rotatedX = vertex.X * cos - vertex.Y * sin + fixture.Body.Position.X;
|
|
|
|
|
float rotatedY = vertex.X * sin + vertex.Y * cos + fixture.Body.Position.Y;
|
|
|
|
|
|
|
|
|
|
polygon.Points.Add(new Point(p(rotatedX), p(rotatedY)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return polygon;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|