-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
Fpmath/feature minkowski
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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; | ||
// } | ||
|
||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
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); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.