【问题标题】:How to find a position, knowing a direction, angle and distance?如何找到位置,知道方向、角度和距离?
【发布时间】:2019-07-26 01:13:00
【问题描述】:

使用提供的图表,我知道:

  • 位置 A 的字符,面向 B (posA)
  • 对象位于位置 B (posB),面向随机方向。
  • C 的期望角度 (20) (desiredAngle),基于 A 到 B 的方向。
  • B 到 C 的距离

我将如何找到 C 的位置?

在下面的代码中,我设法得到了向量 BC,现在如何使用从 A 到 B 的距离沿该向量定义一个点?

//The angle of the hand in relation to the object
public float angleFromPlayer = -60f;

//Get the direction vector from the selectedObject to the actor
Vector3 objectDirectionToActor = actorParent.position - selectedObjectCenter;

//Make it horizontal (Flatten the y)
objectDirectionToActor.y = 0;

//Get the rotated vector using the desiredAngle
Vector3 rotatedVector = Quaternion.Euler(0, angleFromPlayer, 0) * objectDirectionToActor;

【问题讨论】:

  • 这不是一道数学题吗?基本上:使用Atan得到线BA相对于0的角度。加/减所需的角度,并使用SinCos加上角度和长度BC来计算C的位置(不要忘记将位置 B 添加到结果中)。不要忘记 C# 的 Math 处理的是弧度,而不是度数。
  • @Castor Vector3 来自哪里? Mathf 是什么?
  • 看起来您使用的是度数而不是desiredAngle,因为从未指定度数。
  • 通过减去两个点,然后将得到的增量除以斜边来获取方向。这将为您提供标准化的方向。然后你可以简单地将它乘以你想要的任何距离,比如 0.1f 并将它添加到起点 B。
  • @Bart 谢谢你让我知道。我从原始问题中删除了答案,并按照建议将其添加到下面。

标签: c# unity3d


【解决方案1】:

想通了,这是后人的解决方案:

//How high the hand will be from the dropSurface/selectedObjectPivot
public float handOffsetFromDropSurface = 0.15f;

//The angle of the hand in relation to the object
public float targetAngleFromPlayer = -80f;

//Extra distance between selectedObjectCenter and wristBone (since the handDistance is a bit too short)
public float extraHandDistance = 0f;

Vector3 GetHandBonePositionForDropSurface(Vector3 selectedObjectCenter) {
    //Get the direction vector from the selectedObject to the actor
    Vector3 objectDirectionToActor = actorParent.position - selectedObjectCenter;

    //Make it horizontal (Flatten the y)
    objectDirectionToActor.y = 0;

    //Change vector size to 1 for multiplication
    objectDirectionToActor.Normalize();

    //Get the rotated vector using the desiredAngle
    Vector3 rotatedVector = Quaternion.Euler(0, targetAngleFromPlayer, 0) * objectDirectionToActor;

    //Get the distance we want the point to be from the center of the selectedObject
    float handDistance = 
    Vector3.Distance(playerProperties.boneRightIndexProximal.position, 
    playerProperties.boneRightHand.position) + extraHandDistance;

    //Resize vector to match the hand distance
    rotatedVector *= handDistance;

    //Get finalPosition, adding the handOffsetFromDropSurface
    Vector3 wristTargetPosition = selectedObjectCenter + rotatedVector + (Vector3.up * handOffsetFromDropSurface);

    return wristTargetPosition;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 2010-11-28
    • 2012-11-30
    • 2013-03-07
    • 2011-02-18
    • 1970-01-01
    • 2013-04-29
    相关资源
    最近更新 更多