lab3
This commit is contained in:
1 parent
a70076ebe9
commit
854835878c
20 files changed
+2542
No files matched your search
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Lab3
|
||||
{
|
||||
public class Path : MonoBehaviour
|
||||
{
|
||||
public List<GameObject> nodes;
|
||||
private List<PathSegment> _segments;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_segments = GetSegments();
|
||||
}
|
||||
|
||||
public List<PathSegment> GetSegments()
|
||||
{
|
||||
var segments = new List<PathSegment>();
|
||||
for (var i = 0; i < nodes.Count - 1; i++)
|
||||
{
|
||||
var src = nodes[i].transform.position;
|
||||
var dst = nodes[i + 1].transform.position;
|
||||
var segment = new PathSegment(src, dst);
|
||||
segments.Add(segment);
|
||||
}
|
||||
return segments;
|
||||
}
|
||||
|
||||
public float GetParam(Vector3 position, float lastParam)
|
||||
{
|
||||
PathSegment currentSegment = null;
|
||||
var tempParam = 0f;
|
||||
foreach (var ps in _segments)
|
||||
{
|
||||
tempParam += Vector3.Distance(ps.A, ps.B);
|
||||
|
||||
if (!(lastParam <= tempParam)) continue;
|
||||
|
||||
currentSegment = ps;
|
||||
break;
|
||||
}
|
||||
|
||||
if (currentSegment == null)
|
||||
return 0;
|
||||
|
||||
var currPos = position - currentSegment.A;
|
||||
var segmentDirection = currentSegment.B - currentSegment.A;
|
||||
segmentDirection.Normalize();
|
||||
|
||||
var pointInSegment = Vector3.Project(currPos, segmentDirection);
|
||||
|
||||
var param = tempParam - Vector3.Distance(currentSegment.A, currentSegment.B) + pointInSegment.magnitude;
|
||||
return param;
|
||||
}
|
||||
|
||||
public Vector3 GetPosition(float param)
|
||||
{
|
||||
PathSegment currentSegment = null;
|
||||
var tempParam = 0f;
|
||||
|
||||
foreach (var ps in _segments)
|
||||
{
|
||||
tempParam += Vector3.Distance(ps.A, ps.B);
|
||||
|
||||
if (!(param <= tempParam)) continue;
|
||||
|
||||
currentSegment = ps;
|
||||
break;
|
||||
}
|
||||
|
||||
if (currentSegment == null)
|
||||
return Vector3.zero;
|
||||
|
||||
var segmentDirection = currentSegment.B - currentSegment.A;
|
||||
segmentDirection.Normalize();
|
||||
|
||||
var localParam = param - (tempParam - Vector3.Distance(currentSegment.A, currentSegment.B));
|
||||
|
||||
var position = currentSegment.A + segmentDirection * localParam;
|
||||
|
||||
return position;
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
var tmp = Gizmos.color;
|
||||
Gizmos.color = Color.magenta;
|
||||
for (var i = 0; i < nodes.Count - 1; i++)
|
||||
{
|
||||
var src = nodes[i].transform.position;
|
||||
var dst = nodes[i + 1].transform.position;
|
||||
var direction = dst - src;
|
||||
Gizmos.DrawRay(src, direction);
|
||||
}
|
||||
Gizmos.color = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user