Commit 8cfc3dea authored by 15박보승's avatar 15박보승 Committed by 18류지석

Greedy Pathfinding 구현

parent 07116f74
......@@ -14,6 +14,7 @@ namespace BS
public LayerMask blockMask;
private Dictionary<Vector2Int, Node> nodes = new Dictionary<Vector2Int, Node>();
public bool isBaked { get { return nodes.Count > 0; } }
public bool enableGizmos = true;
......@@ -27,14 +28,14 @@ namespace BS
{
int xCount = Mathf.FloorToInt((bounds.max.x - bounds.min.x) / pointInterval) + 1;
int yCount = Mathf.FloorToInt((bounds.max.y - bounds.min.y) / pointInterval) + 1;
Gizmos.color = Color.green;
Gizmos.color = new Color(0, 1, 0, .3f);
foreach (var node in nodes.Values)
{
Gizmos.color = new Color(node.gridPosition.x / (float)xCount, node.gridPosition.y / (float)yCount, 0, 1);
//Gizmos.color = new Color(node.gridPosition.x / (float)xCount, node.gridPosition.y / (float)yCount, 0, 1);
Gizmos.DrawSphere(node.worldPositon, pointInterval / 5);
foreach (var adj in node.adjacencies)
{
Gizmos.DrawLine(node.worldPositon, adj.worldPositon);
Gizmos.DrawLine(node.worldPositon, nodes[adj].worldPositon);
}
}
}
......@@ -42,9 +43,9 @@ namespace BS
{
Gizmos.DrawWireCube(bounds.center, bounds.size);
for (float i = bounds.min.x; i <= bounds.max.x; i += pointInterval)
for (float i = bounds.min.x; i <= bounds.max.x; i += pointInterval * 2)
{
for (float j = bounds.min.y; j <= bounds.max.y; j += pointInterval)
for (float j = bounds.min.y; j <= bounds.max.y; j += pointInterval * 2)
{
if (Physics2D.OverlapPoint(new Vector2(i, j), blockMask))
Gizmos.color = Color.red;
......@@ -54,9 +55,9 @@ namespace BS
}
}
for (float i = bounds.min.x + pointInterval / 2; i <= bounds.max.x; i += pointInterval)
for (float i = bounds.min.x + pointInterval; i <= bounds.max.x; i += pointInterval * 2)
{
for (float j = bounds.min.y + pointInterval / 2; j <= bounds.max.y; j += pointInterval)
for (float j = bounds.min.y + pointInterval; j <= bounds.max.y; j += pointInterval * 2)
{
if (Physics2D.OverlapPoint(new Vector2(i, j), blockMask))
Gizmos.color = Color.red;
......@@ -73,15 +74,15 @@ namespace BS
Debug.Log("Baking Start");
ClearNodes();
int xCount = Mathf.FloorToInt((bounds.max.x - bounds.min.x) / pointInterval) + 1;
int yCount = 2 * Mathf.FloorToInt((bounds.max.y - bounds.min.y) / pointInterval) + 1;
int yCount = Mathf.FloorToInt((bounds.max.y - bounds.min.y) / pointInterval) + 1;
for (int i = 0; i < xCount; i++)
{
for (int j = 0; j < yCount; j++)
{
if (j % 2 == 1 && i + 1 == xCount)
if ((i + j) % 2 != 0)
continue;
Vector3 worldPosition = bounds.min + new Vector3(i * pointInterval + (j % 2 == 0 ? 0 : pointInterval / 2), j * pointInterval / 2);
Vector3 worldPosition = IndexToWorld(new Vector2Int(i, j));
if (Physics2D.OverlapPoint(worldPosition, blockMask))
continue;
Node newNode = new Node(new Vector2Int(i, j), worldPosition);
......@@ -94,52 +95,164 @@ namespace BS
{
for (int j = 0; j < yCount; j++)
{
List<Node> adj = new List<Node>();
List<Vector2Int> adj = new List<Vector2Int>();
Vector2Int pos = new Vector2Int(i, j);
if (!nodes.ContainsKey(pos))
continue;
Node node = nodes[pos];
if (nodes.ContainsKey(node.gridPosition + new Vector2Int(1, 0))) //Right
if (nodes.ContainsKey(node.gridPosition + new Vector2Int(2, 0))) //Right
{
adj.Add(nodes[node.gridPosition + new Vector2Int(1, 0)]);
adj.Add(node.gridPosition + new Vector2Int(2, 0));
}
if (nodes.ContainsKey(node.gridPosition + new Vector2Int(-1, 0))) //Left
if (nodes.ContainsKey(node.gridPosition + new Vector2Int(-2, 0))) //Left
{
adj.Add(nodes[node.gridPosition + new Vector2Int(-1, 0)]);
adj.Add(node.gridPosition + new Vector2Int(-2, 0));
}
if (nodes.ContainsKey(node.gridPosition + new Vector2Int(0, 2))) // Up
{
adj.Add(nodes[node.gridPosition + new Vector2Int(0, 2)]);
adj.Add(node.gridPosition + new Vector2Int(0, 2));
}
if (nodes.ContainsKey(node.gridPosition + new Vector2Int(0, -2))) // Down
{
adj.Add(nodes[node.gridPosition + new Vector2Int(0, -2)]);
adj.Add(node.gridPosition + new Vector2Int(0, -2));
}
if (nodes.ContainsKey(node.gridPosition + new Vector2Int(-1 + node.gridPosition.y % 2, 1))) //UpLeft
if (nodes.ContainsKey(node.gridPosition + new Vector2Int(-1, 1))) //UpLeft
{
adj.Add(nodes[node.gridPosition + new Vector2Int(-1 + node.gridPosition.y % 2, 1)]);
adj.Add(node.gridPosition + new Vector2Int(-1, 1));
}
if (nodes.ContainsKey(node.gridPosition + new Vector2Int(node.gridPosition.y % 2, 1))) //UpRight
if (nodes.ContainsKey(node.gridPosition + new Vector2Int(1, 1))) //UpRight
{
adj.Add(nodes[node.gridPosition + new Vector2Int(node.gridPosition.y % 2, 1)]);
adj.Add(node.gridPosition + new Vector2Int(1, 1));
}
if (nodes.ContainsKey(node.gridPosition + new Vector2Int(-1 + node.gridPosition.y % 2, -1))) //DownLeft
if (nodes.ContainsKey(node.gridPosition + new Vector2Int(-1, -1))) //DownLeft
{
adj.Add(nodes[node.gridPosition + new Vector2Int(-1 + node.gridPosition.y % 2, -1)]);
adj.Add(node.gridPosition + new Vector2Int(-1, -1));
}
if (nodes.ContainsKey(node.gridPosition + new Vector2Int(node.gridPosition.y % 2, -1))) //DownRight
if (nodes.ContainsKey(node.gridPosition + new Vector2Int(1, -1))) //DownRight
{
adj.Add(nodes[node.gridPosition + new Vector2Int(node.gridPosition.y % 2, -1)]);
adj.Add(node.gridPosition + new Vector2Int(1, -1));
}
node.adjacencies = adj;
nodes[pos] = node;
}
}
Debug.Log("Baking End");
}
public List<Vector3> GetPath(Vector3 from, Vector3 to)
{
return null;
}
public List<Vector3> GetPathGreedy(Vector3 from, Vector3 to)
{
List<Vector3> path = new List<Vector3>();
if (WorldToIndex(from, to - from).x < 0 || WorldToIndex(to, from - to).x < 0)
{
return path;
}
Node cur = nodes[WorldToIndex(from, to - from)];
Node goal = nodes[WorldToIndex(to, from - to)];
path.Add(cur.worldPositon);
Queue<Node> off = new Queue<Node>();
Node next;
while (Vector2.Distance(cur.worldPositon, to) > pointInterval * 2)
{
off.Enqueue(cur);
next = cur;
float min = Vector2.Distance(cur.worldPositon, to);
foreach (var adj in cur.adjacencies)
{
float score = Vector2.Distance(nodes[adj].worldPositon, to);
if (min > score)
{
next = nodes[adj];
min = score;
}
}
if (cur.Equals(next))
{
return path;
}
path.Add(next.worldPositon);
cur = next;
}
path.Add(to);
return path;
}
public List<Vector3> GetPathAStar(Vector3 from, Vector3 to)
{
List<Vector3> path = new List<Vector3>();
if (WorldToIndex(from, to - from).x < 0 || WorldToIndex(to, from - to).x < 0)
{
return path;
}
return path;
}
private Vector3 IndexToWorld(Vector2Int index)
{
return IndexToWorld(index.x, index.y);
}
private Vector3 IndexToWorld(int x, int y)
{
return bounds.min + new Vector3(x * pointInterval, y * pointInterval);
}
private Vector2Int WorldToIndex(Vector3 position, Vector2 direction)
{
int minX = Mathf.FloorToInt((position.x - bounds.min.x) / pointInterval);
int maxX = Mathf.CeilToInt((position.x - bounds.min.x) / pointInterval);
int minY = Mathf.FloorToInt((position.y - bounds.min.y) / pointInterval);
int maxY = Mathf.CeilToInt((position.y - bounds.min.y) / pointInterval);
Vector2Int center;
if ((minX + minY) % 2 != 0)
{
center =
Vector2.Distance(IndexToWorld(minX, minY), position) > Vector2.Distance(IndexToWorld(maxX, maxY), position) ?
new Vector2Int(maxX, maxY) :
new Vector2Int(minX, minY);
}
else
{
center =
Vector2.Distance(IndexToWorld(minX, maxY), position) > Vector2.Distance(IndexToWorld(maxX, minY), position) ?
new Vector2Int(maxX, minY) :
new Vector2Int(minX, maxY);
}
Vector2Int[] adjs = { center + Vector2Int.up, center + Vector2Int.down, center + Vector2Int.left, center + Vector2Int.right };
Vector2Int index = new Vector2Int(-1, -1);
float max = float.MinValue;
foreach (var adj in adjs)
{
if (!nodes.ContainsKey(adj))
continue;
float score = Vector2.Dot(nodes[adj].worldPositon - position, direction.normalized);
if (max < score)
{
index = adj;
max = score;
}
}
if (index.x < 0)
Debug.Log("Blocked!!");
return index;
}
public void ClearNodes()
{
nodes.Clear();
......@@ -156,6 +269,19 @@ namespace BS
}
public Vector2Int gridPosition;
public Vector3 worldPositon;
public List<Node> adjacencies;
public List<Vector2Int> adjacencies;
}
public class PriorityQueue<T>
{
public PriorityQueue()
{
}
public void Enqueue(T item)
{
}
}
}
\ No newline at end of file
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BS {
public class NodalPathfinding2DAgent : MonoBehaviour
{
public NodalPathfinding2D pathFinder;
public Vector3 destination;
public List<Vector3> path = new List<Vector3>();
float t = 0.5f;
private void OnDrawGizmos()
{
Gizmos.color = Color.white;
if (path.Count > 0)
{
Gizmos.DrawLine(transform.position, path[0]);
for (int i = 0; i < path.Count - 1; i++)
{
Gizmos.DrawLine(path[i] + new Vector3(0,0,1), path[i + 1] + new Vector3(0, 0, 1));
}
Gizmos.color = Color.red;
Gizmos.DrawSphere(destination, 0.5f);
}
}
private void Update()
{
t -= Time.deltaTime;
if (t < 0)
{
MoveTo(new Vector2(Random.Range(pathFinder.bounds.min.x, pathFinder.bounds.max.x), Random.Range(pathFinder.bounds.min.y, pathFinder.bounds.max.y)));
t = 0.5f;
}
if (Input.GetKeyDown(KeyCode.Space))
{
transform.position = new Vector2(Random.Range(pathFinder.bounds.min.x, pathFinder.bounds.max.x), Random.Range(pathFinder.bounds.min.y, pathFinder.bounds.max.y));
}
if (Input.GetMouseButtonDown(1))
{
MoveTo(Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
//path = pathFinder.GetPathGreedy(transform.position, destination);
}
public void MoveTo(Vector3 destination)
{
this.destination = destination;
path = pathFinder.GetPathGreedy(transform.position, destination);
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: f4a5a47a8846b8940a9edd3801d55993
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -14,10 +14,12 @@ public class NodalPathfinding2DEditor : Editor
if (GUILayout.Button("Bake"))
{
myTarget.BakeNodes();
SceneView.RepaintAll();
}
if (GUILayout.Button("Clear"))
{
myTarget.ClearNodes();
SceneView.RepaintAll();
}
}
}
......@@ -221,7 +221,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 38041422}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -4.76, y: -4.97, z: -41.328003}
m_LocalPosition: {x: -4.76, y: -4.97, z: 0}
m_LocalScale: {x: 10, y: 5, z: 0}
m_Children: []
m_Father: {fileID: 0}
......@@ -259,7 +259,7 @@ MonoBehaviour:
bounds:
m_Center: {x: 0, y: 0, z: 0}
m_Extent: {x: 10, y: 10, z: 0}
pointInterval: 0.907
pointInterval: 0.324
blockMask:
serializedVersion: 2
m_Bits: 256
......@@ -379,7 +379,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 614196138}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 3.74, y: -1.72, z: -41.328003}
m_LocalPosition: {x: 3.74, y: -1.72, z: 0}
m_LocalScale: {x: 50.72, y: 10, z: 16.9931}
m_Children: []
m_Father: {fileID: 0}
......@@ -486,7 +486,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1002756477}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -0.032, y: 4.9, z: -41.328003}
m_LocalPosition: {x: -0.032, y: 4.9, z: 0}
m_LocalScale: {x: 50.72, y: 10, z: 16.9931}
m_Children: []
m_Father: {fileID: 0}
......@@ -593,7 +593,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1036674795}
m_LocalRotation: {x: 0, y: 0, z: 0.2588191, w: 0.9659258}
m_LocalPosition: {x: -5.48, y: -0.04, z: -41.328003}
m_LocalPosition: {x: -5.48, y: -0.04, z: 0}
m_LocalScale: {x: 50.72, y: 10, z: 16.9931}
m_Children: []
m_Father: {fileID: 0}
......@@ -700,12 +700,135 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1324657432}
m_LocalRotation: {x: 0, y: 0, z: 0.7071068, w: 0.7071068}
m_LocalPosition: {x: 3.33, y: -6.5, z: -41.328003}
m_LocalPosition: {x: 3.33, y: -6.5, z: 0}
m_LocalScale: {x: 50, y: 5, z: 0}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 6
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 90}
--- !u!1 &1592952600
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1592952604}
- component: {fileID: 1592952603}
- component: {fileID: 1592952602}
- component: {fileID: 1592952601}
m_Layer: 8
m_Name: New Sprite (5)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1592952601
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1592952600}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f4a5a47a8846b8940a9edd3801d55993, type: 3}
m_Name:
m_EditorClassIdentifier:
pathFinder: {fileID: 178212234}
destination: {x: 0, y: 0, z: 0}
path: []
--- !u!61 &1592952602
BoxCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1592952600}
m_Enabled: 1
m_Density: 1
m_Material: {fileID: 0}
m_IsTrigger: 0
m_UsedByEffector: 0
m_UsedByComposite: 0
m_Offset: {x: 0, y: 0}
m_SpriteTilingProperty:
border: {x: 0.049999997, y: 0.049999997, z: 0.049999997, w: 0.049999997}
pivot: {x: 0.5, y: 0.5}
oldSize: {x: 0.16, y: 0.16}
newSize: {x: 0.16, y: 0.16}
adaptiveTilingThreshold: 0.5
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
serializedVersion: 2
m_Size: {x: 0.16, y: 0.16}
m_EdgeRadius: 0
--- !u!212 &1592952603
SpriteRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1592952600}
m_Enabled: 1
m_CastShadows: 0
m_ReceiveShadows: 0
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 0
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_Sprite: {fileID: 10911, guid: 0000000000000000f000000000000000, type: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_FlipX: 0
m_FlipY: 0
m_DrawMode: 0
m_Size: {x: 0.16, y: 0.16}
m_AdaptiveModeThreshold: 0.5
m_SpriteTileMode: 0
m_WasSpriteAssigned: 1
m_MaskInteraction: 0
m_SpriteSortPoint: 0
--- !u!4 &1592952604
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1592952600}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -4.76, y: -2.64, z: 0}
m_LocalScale: {x: 2, y: 2, z: 0}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 7
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1627406709
GameObject:
m_ObjectHideFlags: 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment