From 9415967d16ad3dffa57565363e3d93177686413c Mon Sep 17 00:00:00 2001 From: se5a Date: Sun, 22 Dec 2024 06:01:48 +1300 Subject: [PATCH] New Bezier warp line. --- .../Rendering/Icons/DrawPrimitive.cs | 21 +++++++++- .../Rendering/Icons/WarpMovingIcon.cs | 42 +++++++++++++++++-- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/Pulsar4X/Pulsar4X.Client/Rendering/Icons/DrawPrimitive.cs b/Pulsar4X/Pulsar4X.Client/Rendering/Icons/DrawPrimitive.cs index 1b660c4fa..8436bd9d5 100644 --- a/Pulsar4X/Pulsar4X.Client/Rendering/Icons/DrawPrimitive.cs +++ b/Pulsar4X/Pulsar4X.Client/Rendering/Icons/DrawPrimitive.cs @@ -644,7 +644,26 @@ public static Orbital.Vector2[] CreateArrow(int len = 32) return arrowPoints; } - + public static Vector2[] BezierPoints(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float dt) + { + List _linePoints = new List(); + for (float t = 0.0f; t < 1.0; t += dt) + { + var x = BezCalc(t, p0.X, p1.X, p2.X, p3.X); + var y = BezCalc(t, p0.Y, p1.Y, p2.Y, p3.Y); + + _linePoints.Add(new Vector2() {X = x, Y = y}); + } + return _linePoints.ToArray(); + } + private static double BezCalc(double t, double a0, double a1, double a2, double a3) + { + double foo = a0 * Math.Pow((1 - t), 3); + foo += a1 * 3 * t * Math.Pow((1 - t), 2); + foo += a2 * 3 * Math.Pow(t, 2) * (1 - t); + foo += a3 * Math.Pow(t, 3); + return foo; + } } diff --git a/Pulsar4X/Pulsar4X.Client/Rendering/Icons/WarpMovingIcon.cs b/Pulsar4X/Pulsar4X.Client/Rendering/Icons/WarpMovingIcon.cs index d3a3abdce..751808654 100644 --- a/Pulsar4X/Pulsar4X.Client/Rendering/Icons/WarpMovingIcon.cs +++ b/Pulsar4X/Pulsar4X.Client/Rendering/Icons/WarpMovingIcon.cs @@ -15,11 +15,18 @@ public class WarpMovingIcon : Icon private Vector3 _currentRelativeEndPoint = new Vector3(); private PositionDB? _targetParentPos; + private Vector2 _bzsp; + private Vector2 _bzsp2; + private Vector2 _bzep; + private Vector2 _bzep2; + public byte Red = 255; public byte Grn = 255; public byte Blu = 0; byte alpha = 100; - SDL.SDL_Point[] _drawPoints = new SDL.SDL_Point[2]; + //SDL.SDL_Point[] _drawPoints = new SDL.SDL_Point[2]; + private Vector2[] _bezierCurve; + SDL.SDL_Point[] _bezierDrawPoints = new SDL.SDL_Point[10]; public WarpMovingIcon(WarpMovingDB warpMovingDB, PositionDB positionDB): base(new Vector3()) { _translateStartPoint = warpMovingDB.EntryPointAbsolute; @@ -27,6 +34,7 @@ public WarpMovingIcon(WarpMovingDB warpMovingDB, PositionDB positionDB): base(ne _relativeEndPoint = warpMovingDB.ExitPointrelative; _targetParentPos = warpMovingDB.GetTargetPosDB; _positionDB = positionDB; + this.OnPhysicsUpdate(); } public override void OnPhysicsUpdate() @@ -34,11 +42,30 @@ public override void OnPhysicsUpdate() _currentPosition = _positionDB.AbsolutePosition; if(_targetParentPos != null) _currentRelativeEndPoint = _targetParentPos.AbsolutePosition + _relativeEndPoint; + + Vector2 spos = (Vector2)_currentPosition; + Vector2 rpos = (Vector2)_currentRelativeEndPoint; + Vector2 epos = (Vector2)_translateEndPoint; + var ang = Angle.RadiansFromVector2(spos - epos); + var deg = Angle.ToDegrees(ang); + var range = (spos - epos).Length(); + var spMult = range * 0.7; + var epMult = range * 0.3; + _bzsp = new Vector2(spos.X, spos.Y); + _bzsp2 = spos - Angle.PositionFromAngle(ang, spMult); + _bzep2 = rpos + Angle.PositionFromAngle(ang, epMult); + _bzep = new Vector2(rpos.X, rpos.Y); + + _bezierCurve = CreatePrimitiveShapes.BezierPoints(_bzsp, _bzsp2, _bzep2, _bzep, 0.05f); + if(_bezierDrawPoints.Length != _bezierCurve.Length) + _bezierDrawPoints = new SDL.SDL_Point[_bezierCurve.Length]; + } public override void OnFrameUpdate(Matrix matrix, Camera camera) { ViewScreenPos = camera.ViewCoordinate_m(WorldPosition_m); + /* _drawPoints = new SDL.SDL_Point[3]; var spos = camera.ViewCoordinateV2_m(_currentPosition); @@ -49,6 +76,12 @@ public override void OnFrameUpdate(Matrix matrix, Camera camera) var rpos = camera.ViewCoordinateV2_m(_currentRelativeEndPoint); _drawPoints[2] = new SDL.SDL_Point(){x = (int)rpos.X, y = (int)rpos.Y}; + */ + for (int index = 0; index < _bezierCurve.Length; index++) + { + var pos = camera.ViewCoordinateV2_m(_bezierCurve[index]); + _bezierDrawPoints[index] = new SDL.SDL_Point(){x = Convert.ToInt32(pos.X), y = Convert.ToInt32(pos.Y)}; + } } @@ -56,9 +89,12 @@ public override void Draw(IntPtr rendererPtr, Camera camera) { SDL.SDL_SetRenderDrawColor(rendererPtr, Red, Grn, Blu, alpha); - SDL.SDL_RenderDrawLine(rendererPtr, _drawPoints[0].x, _drawPoints[0].y, _drawPoints[1].x, _drawPoints[1].y); - SDL.SDL_RenderDrawLine(rendererPtr, _drawPoints[0].x, _drawPoints[0].y, _drawPoints[2].x, _drawPoints[2].y); + //SDL.SDL_RenderDrawLine(rendererPtr, _drawPoints[0].x, _drawPoints[0].y, _drawPoints[1].x, _drawPoints[1].y); + //SDL.SDL_RenderDrawLine(rendererPtr, _drawPoints[0].x, _drawPoints[0].y, _drawPoints[2].x, _drawPoints[2].y); + + SDL.SDL_RenderDrawLines(rendererPtr, _bezierDrawPoints, _bezierDrawPoints.Length ); + } } }