[英]How can I find the co-ordinates where the user wearing a VR Headset is looking at in a 360 video playing insider Unity Skybox?
[英]How can I find the centre co-ordinates of an array of connected lines?
对于每个段,计算(并存储)段的长度。 将所有长度相加,然后将总数除以2。
按路径顺序遍历所有段,从此减半的总和中减去每个段的长度,直到当前段的长度大于其余的总和。
然后沿着该线段计算该长度处的点。
这是一个抓住中间点的快速代码示例:
Vector2 GetMidPoint(Vector2[] path)
{
var totalLength = 0d;
for(var i = 0; i < path.Length - 1; i++)
totalLength += GetDistanceBetween(path[i], path[i + 1]);
var halfLength = totalLength / 2;
var currLength = 0d;
for(var i = 0; i < path.Length - 1; i++)
{
var currentNode = path[i];
var nextNode = path[i+1];
var nextStepLength = GetDistanceBetween(currentNode, nextNode);
if (halfLength < currLength + nextStepLength)
{
var distanceLeft = halfLength - currLength;
var ratio = distanceLeft / nextStepLength;
return new Vector2(currentNode.x + (nextNode.x - currentNode.x) * ratio, currentNode.y + (nextNode.y - currentNode.y) * ratio);
}
else
currLength += nextStepLength;
}
throw new Exception("Couldn't get the mid point");
}
public double GetDistanceBetween(Vector2 a, Vector2 b)
{
var x = Math.Abs(a.x - b.x);
var y = Math.Abs(a.y - b.y);
return (Math.Sqrt(Math.Pow(x,2) + Math.Pow(y, 2)));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.