Skip to content

Commit

Permalink
Rename GJKEPA to UEPA
Browse files Browse the repository at this point in the history
  • Loading branch information
notgiven688 committed Nov 22, 2024
1 parent f071f39 commit 7e48437
Show file tree
Hide file tree
Showing 20 changed files with 61 additions and 62 deletions.
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
# Unified EPA Demo
[![uepa_tests](https://github.com/notgiven688/unified_epa/actions/workflows/uepa_tests.yml/badge.svg)](https://github.com/notgiven688/unified_epa/actions/workflows/uepa_tests.yml)

Robust and simple implementation of the **Gilbert-Johnson-Keerthi** (GJK) distance and the **Expanding Polytope Algorithm** (EPA).
Robust and simple implementation of the **Expanding Polytope Algorithm** (EPA).

The demo is written in C#, with [OpenTK](https://github.com/opentk/opentk) as only dependency.

## Some Details

The implementation itself is contained in [GJKEPA.cs](src/GJKEPA.cs) with ~400 lines of code. The algorithm detects whether two convex objects are separated or colliding and returns the points of closest distance and deepest penetration, respectively.
The implementation itself is contained in [UEPA.cs](src/UEPA.cs) with ~400 lines of code. The algorithm detects whether two convex objects are separated or colliding and returns the points of closest distance and deepest penetration, respectively.

In contrast to other available implementations we only work with a convex hull with finite volume. Thus the GJK-phase differs from the computationally more efficient "traditional implementations" of GJK. The algorithm relies solely on point-triangle distances.
A variant of the expanding polytope algorithm is used also for the separating case - simplifying
the algorithm for collision detection for the general case.

## Run the demo

The demo should be able to run cross-platform utilizing OpenGL.
```
1. Install the [.NET 8.0 SDK](https://dotnet.microsoft.com/download/dotnet/8.0)

1. Install the [.NET 9.0 SDK](https://dotnet.microsoft.com/download/dotnet/9.0)
2. git clone https://github.com/notgiven688/unified_epa.git
3. cd unified_epa
4. dotnet run -c Release
```
3. cd unified_epa && dotnet run -c Release

## Screenshots

[YouTube Video](https://www.youtube.com/watch?v=NMdp7A13EAI)

![alt text](screenshots/gjkepa.png?raw=true)
![alt text](screenshots/uepa.png?raw=true)
File renamed without changes
2 changes: 1 addition & 1 deletion src/Drawables/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
using OpenTK.Windowing.GraphicsLibraryFramework;
using OpenTK.Windowing.Common;

namespace GJKEPADemo
namespace UEPADemo
{
public class Camera : IDrawableComponent
{
Expand Down
2 changes: 1 addition & 1 deletion src/Drawables/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;

namespace GJKEPADemo
namespace UEPADemo
{
public class Connection : IDrawableComponent
{
Expand Down
2 changes: 1 addition & 1 deletion src/Drawables/ImplicitShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;

namespace GJKEPADemo
namespace UEPADemo
{
public class ImplicitShape : Primitive
{
Expand Down
2 changes: 1 addition & 1 deletion src/Drawables/Primitive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;

namespace GJKEPADemo
namespace UEPADemo
{
public class Primitive : IDrawableComponent
{
Expand Down
20 changes: 10 additions & 10 deletions src/Drawables/Showcase.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
/* Copyright <2021> <Thorben Linneweber>
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
using System;
using System.Reflection;
Expand All @@ -27,7 +27,7 @@
using OpenTK.Windowing.Common;
using OpenTK.Windowing.GraphicsLibraryFramework;

namespace GJKEPADemo
namespace UEPADemo
{
public class Showcase : IDrawableComponent
{
Expand Down Expand Up @@ -121,7 +121,7 @@ public void Detect()
string leftShape = PrimitiveLeft.SupportMap.GetType().GetCustomAttribute<ShapeAttribute>(true).Name;
string rightShape = PrimitiveRight.SupportMap.GetType().GetCustomAttribute<ShapeAttribute>(true).Name;

Overlay.Text = $"GJK/EPA Demonstration ({leftShape}/{rightShape})\n\n";
Overlay.Text = $"UEPA Demonstration ({leftShape}/{rightShape})\n\n";
Overlay.Text += "+ Controls\n\n";
Overlay.Text += " change shape distance (k,l)\n";
Overlay.Text += " move the camera (w,a,s,d,mouse)\n";
Expand All @@ -139,7 +139,7 @@ public void Detect()
JVector pl, pr;
double separation;

bool success = GJKEPA.Detect(PrimitiveLeft.SupportMap, PrimitiveRight.SupportMap,
bool success = UEPA.Detect(PrimitiveLeft.SupportMap, PrimitiveRight.SupportMap,
orientLeft, orientRight, posLeft, posRight,
out pl, out pr, out separation);

Expand All @@ -149,8 +149,8 @@ public void Detect()
Vector3 pB = new Vector3((float)pr.X, (float)pr.Y, (float)pr.Z);

Overlay.Text += $" Collision: {(separation < 0.0d).ToString()}\n";
Overlay.Text += $" Iterations: {GJKEPA.epaSolver.Statistics.Iterations}\n";
Overlay.Text += $" Accuracy: {GJKEPA.epaSolver.Statistics.Accuracy:0E+00}\n";
Overlay.Text += $" Iterations: {UEPA.epaSolver.Statistics.Iterations}\n";
Overlay.Text += $" Accuracy: {UEPA.epaSolver.Statistics.Accuracy:0E+00}\n";
Overlay.Text += $" Separation: {separation:0.###}\n";

PrimitiveLeft.Alpha = separation > 0.0d ? 1.0f : 0.5f;
Expand All @@ -173,7 +173,7 @@ bool IsSwitched(Keys key) =>

if (KeyState.IsKeyDown(Keys.K)) distance -= 0.03f * timescale;
if (KeyState.IsKeyDown(Keys.L)) distance += 0.03f * timescale;

if (IsSwitched(Keys.R)) autorotate = !autorotate;
if (IsSwitched(Keys.T)) advancerotation = true;
if (IsSwitched(Keys.N)) NextRandomShapes();
Expand Down
22 changes: 11 additions & 11 deletions src/Drawables/TextOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
using System.IO;
using System.IO.Compression;

using Font = GJKEPADemo.Monospace12;
using Font = UEPADemo.Monospace12;

namespace GJKEPADemo
namespace UEPADemo
{
public class TextOverlay : IDrawableComponent
{
Expand Down Expand Up @@ -67,10 +67,10 @@ private TextureHandle CreateTexture(TextureUnit unit)

unsafe
{
byte[] raw = Font.GetData();
byte[] raw = Monospace12.GetData();
fixed(void* ptr = &raw[0])
{
GL.TexImage2D(TextureTarget.Texture2d, 0, OpenTK.Graphics.OpenGL.InternalFormat.Rgba, Font.TextureWidth, Font.TextureHeight, 0,
GL.TexImage2D(TextureTarget.Texture2d, 0, OpenTK.Graphics.OpenGL.InternalFormat.Rgba, Monospace12.TextureWidth, Monospace12.TextureHeight, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, (IntPtr)ptr);
}
}
Expand All @@ -90,17 +90,17 @@ private void AddGlyphs()

for (int i = 0; i < len; i++)
{
float deltaW = (float)Font.PixelWidth / (float)Font.TextureWidth;
float deltaH = (float)Font.PixelHeight / (float)Font.TextureHeight;
int numch = Font.NumberCharactersHorizontal;
float deltaW = (float)Monospace12.PixelWidth / (float)Monospace12.TextureWidth;
float deltaH = (float)Monospace12.PixelHeight / (float)Monospace12.TextureHeight;
int numch = Monospace12.NumberCharactersHorizontal;

vertices.Add(0); vertices.Add(Font.PixelHeight);
vertices.Add(0); vertices.Add(Monospace12.PixelHeight);
vertices.Add(deltaW * (float)((i % numch) + 0)); vertices.Add(deltaH * (float)(i / numch + 1));

vertices.Add(Font.PixelWidth); vertices.Add(Font.PixelHeight);
vertices.Add(Monospace12.PixelWidth); vertices.Add(Monospace12.PixelHeight);
vertices.Add(deltaW * (float)((i % numch) + 1)); vertices.Add(deltaH * (float)(i / numch + 1));

vertices.Add(Font.PixelWidth); vertices.Add(0);
vertices.Add(Monospace12.PixelWidth); vertices.Add(0);
vertices.Add(deltaW * (float)((i % numch) + 1)); vertices.Add(deltaH * (float)(i / numch + 0));

vertices.Add(0); vertices.Add(0);
Expand Down Expand Up @@ -168,7 +168,7 @@ public void Draw(FrameEventArgs e)
int index = (int)lines[i][k];
int pointer = (index - asciiMin) * 6;

shader.Offset.Set(Position + new Vector2((Font.PixelWidth - Font.ExtraMargin) * k, (Font.PixelHeight - Font.ExtraMargin) * i));
shader.Offset.Set(Position + new Vector2((Monospace12.PixelWidth - Monospace12.ExtraMargin) * k, (Monospace12.PixelHeight - Monospace12.ExtraMargin) * i));
GL.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, sizeof(float) * pointer);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/LinearMath/JMatrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
using System;
using System.Runtime.CompilerServices;

namespace GJKEPADemo
namespace UEPADemo
{
/// <summary>
/// 3x3 Matrix. Member of the math namespace, so every method
Expand Down
2 changes: 1 addition & 1 deletion src/LinearMath/JVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
using System;
using System.Runtime.CompilerServices;

namespace GJKEPADemo
namespace UEPADemo
{
/// <summary>
/// A vector structure. Member of the math
Expand Down
2 changes: 1 addition & 1 deletion src/MinkowskiDifference.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GJKEPADemo;
namespace UEPADemo;

public struct MinkowskiDifference
{
Expand Down
12 changes: 6 additions & 6 deletions src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
/* Copyright <2021> <Thorben Linneweber>
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
using System;

Expand All @@ -26,7 +26,7 @@
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;

namespace GJKEPADemo
namespace UEPADemo
{
public interface IDrawableComponent : IDisposable
{
Expand Down Expand Up @@ -96,7 +96,7 @@ static void Main()
GameWindowSettings gws = GameWindowSettings.Default;
NativeWindowSettings nws = NativeWindowSettings.Default;

nws.Title = "GJK/EPA Demonstration";
nws.Title = "UEPA Demonstration";
nws.Size = new Vector2i(1024, 768);
new MainWindow(gws, nws).Run();

Expand Down
2 changes: 1 addition & 1 deletion src/Shaders/NeonShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
using System;

namespace GJKEPADemo
namespace UEPADemo
{
public class NeonShader : BasicShader, IModelViewProjection
{
Expand Down
2 changes: 1 addition & 1 deletion src/Shaders/PhongShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
using System;

namespace GJKEPADemo
namespace UEPADemo
{
public class PhongShader : BasicShader, IModelViewProjection, IViewPosition
{
Expand Down
2 changes: 1 addition & 1 deletion src/Shaders/QuadShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
using System;

namespace GJKEPADemo
namespace UEPADemo
{
public class QuadShader : BasicShader
{
Expand Down
10 changes: 5 additions & 5 deletions src/Shaders/Shader.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
/* Copyright <2021> <Thorben Linneweber>
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
using System;
using System.Collections.Generic;
Expand All @@ -26,7 +26,7 @@
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;

namespace GJKEPADemo
namespace UEPADemo
{
interface IModelViewProjection
{
Expand Down
2 changes: 1 addition & 1 deletion src/Shaders/Uniform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
using OpenTK.Graphics.OpenGL;
using OpenTK.Mathematics;

namespace GJKEPADemo
namespace UEPADemo
{
public abstract class Uniform
{
Expand Down
2 changes: 1 addition & 1 deletion src/Shapes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
using System.Reflection;
using System.Collections.Generic;

namespace GJKEPADemo
namespace UEPADemo
{
public class ShapeAttribute : Attribute
{
Expand Down
8 changes: 4 additions & 4 deletions src/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
using System;

namespace GJKEPADemo
namespace UEPADemo
{
public static class Tests
{
Expand Down Expand Up @@ -55,7 +55,7 @@ private static void CubeCube(int i)
JVector p1, p2;
double separation;

GJKEPA.Detect(s1, s2, rot1, rot2, pos1, pos2,
UEPA.Detect(s1, s2, rot1, rot2, pos1, pos2,
out p1, out p2, out separation);

double analyticalDistance = 2.0d * (double)i / 10.0d;
Expand All @@ -78,7 +78,7 @@ private static void CubeSphere(int i)
JVector p1, p2;
double separation;

GJKEPA.Detect(s1, s2, rot1, rot2, pos1, pos2,
UEPA.Detect(s1, s2, rot1, rot2, pos1, pos2,
out p1, out p2, out separation);
}

Expand All @@ -96,7 +96,7 @@ private static void SphereSphere(int i)
JVector p1, p2;
double separation;

GJKEPA.Detect(s1, s2, rot1, rot2, pos1, pos2,
UEPA.Detect(s1, s2, rot1, rot2, pos1, pos2,
out p1, out p2, out separation);

double analyticalDistance = (pos2 - pos1).Length() - 1.0d;
Expand Down
Loading

0 comments on commit 7e48437

Please sign in to comment.