0

http://i.imgur.com/3OAAH.jpg

我画了这个惊人的图表来展示我想要计算的东西。如果你看不出来,这些是城堡的城墙和塔楼。为了“关闭”城堡,我需要找出两条红线相交的点,这样就可以建造一座塔将两堵墙连接在一起。

为了解决这个问题,墙的大小是固定的,因此所有三个边长都是已知的。这意味着您可以使用余弦定律找出其中一个旋转墙与静态墙的角度。但是,我尝试实现它,但无法使其正常工作。

这是代码:

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 度的角度,这可能是一个问题,但我真的不知道如何从这里开始。感谢您的任何帮助!

4

2 回答 2

0

你想要的是A三角形边的角度S1S2S3。你可以使用余弦定律得到

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)
于 2012-06-14T14:54:19.183 回答
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;
}
于 2012-06-14T15:34:28.880 回答