我画了这个惊人的图表来展示我想要计算的东西。如果你不知道,这些是城堡的墙壁和塔楼。为了“关闭”城堡,我需要找出两条红线相交的点,以便可以将一座塔连接在一起。
出于这个问题,壁的尺寸是固定的,因此所有三个边长都是已知的。这意味着您可以使用余弦定律找出其中一个旋转墙与静态墙的角度。但是,我尝试实现它,但我无法正常工作。
以下是代码:
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度的角度,这可能是一个问题,但我真的不知道如何从这里开始。谢谢你的帮助!
答案 0 :(得分:0)
您想要的是三角边A
,S1
和S2
的角度S3
。您可以使用law of cosines获取
A = ACOS( (S1*S1+S2*S2-S3*S3)/(2*S1*S2) )
如果知道底壁的方向(S1),则找到顶点的坐标。
TH = ATAN( (Tower2.Y-Tower1.Y)/(Tower2.X-Tower1.X) )
Tower3.X = S2*COS(A+TH)
Tower3.Y = S2*SIN(A+TH)
答案 1 :(得分:0)
给它一个旋转:
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 Vec1;
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("--------------------- Angle in degrees" $ (A* 180.f) / M_PI $ " ---------------------");
// Get a vector angle between up and S1.
Vec1 = Normal(Tower2-Tower1);
// rotate the normalized vector around axis (0,1,0) (assuming Unreals co-ordinate system is x,y,z and Y is UP
local quat quatRot = QuatFromAxisAndAngle( Vect(0,0,1), -A ); //angle accepted in radians NOTE: Could just be A instead of -A, depends on which way the point will rotate
local vector corectedS2Vector = Normal ( QuatRotateVector ( quatRot , Vec1 ));
// Determine the location of the new tower.
Point = Tower1 + corectedS2Vector * S2;
}