Skip to content

Commit

Permalink
New Bezier warp line.
Browse files Browse the repository at this point in the history
  • Loading branch information
se5a committed Dec 21, 2024
1 parent 24b8ff8 commit 9415967
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
21 changes: 20 additions & 1 deletion Pulsar4X/Pulsar4X.Client/Rendering/Icons/DrawPrimitive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vector2> _linePoints = new List<Vector2>();
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;
}

}

Expand Down
42 changes: 39 additions & 3 deletions Pulsar4X/Pulsar4X.Client/Rendering/Icons/WarpMovingIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,57 @@ 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;
_translateEndPoint = warpMovingDB.ExitPointAbsolute;
_relativeEndPoint = warpMovingDB.ExitPointrelative;
_targetParentPos = warpMovingDB.GetTargetPosDB;
_positionDB = positionDB;
this.OnPhysicsUpdate();
}

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);
Expand All @@ -49,16 +76,25 @@ 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)};
}
}


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

}
}
}

0 comments on commit 9415967

Please sign in to comment.