-1

Introducing: I'm developing a little Tower defense game in opengl, currently I'm just despairing of a little problem....

I want the projectiles from the tower to aim with the head facing the unit. So my problem is more a mathmatical one but it belongs to opengl :)

I had the following idea; I could use a dot product to get an angle rotating around the x axis to get the head depending on the distance just straight down or flat to the ground and after that an additional angle to rotate around the y axis that the head of the arrow is everytime adjusted to the unit it's aiming on.

My code for the angle of rotation around the X axis (i called it m_fYNeigung because the height(Y) of the head changes by rotating around the x axis) looks like this:

plocalTowerArray[(sizeMapIndexY * 12) + sizeMapIndexX].Projektils[byteProjectilIndex].m_fYNeigung = 
RADIANS_TO_DEGREES (acos ((float)
        (
            (faTowerPosition[0]) * (plocalTowerArray[(sizeMapIndexY * 12) + sizeMapIndexX].Projektils[byteProjectilIndex].m_faProDirectionVector[0]) +
            (faTowerPosition[1] - 1) * (plocalTowerArray[(sizeMapIndexY * 12) + sizeMapIndexX].Projektils[byteProjectilIndex].m_faProDirectionVector[1]) +
            (faTowerPosition[2]) * (plocalTowerArray[(sizeMapIndexY * 12) + sizeMapIndexX].Projektils[byteProjectilIndex].m_faProDirectionVector[2])
        )
        /
        (
            fabs (faTowerPosition[0]) * fabs (plocalTowerArray[(sizeMapIndexY * 12) + sizeMapIndexX].Projektils[byteProjectilIndex].m_faProDirectionVector[0]) +
            fabs (faTowerPosition[1] - 1) * fabs (plocalTowerArray[(sizeMapIndexY * 12) + sizeMapIndexX].Projektils[byteProjectilIndex].m_faProDirectionVector[1]) +
            fabs (faTowerPosition[2]) * fabs (plocalTowerArray[(sizeMapIndexY * 12) + sizeMapIndexX].Projektils[byteProjectilIndex].m_faProDirectionVector[2])
        )
));    

where faTowerPosition is the first vector, which is pointing down from the top of the tower (the arrow also starts at faTowerPosition[X/Y/Z]) the second vector for the dot product is m_faProDirectionVector which is a normalized direction vector describing the route of the arrow from the tower to the unit.

The Opengl Drawing part looks just as simple as this:

        for (sizeJ = 0; sizeJ < localTowerArray[sizeI].m_byteProjectilAmount; sizeJ++)
        {
            if (localTowerArray[sizeI].Projektils[sizeJ].m_bOnFlight == true)
            {
                glPushMatrix();
                glTranslatef (localTowerArray[sizeI].Projektils[sizeJ].m_faProPosition[0], localTowerArray[sizeI].Projektils[sizeJ].m_faProPosition[1], localTowerArray[sizeI].Projektils[sizeJ].m_faProPosition[2]);
                //glRotatef (360.0f - localTowerArray[sizeI].Projektils[sizeJ].m_fXNeigung, 0, 1, 0);
                glRotatef (localTowerArray[sizeI].Projektils[sizeJ].m_fYNeigung, 1, 0, 0);
                    DrawWaveFrontObject (m_pArrowProjektilObject);
                glPopMatrix();
            }
        } 

Just ignore the calculations I'm doing to the angle, I just did it to experiment with the acting of the arrows, i just noticed that it appears as would the arrow act different depending on the (i gotta say: the buildable map is scaled by x: -3.4 to 3.4 and z from 4 to -4) cords the tower was builded on -x/z,-z/x,z/x,-z/-x all these cases i guess are different and at least depending on the unit is running left or right side of the tower, the acting is also different.... so what i forgot to remind by using the dot product in this way?

4

1 回答 1

2

首先,你的代码很难理解,所以我猜了很多试图回答你。如果我认为有问题,我为此道歉。

我假设你想使用欧拉角旋转来正确对齐你的弹丸。因此,首先您将进行 X 旋转,然后进行 Y 旋转。

要进行 X 旋转,对于点积,您的向量必须在 YZ 平面上,并且假设您的弹丸从 Z 方向开始,您的第一个向量是 (0, 0, 1)。如您所说,第二个向量是指向单元的向量,可以用(px,py,pz)表示。您必须将此向量投影到平面 YZ 以获得点积的第二个向量,因此此向量将为 (0, py, pz)

现在,要计算点积,请应用以下公式

x1.x2+y1.y2+z1.z2 = |p1|.|p2|.cos a,其中 |p1| 和 |p2| 是向量的模(它的长度)

在这个例子中,第一个向量是单一的,但第二个不是。所以 |p2| = sqrt(py^2 +pz^2)。此后:

acos(a) = pz/sqrt(py^2 + pz^2)

这将为您提供围绕 X 轴的角度。做同样的计算来实现Y角旋转

PS。在我写完这个答案后,我注意到您使用了“fabs”功能。我猜你想找到你的第二个向量的模块,但是 fabs 给你一个 escalar 的绝对值。要计算向量的模块(其长度),您需要使用上面引用的公式。

于 2013-05-03T21:35:35.063 回答