英文:
Unity C# - How to find point C in a right-angled triangle?
问题
请问你能帮我处理这个任务吗?这里是问题:
我们知道点A和点B的位置,而且我们知道我们的三角形是直角三角形。我们还知道直角会出现在点C,我们明白在直角三角形中所有角的和等于180度。
问题是,我们如何找到点C的位置?
我正在开发一个守门员机器人,其中点A代表守门员,B代表足球,点C是机器人应该到达的位置,以站在足球的对面。守门员始终背对球门,所以我们可以使用transform.right。
所有点都是Vector3,其中Y始终为0。
(在图片中,有一个自上而下的投影。球门和守门员可以旋转,但守门员始终背对球门。)
英文:
could you please help me with this task? Here's the problem:
We know the locations of point A and point B, and we know that our triangle is right-angled. We also know that the right angle will be at point C, and we understand that the sum of all angles in a right-angled triangle equals 180 degrees.
The question is, how do we find the location of point C?
I'm developing a goalkeeper bot, where point A represents the goalkeeper, B represents the ball, and point C is the location where the bot should reach to stand opposite the ball. The goalkeeper always has their back turned to the goal, so we can use transform.right.
All points are Vector3, where Y will always be 0.
(In the picture, there is a top-down projection. The goal and the goalkeeper can rotate, but the goalkeeper will always have their back to the goal.)
I've tried many ways to solve this problem, but haven't been able to arrive at the correct solution.
答案1
得分: 4
我理解你知道goal
的旋转/方向,让我们假设我们可以直接使用goal
的right
向量作为法线,然后你可以简单地使用Vector3.Project
:
Vector3 A, B;
Transform Goal;
// 向量 A->B
// 映射到 Goal.right 方向
// 并将结果作为相对于 A 的新向量应用
Vector3 C = A + Vector3.Project(B - A, Goal.right);
不需要自己计算角度
英文:
As I understand you know the goal
rotation / direction and lets just say we can directly use the goal's right
vector as a normal then you could simply use Vector3.Project
Vector3 A, B;
Transform Goal;
// Vector A->B
// mapped onto the Goal.right direction
// and result applied as new vector relative to A
Vector3 C = A + Vector3.Project(B - A, Goal.right);
No need to calculate the angles yourself
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论