Skip to content

Commit

Permalink
Merge pull request #8 from jackutea/fpmath/feature_minkowski
Browse files Browse the repository at this point in the history
<feature> fix: no cyclic indexing
  • Loading branch information
jackutea authored Dec 12, 2023
2 parents 833110f + f80ad9d commit d64d0e1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 45 deletions.
29 changes: 12 additions & 17 deletions Assets/com.gamearki.fpmath/Runtime/FPMinkowski.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,31 @@ namespace GameArki.FPMath {
public static class FPMinkowski2D {

/// <summary> return the count of the result polygon </summary>
public static int Sum_CulledPolygon(Span<Vector2> aConvex, Span<Vector2> bConvex, Vector2[] culledPolygon) {
public static int Sum_CulledPolygon(Span<Vector2> aVectors, Span<Vector2> bVectors, Vector2[] culledPolygon) {

int resCount = 0;

int aLen = aConvex.Length;
int bLen = bConvex.Length;
int aLen = aVectors.Length;
int bLen = bVectors.Length;

int aBottomIndex = BottomIndex(aConvex);
int bBottomIndex = BottomIndex(bConvex);
culledPolygon[resCount++] = aConvex[aBottomIndex] + bConvex[bBottomIndex];
int aCurIndex = (aBottomIndex + 1) % aLen;
int bCurIndex = (bBottomIndex + 1) % bLen;
int aBottomIndex = BottomIndex(aVectors);
int bBottomIndex = BottomIndex(bVectors);
int aCurIndex = aBottomIndex;
int bCurIndex = bBottomIndex;

while (resCount < aLen + bLen) {

culledPolygon[resCount] = aConvex[aCurIndex] + bConvex[bCurIndex];
culledPolygon[resCount] = aVectors[aCurIndex] + bVectors[bCurIndex];
resCount += 1;

int aNextIndex = (aCurIndex + 1) % aLen;
int bNextIndex = (bCurIndex + 1) % bLen;

float cross = Cross(aConvex[aNextIndex] - aConvex[aCurIndex], bConvex[bNextIndex] - bConvex[bCurIndex]);
float cross = Cross(aVectors[aNextIndex] - aVectors[aCurIndex], bVectors[bNextIndex] - bVectors[bCurIndex]);

if (cross >= 0 && aCurIndex != aBottomIndex) {
if (cross >= 0 && aCurIndex != aBottomIndex - 1) {
aCurIndex = aNextIndex;
}
if (cross <= 0 && bCurIndex != bBottomIndex) {
} else if (cross <= 0 && bCurIndex != bBottomIndex - 1) {
bCurIndex = bNextIndex;
}

Expand All @@ -44,14 +42,11 @@ public static int Sum_CulledPolygon(Span<Vector2> aConvex, Span<Vector2> bConvex

/// <summary> return the count of the result polygon </summary>
public static int Diff_CulledPolygon(Span<Vector2> aVectors, Span<Vector2> bVectors, Vector2[] culledPolygon) {

Span<Vector2> bReversed = stackalloc Vector2[bVectors.Length];
for (int i = 0; i < bVectors.Length; i += 1) {
bReversed[i] = -bVectors[i];
}

return Sum_CulledPolygon(aVectors, bReversed, culledPolygon);

}

static float Cross(Vector2 a, Vector2 b) {
Expand All @@ -60,7 +55,7 @@ static float Cross(Vector2 a, Vector2 b) {

static int BottomIndex(Span<Vector2> vectors) {
int res = 0;
for (int i = 0; i < vectors.Length; i += 1) {
for (int i = 1; i < vectors.Length; i += 1) {
if (vectors[i].y < vectors[res].y || (vectors[i].y == vectors[res].y && vectors[i].x < vectors[res].x)) {
res = i;
}
Expand Down
12 changes: 6 additions & 6 deletions Assets/com.gamearki.fpmath/Sample/Sample_FPMinkowski.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@

namespace GameArki.FPMath.Sample {

[ExecuteInEditMode]
public class Sample_FPMinkowski : MonoBehaviour {

[SerializeField] Vector2[] aVectors;
[SerializeField] Vector2[] bVectors;
Vector2[] results;

void OnEnable() {
results = new Vector2[1000];
void Awake() {
results = new Vector2[10000];
}

// Update is called once per frame
void Update() {

}

void OnDrawGizmos() {
if (aVectors == null || bVectors == null) return;
// Draw A
Gizmos.color = Color.blue;
Gizmos.color = Color.green;
for (int i = 0; i < aVectors.Length; i++) {
Vector2 curP = aVectors[i];
Vector2 nextP = aVectors[(i + 1) % aVectors.Length];
Gizmos.DrawLine(curP, nextP);
}
}
// Draw B
Gizmos.color = Color.green;
for (int i = 0; i < bVectors.Length; i++) {
Expand All @@ -46,7 +46,7 @@ void OnDrawGizmos() {
}

// Draw Diff
int resultLength = FPMinkowski2D.Diff_CulledPolygon(bVectors, aVectors, results);
int resultLength = FPMinkowski2D.Diff_CulledPolygon(aVectors, bVectors, results);
Gizmos.color = Color.red;
for (int i = 0; i < resultLength; i++) {
Vector2 point = results[i];
Expand Down
44 changes: 22 additions & 22 deletions Assets/com.gamearki.fpmath/Sample/scene_sample_minkowski.unity
Original file line number Diff line number Diff line change
Expand Up @@ -132,35 +132,14 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 1153529336}
- component: {fileID: 1153529335}
- component: {fileID: 1153529337}
m_Layer: 0
m_Name: GameObject
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1153529335
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1153529334}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: df3a940c9e5217e4daa49b17c1d3a7d8, type: 3}
m_Name:
m_EditorClassIdentifier:
aVectors:
- {x: 8.01, y: 9.1}
- {x: 8.2, y: 5}
- {x: 12.9, y: 1.1}
- {x: 18.18, y: 11.98}
bVectors:
- {x: -0.62, y: 10.86}
- {x: -0.8, y: -4.42}
- {x: 13.08, y: 6.62}
--- !u!4 &1153529336
Transform:
m_ObjectHideFlags: 0
Expand All @@ -176,6 +155,27 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1153529337
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1153529334}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: df3a940c9e5217e4daa49b17c1d3a7d8, type: 3}
m_Name:
m_EditorClassIdentifier:
aVectors:
- {x: -4.89, y: 0}
- {x: 5, y: -8.45}
- {x: 5, y: 0.35}
- {x: 0, y: 5}
bVectors:
- {x: 2.2, y: 1.85}
- {x: 34.74, y: -25.2}
- {x: 20.66, y: 8.12}
--- !u!1 &1765483758
GameObject:
m_ObjectHideFlags: 0
Expand Down

0 comments on commit d64d0e1

Please sign in to comment.