Skip to content

Commit

Permalink
refactor: float[2] -> RcVec2f
Browse files Browse the repository at this point in the history
  • Loading branch information
ikpil committed Oct 12, 2023
1 parent 61a2132 commit adfded5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
6 changes: 6 additions & 0 deletions src/DotRecast.Core/RcVec2f.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ public struct RcVec2f

public static RcVec2f Zero { get; } = new RcVec2f { X = 0, Y = 0 };

public RcVec2f(float x, float y)
{
X = x;
Y = y;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Get(int idx)
{
Expand Down
6 changes: 3 additions & 3 deletions src/DotRecast.Recast.Demo/Draw/DebugDraw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -636,10 +636,10 @@ public RcMatrix4x4f ProjectionMatrix(float fovy, float aspect, float near, float
return _projectionMatrix;
}

public RcMatrix4x4f ViewMatrix(RcVec3f cameraPos, float[] cameraEulers)
public RcMatrix4x4f ViewMatrix(RcVec3f cameraPos, RcVec2f cameraEulers)
{
var rx = RcMatrix4x4f.CreateFromRotate(cameraEulers[0], 1, 0, 0);
var ry = RcMatrix4x4f.CreateFromRotate(cameraEulers[1], 0, 1, 0);
var rx = RcMatrix4x4f.CreateFromRotate(cameraEulers.X, 1, 0, 0);
var ry = RcMatrix4x4f.CreateFromRotate(cameraEulers.Y, 0, 1, 0);
var r = RcMatrix4x4f.Mul(ref rx, ref ry);

var t = new RcMatrix4x4f();
Expand Down
41 changes: 18 additions & 23 deletions src/DotRecast.Recast.Demo/RecastDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,19 @@ public class RecastDemo : IRecastDemoChannel
private bool processHitTestShift;
private int _modState;

private readonly float[] mousePos = new float[2];
private RcVec2f mousePos = new RcVec2f();

private bool _mouseOverMenu;
private bool pan;
private bool movedDuringPan;

Check warning on line 85 in src/DotRecast.Recast.Demo/RecastDemo.cs

View workflow job for this annotation

GitHub Actions / build-and-test-ubuntu-latest

The field 'RecastDemo.movedDuringPan' is assigned but its value is never used

Check warning on line 85 in src/DotRecast.Recast.Demo/RecastDemo.cs

View workflow job for this annotation

GitHub Actions / build-and-test-ubuntu-latest

The field 'RecastDemo.movedDuringPan' is assigned but its value is never used

Check warning on line 85 in src/DotRecast.Recast.Demo/RecastDemo.cs

View workflow job for this annotation

GitHub Actions / build-and-test-macos-latest

The field 'RecastDemo.movedDuringPan' is assigned but its value is never used

Check warning on line 85 in src/DotRecast.Recast.Demo/RecastDemo.cs

View workflow job for this annotation

GitHub Actions / build

The field 'RecastDemo.movedDuringPan' is assigned but its value is never used

Check warning on line 85 in src/DotRecast.Recast.Demo/RecastDemo.cs

View workflow job for this annotation

GitHub Actions / build

The field 'RecastDemo.movedDuringPan' is assigned but its value is never used

Check warning on line 85 in src/DotRecast.Recast.Demo/RecastDemo.cs

View workflow job for this annotation

GitHub Actions / build-and-test-windows-latest

The field 'RecastDemo.movedDuringPan' is assigned but its value is never used

Check warning on line 85 in src/DotRecast.Recast.Demo/RecastDemo.cs

View workflow job for this annotation

GitHub Actions / build-and-test-windows-latest

The field 'RecastDemo.movedDuringPan' is assigned but its value is never used

Check warning on line 85 in src/DotRecast.Recast.Demo/RecastDemo.cs

View workflow job for this annotation

GitHub Actions / build-and-test-ubuntu-latest

The field 'RecastDemo.movedDuringPan' is assigned but its value is never used

Check warning on line 85 in src/DotRecast.Recast.Demo/RecastDemo.cs

View workflow job for this annotation

GitHub Actions / build-and-test-ubuntu-latest

The field 'RecastDemo.movedDuringPan' is assigned but its value is never used

Check warning on line 85 in src/DotRecast.Recast.Demo/RecastDemo.cs

View workflow job for this annotation

GitHub Actions / build-and-test-macos-latest

The field 'RecastDemo.movedDuringPan' is assigned but its value is never used

Check warning on line 85 in src/DotRecast.Recast.Demo/RecastDemo.cs

View workflow job for this annotation

GitHub Actions / build-and-test-windows-latest

The field 'RecastDemo.movedDuringPan' is assigned but its value is never used

Check warning on line 85 in src/DotRecast.Recast.Demo/RecastDemo.cs

View workflow job for this annotation

GitHub Actions / build-and-test-windows-latest

The field 'RecastDemo.movedDuringPan' is assigned but its value is never used

Check warning on line 85 in src/DotRecast.Recast.Demo/RecastDemo.cs

View workflow job for this annotation

GitHub Actions / publish-to-nuget

The field 'RecastDemo.movedDuringPan' is assigned but its value is never used

Check warning on line 85 in src/DotRecast.Recast.Demo/RecastDemo.cs

View workflow job for this annotation

GitHub Actions / publish-to-nuget

The field 'RecastDemo.movedDuringPan' is assigned but its value is never used
private bool rotate;
private bool movedDuringRotate;
private float scrollZoom;
private readonly float[] origMousePos = new float[2];
private readonly float[] origCameraEulers = new float[2];
private RcVec2f origMousePos = new RcVec2f();
private RcVec2f origCameraEulers = new RcVec2f();
private RcVec3f origCameraPos = new RcVec3f();

private readonly float[] cameraEulers = { 45, -45 };
private RcVec2f cameraEulers = new RcVec2f(45, -45);
private RcVec3f cameraPos = RcVec3f.Of(0, 0, 0);


Expand Down Expand Up @@ -155,14 +155,14 @@ public void OnMouseScrolled(IMouse mice, ScrollWheel scrollWheel)

public void OnMouseMoved(IMouse mouse, Vector2 position)
{
mousePos[0] = (float)position.X;
mousePos[1] = (float)position.Y;
int dx = (int)(mousePos[0] - origMousePos[0]);
int dy = (int)(mousePos[1] - origMousePos[1]);
mousePos.X = position.X;
mousePos.Y = position.Y;
int dx = (int)(mousePos.X - origMousePos.X);
int dy = (int)(mousePos.Y - origMousePos.Y);
if (rotate)
{
cameraEulers[0] = origCameraEulers[0] + dy * 0.25f;
cameraEulers[1] = origCameraEulers[1] + dx * 0.25f;
cameraEulers.X = origCameraEulers.X + dy * 0.25f;
cameraEulers.Y = origCameraEulers.Y + dx * 0.25f;
if (dx * dx + dy * dy > 3 * 3)
{
movedDuringRotate = true;
Expand Down Expand Up @@ -199,10 +199,8 @@ public void OnMouseUpAndDown(IMouse mouse, MouseButton button, bool down)
// Rotate view
rotate = true;
movedDuringRotate = false;
origMousePos[0] = mousePos[0];
origMousePos[1] = mousePos[1];
origCameraEulers[0] = cameraEulers[0];
origCameraEulers[1] = cameraEulers[1];
origMousePos = mousePos;
origCameraEulers = cameraEulers;
}
}
else if (button == MouseButton.Middle)
Expand All @@ -212,11 +210,8 @@ public void OnMouseUpAndDown(IMouse mouse, MouseButton button, bool down)
// Pan view
pan = true;
movedDuringPan = false;
origMousePos[0] = mousePos[0];
origMousePos[1] = mousePos[1];
origCameraPos.X = cameraPos.X;
origCameraPos.Y = cameraPos.Y;
origCameraPos.Z = cameraPos.Z;
origMousePos = mousePos;
origCameraPos = cameraPos;
}
}
}
Expand Down Expand Up @@ -513,8 +508,8 @@ private void OnWindowUpdate(double dt)
RcVec3f rayStart = new RcVec3f();
RcVec3f rayEnd = new RcVec3f();

GLU.GlhUnProjectf(mousePos[0], viewport[3] - 1 - mousePos[1], 0.0f, modelviewMatrix, projectionMatrix, viewport, ref rayStart);
GLU.GlhUnProjectf(mousePos[0], viewport[3] - 1 - mousePos[1], 1.0f, modelviewMatrix, projectionMatrix, viewport, ref rayEnd);
GLU.GlhUnProjectf(mousePos.X, viewport[3] - 1 - mousePos.Y, 0.0f, modelviewMatrix, projectionMatrix, viewport, ref rayStart);
GLU.GlhUnProjectf(mousePos.X, viewport[3] - 1 - mousePos.Y, 1.0f, modelviewMatrix, projectionMatrix, viewport, ref rayEnd);

SendMessage(new RaycastEvent()
{
Expand Down Expand Up @@ -578,8 +573,8 @@ private void OnWindowUpdate(double dt)
cameraPos.Y = (bmax.Y + bmin.Y) / 2 + camr;
cameraPos.Z = (bmax.Z + bmin.Z) / 2 + camr;
camr *= 5;
cameraEulers[0] = 45;
cameraEulers[1] = -45;
cameraEulers.X = 45;
cameraEulers.Y = -45;
}

_sample.SetChanged(false);
Expand Down

0 comments on commit adfded5

Please sign in to comment.