Skip to content

Commit

Permalink
Merge pull request #7 from jackutea/fpmath/feature_minkowski
Browse files Browse the repository at this point in the history
Fpmath/feature minkowski
  • Loading branch information
jackutea authored Dec 11, 2023
2 parents a0a3ff7 + b935478 commit 833110f
Show file tree
Hide file tree
Showing 69 changed files with 544 additions and 5 deletions.
8 changes: 8 additions & 0 deletions Assets/com.gamearki.fpmath.fp.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
23 changes: 23 additions & 0 deletions Assets/com.gamearki.fpmath.fp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "com.gamearki.fpmath.fp",
"version": "1.8.3",
"description": "依赖 FixedMath.NET 定点数库",
"displayName": "GameArki.FPMath",
"unity": "2019.4",
"author": {
"name": "GameArki",
"email": "chenwansal1@163.com",
"url": "https://github.com/GameArki"
},
"changelogUrl": "",
"dependencies": {},
"documentationUrl": "",
"hideInEditor": false,
"keywords": [
"GameArki"
],
"license": "",
"licensesUrl": "",
"samples": [],
"unityRelease": "16f1"
}
7 changes: 7 additions & 0 deletions Assets/com.gamearki.fpmath.fp/package.json.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Assets/com.gamearki.fpmath.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/com.gamearki.fpmath/Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions Assets/com.gamearki.fpmath/Runtime/FPMinkowski.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using UnityEngine;

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

int resCount = 0;

int aLen = aConvex.Length;
int bLen = bConvex.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;

while (resCount < aLen + bLen) {

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

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

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

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

}

return resCount;
}

/// <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) {
return a.x * b.y - a.y * b.x;
}

static int BottomIndex(Span<Vector2> vectors) {
int res = 0;
for (int i = 0; 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;
}
}
return res;
}

// public static int Sum_FullPolygon(Vector2[] aVectors, Vector2[] bVectors, Vector2[] fullPolygon) {
// int resIndex = 0;
// for (int i = 0; i < aVectors.Length; i += 1) {
// for (int j = 0; j < bVectors.Length; j += 1) {
// fullPolygon[resIndex] = aVectors[i] + bVectors[j];
// resIndex += 1;
// }
// }
// return resIndex;
// }

}

}
11 changes: 11 additions & 0 deletions Assets/com.gamearki.fpmath/Runtime/FPMinkowski.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/com.gamearki.fpmath/Runtime/GameArki.FPMath.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "GameArki.FPMath"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/com.gamearki.fpmath/Sample.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Assets/com.gamearki.fpmath/Sample/GameArki.FPMath.Sample.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "GameArki.FPMath.Sample",
"rootNamespace": "",
"references": [
"GUID:b8f55fa5a969fb247ac8ba8dcd19d463"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [
"UNITY_EDITOR"
],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions Assets/com.gamearki.fpmath/Sample/Sample_FPMinkowski.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace GameArki.FPMath.Sample {

public class Sample_FPMinkowski : MonoBehaviour {

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

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

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

}

void OnDrawGizmos() {
if (aVectors == null || bVectors == null) return;
// Draw A
Gizmos.color = Color.blue;
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++) {
Vector2 curP = bVectors[i];
Vector2 nextP = bVectors[(i + 1) % bVectors.Length];
Gizmos.DrawLine(curP, nextP);
}

// Draw Sum
int sumLength = FPMinkowski2D.Sum_CulledPolygon(aVectors, bVectors, results);
Gizmos.color = Color.yellow;
for (int i = 0; i < sumLength; i++) {
Vector2 point = results[i];
Vector2 next = results[(i + 1) % sumLength];
Gizmos.DrawLine(point, next);
}

// Draw Diff
int resultLength = FPMinkowski2D.Diff_CulledPolygon(bVectors, aVectors, results);
Gizmos.color = Color.red;
for (int i = 0; i < resultLength; i++) {
Vector2 point = results[i];
Vector2 next = results[(i + 1) % resultLength];
Gizmos.DrawLine(point, next);
}

Gizmos.DrawSphere(Vector2.zero, 1f);
}
}
}
11 changes: 11 additions & 0 deletions Assets/com.gamearki.fpmath/Sample/Sample_FPMinkowski.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 833110f

Please sign in to comment.