c++ - Adjusting glRotate, using dot product -


introducing: i'm developing little tower defense game in opengl, i'm despairing of little problem....

i want projectiles tower aim head facing unit. problem more mathmatical 1 belongs opengl :)

i had following idea; use dot product angle rotating around x axis head depending on distance straight down or flat ground , after additional angle rotate around y axis head of arrow everytime adjusted unit it's aiming on.

my code angle of rotation around x axis (i called m_fyneigung because height(y) of head changes rotating around x axis) looks 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 first vector, pointing down top of tower (the arrow starts @ fatowerposition[x/y/z]) second vector dot product m_faprodirectionvector normalized direction vector describing route of arrow tower unit.

the opengl drawing part looks simple this:

        (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 calculations i'm doing angle, did experiment acting of arrows, noticed appears arrow act different depending on (i gotta say: buildable map scaled x: -3.4 3.4 , z 4 -4) cords tower builded on -x/z,-z/x,z/x,-z/-x these cases guess different , @ least depending on unit running left or right side of tower, acting different.... forgot remind using dot product in way?

first @ all, code difficult understand, i'm guessing lot try answer you. if assume wrong, apologize it.

i assuming want use euler angle rotation align correctly projectiles. so, first x rotation , after that, y rotation.

to x rotation, vectors, dot product, must on yz plane , assuming projectile start @ z direction, first vector (0, 0, 1). second vector, said, vector pointing unit , expressed (px, py, pz). must project vector plane yz second vector dot product, vector (0, py, pz)

now, calculate dot product apply following formule

x1.x2+y1.y2+z1.z2 = |p1|.|p2|.cos a, |p1| , |p2| module of vector (its length)

in example, first vector unitary, second not. |p2| = sqrt(py^2 +pz^2). thereafter:

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

this give angle around x axis. same calculation achieve y angle rotation

ps. after wrote answer, noted use function "fabs". guess want find module of second vector, fabs give absolute value of escalar. calculate module of vector (its length) need use above formulae cited.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -