我画了这个惊人的图表来展示我想要计算的东西。如果你看不出来,这些是城堡的城墙和塔楼。为了“关闭”城堡,我需要找出两条红线相交的点,这样就可以建造一座塔将两堵墙连接在一起。
为了解决这个问题,墙的大小是固定的,因此所有三个边长都是已知的。这意味着您可以使用余弦定律找出其中一个旋转墙与静态墙的角度。但是,我尝试实现它,但无法使其正常工作。
这是代码:
function FindIntersection(vector Tower1, vector Tower2, float Wall1, float Wall2, out vector Point)
{
local float S1; // Length of side between the two towers.
local float S2; // Length of the first wall.
local float S3; // Length of the second wall.
local float CosA, A; // Stores the angle between S1 and S2.
local vector Vec, Vec2;
Tower1.Z = 0; // Make sure the towers are level.
Tower2.Z = 0;
S1 = VSize(Tower2 - Tower1); // Get the first side length.
S2 = Wall1; // Get the second side length.
S3 = Wall2; // Get the third side length.
`log("---------- S1: " $ S1 $ " S2: " $ S2 $ " S3: " $ S3 $ " -----------");
// Perform cosine law to get angle between S1 and S2.
CosA = (Sq(S2) + Sq(S1) - Sq(S3)) / (2 * S2 * S1);
A = ACos(CosA);
`log("--------------------- " $ A*57.2957795131 $ " ---------------------");
// Get a vector angle between up and S1.
Vec = Normal(Tower2-Tower1);
// Get a vector angle between S1 and S2.
Vec2.X = Cos(A);
Vec2.Y = Sin(A);
Vec2.Z = 0;
Vec2 = Normal(Vec2);
// Determine the location of the new tower.
Point = Tower1 + Normal(Vec+Vec2) * S2;
}
我几乎可以肯定输入是正确的。我知道我没有考虑超过 90 度的角度,这可能是一个问题,但我真的不知道如何从这里开始。感谢您的任何帮助!