# 3D Calculations

## Recommended Posts

Introduction:

Hello.

Im working on the World Of Warcraft Advanced Machinima Tool but need some help with some of the 3D Calculations i use.

We have the cameras position (X,Y,Z) and rotation (X,Z)

They are defined as: posx1 posy1 posz1

And the target is defined as posx2 posy2 posz2

What i want to do is to have the camera always facing towards the target.

So i need to calculate the Rotaion X and Z for the camera.

In my head that sounds very simple but it does not work that well.

The problem:

The X and Z rotation is not defined in 360degrees.

Both goes from 0 to 6.2833 where 6.2833 is 360

And not only that, the X rotation is counterclockwise while the Z rotation is clockwise

You can see it here:

The main Coord system is mirrored.

And the camera rotation is also mirrored.

Let take a example, here is a little 3D space.

Now to calculate the X rotation i need the X and Y values of both target and camera.

```\$radToDeg = -180 / \$pi
\$result = atan((\$posy1 - \$posy2) / (\$posx2 - \$posx1)) * \$radToDeg
if \$posx2 < \$posx1 Then
if \$result < 0 Then
\$result = 270 + \$result
Else
\$result = 270 + \$result
EndIf
EndIf
if \$posx2 >= \$posx1 Then
if \$result < 0 Then
\$result = 90 + \$result
Else
\$result = 90 + \$result
EndIf
EndIf
\$result = \$result - 360
\$result = ((\$result + 360 - (\$result * 2)) + 180)

\$RotCalcX = \$result /  57.294733659064504320977830121115 ;Scaled down to the 0 to 6.2833 ratiooÝ÷ ØÚ0¶¬¥ªí±ç¦¶Þ×M¢ºÞrÕ¡zx&«¢éÝ¶­j¸¶Ø^^º-jØ¨+²±º.Ûh¶­j¸´­¶®Z¶*'ÆÛkÉ2J)Üz)ÞyÛh©q©ºV­ì~Û¢ØXjwYêÞË(ë­ª¡×¢§^Â
±Õ±ÑI=ÑÑ¥½¸hÄ(\$\$\$ÀÌØíÉÍÕ±ÐÈôÑ¸  ÀÌØíÁ½ÍèÄ´ÀÌØíÁ½ÍhÈ¤¼ ÀÌØíÁ½ÍàÈ´ÀÌØíÁ½ÍàÄ¤¤¨ÀÌØíÉQ½(\$\$%¥ÀÌØíÁ½ÍàÈ±ÐìÀÌØíÁ½ÍàÄQ¡¸(\$\$\$%¥ÀÌØíÉÍÕ±ÐÈ±ÐìÀQ¡¸(\$\$\$\$\$ÀÌØíÉÍÕ±ÐÈôÈÜÀ¬ÀÌØíÉÍÕ±ÐÈ(\$\$\$%±Í(\$\$\$\$\$ÀÌØíÉÍÕ±ÐÈôäÀ´ÀÌØíÉÍÕ±ÐÈ(\$\$\$%¹%(\$\$%¹%(\$\$%¥ÀÌØíÁ½ÍàÈÐìôÀÌØíÁ½ÍàÄQ¡¸(\$\$\$%¥ÀÌØíÉÍÕ±ÐÈ±ÐìÀQ¡¸(\$\$\$\$\$ÀÌØíÉÍÕ±ÐÈôäÀ¬ÀÌØíÉÍÕ±ÐÈ(\$\$\$%±Í(\$\$\$\$\$ÀÌØíÉÍÕ±ÐÈôäÀ¬ÀÌØíÉÍÕ±ÐÈ(\$\$\$%¹%(\$\$%¹%((\$\$\$ÀÌØíÉÍÕ±ÐÈôÀÌØíÉÍÕ±ÐÈ´ÌØÀ(\$\$\$ÀÌØíÉÍÕ±ÐÈôÀÌØíÉÍÕ±ÐÈ¬´ÀÌØíÉÍÕ±ÐÈ¬´ÀÌØíÉÍÕ±ÐÈíQÕÉ¸IÍÕ±ÐÑ¼±½­Ý¥ÍÉÍÕ±Ð(\$\$\$ÀÌØíÉÍÕ±ÐÈôÀÌØíÉÍÕ±ÐÈ¼ÔÜ¸ÈäÐÜÌÌØÔäÀØÐÔÀÐÌÈÀäÜÜàÌÀÄÈÄÄÄÔ(\$\$\$ÀÌØíÉÍÕ±ÐÈôÀÌØíÉÍÕ±ÐÈ¬Ä¸ÔÜÀàÈÔ(\$\$%¥ÀÌØíÉÍÕ±ÐÈÐìØ¸ÈàÌÌQ¡¸(\$\$\$\$ÀÌØíÉÍÕ±ÐÈôÀÌØíÉÍÕ±ÐÈ´Ø¸ÈàÌÌ(\$\$%¹%(\$\$%¥ÀÌØíÉÍÕ±ÐÈ±ÐìÀQ¡¸(\$\$\$\$ÀÌØíÉÍÕ±ÐÈôØ¸ÈàÌÌ¬ÀÌØíÉÍÕ±ÐÈ(\$\$%¹%(í
±Õ±ÑI=ÑÑ¥½¸hÈ(\$\$\$ÀÌØíÉÍÕ±ÐÌôÑ¸  ÀÌØíÁ½ÍèÄ´ÀÌØíÁ½ÍhÈ¤¼ ÀÌØíÁ½ÍäÈ´ÀÌØíÁ½ÍäÄ¤¤¨ÀÌØíÉQ½(\$\$%¥ÀÌØíÁ½ÍäÈ±ÐìÀÌØíÁ½ÍäÄQ¡¸(\$\$\$%¥ÀÌØíÉÍÕ±ÐÌ±ÐìÀQ¡¸(\$\$\$\$\$ÀÌØíÉÍÕ±ÐÌôÈÜÀ¬ÀÌØíÉÍÕ±ÐÌ(\$\$\$%±Í(\$\$\$\$\$ÀÌØíÉÍÕ±ÐÌôäÀ´ÀÌØíÉÍÕ±ÐÌ(\$\$\$%¹%(\$\$%¹%(\$\$%¥ÀÌØíÁ½ÍäÈÐìôÀÌØíÁ½ÍäÄQ¡¸(\$\$\$%¥ÀÌØíÉÍÕ±ÐÌ±ÐìÀQ¡¸(\$\$\$\$\$ÀÌØíÉÍÕ±ÐÌôäÀ¬ÀÌØíÉÍÕ±ÐÌ(\$\$\$%±Í(\$\$\$\$\$ÀÌØíÉÍÕ±ÐÌôäÀ¬ÀÌØíÉÍÕ±ÐÌ(\$\$\$%¹%(\$\$%¹%((\$\$\$ÀÌØíÉÍÕ±ÐÌôÀÌØíÉÍÕ±ÐÌ´ÌØÀ(\$\$\$ÀÌØíÉÍÕ±ÐÌôÀÌØíÉÍÕ±ÐÌ¬´ÀÌØíÉÍÕ±ÐÌ¬´ÀÌØíÉÍÕ±ÐÌíQÕÉ¸IÍÕ±ÐÑ¼±½­Ý¥ÍÉÍÕ±Ð(\$\$\$ÀÌØíÉÍÕ±ÐÌôÀÌØíÉÍÕ±ÐÌ¼ÔÜ¸ÈäÐÜÌÌØÔäÀØÐÔÀÐÌÈÀäÜÜàÌÀÄÈÄÄÄÔ(\$\$\$ÀÌØíÉÍÕ±ÐÌôÀÌØíÉÍÕ±ÐÌ¬Ä¸ÔÜÀàÈÔ(\$\$%¥ÀÌØíÉÍÕ±ÐÌÐìØ¸ÈàÌÌQ¡¸(\$\$\$\$ÀÌØíÉÍÕ±ÐÌôÀÌØíÉÍÕ±ÐÌ´Ø¸ÈàÌÌ(\$\$%¹%(\$\$%¥ÀÌØíÉÍÕ±ÐÌ±ÐìÀQ¡¸(\$\$\$\$ÀÌØíÉÍÕ±ÐÌôØ¸ÈàÌÌ¬ÀÌØíÉÍÕ±ÐÌ(\$\$%¹%((\$\$\$ÀÌØíI½Ñ
±hô ÀÌØíÉÍÕ±ÐÈ¼È¤¬ ÀÌØíÉÍÕ±ÐÌ¼È¤ì!½ÜÍ¡½Õ±¤¥¹Ñ¡ÍÈÑ½Ñ¡Èü```
Edited by malu05

[center][u]WoW Machinima Tool[/u] (Tool for Machinima Artists) [/center]

##### Share on other sites

The X and Z rotation is not defined in 360degrees.

Both goes from 0 to 6.2833 where 6.2833 is 360

And not only that, the X rotation is counterclockwise while the Z rotation is clockwise

6.2833 = 2 pi

See unit circle:

http://en.wikipedia.org/wiki/Unit_circle

##### Share on other sites

6.2833 = 2 pi

See unit circle:

http://en.wikipedia.org/wiki/Unit_circle

Ahhh... didnt know that part. ^^

But im still not sure how to calculate the Camera rotation around the X axis

And how the 2 seperate calculations should be joined? (or if it even is 2 calculations)

[center][u]WoW Machinima Tool[/u] (Tool for Machinima Artists) [/center]

##### Share on other sites

RotZ:

0° = 0 pi (Horizon)

90° = pi / 2 = ~1.5 (Ground)

180° = pi = 3.14 (Rear Horizon - Upside-down)

270° = 3pi/2 = 4.71 (Sky)

360° = 2pi = 6.28 (Horizon)

##### Share on other sites

RotZ:

0° = 0 pi (Horizon)

90° = pi / 2 = ~1.5 (Ground)

180° = pi = 3.14 (Rear Horizon - Upside-down)

270° = 3pi/2 = 4.71 (Sky)

360° = 2pi = 6.28 (Horizon)

I think you miss understand.

Lets say the camera is located at -300 , 200 , 10 and the target is located at -351 , 240 , 11

How do i calculate the rotaion around the X axis?

[center][u]WoW Machinima Tool[/u] (Tool for Machinima Artists) [/center]

##### Share on other sites

I think you miss understand.

Lets say the camera is located at -300 , 200 , 10 and the target is located at -351 , 240 , 11

How do i calculate the rotaion around the X axis?

No I understand. I am helping you to understand the concept of radians so you can complete your task.

Edited by weaponx

##### Share on other sites

No I understand. I am helping you to understand the concept of radians so you can complete your task.

I found this on geocities.

```x2-x1                        y2-y1
tg(Yan)= -----  , tg(Zan)= ---------------------------
z1-z2           sqrt((x2-x1)^2 + (z2-z1)^2)```

Where the Yangel matches my calculation but the other calculation also gives a invalid result.

[center][u]WoW Machinima Tool[/u] (Tool for Machinima Artists) [/center]

##### Share on other sites

Seems like the first calculation i made can be used with some tweak. Tho i still would love a more clean calculation if someone knows how it should be done.

The first calculation devide's X and Y rotation by 2 and multiply them.

There is currently only 4 angels that are corrupt and that is when the X or Y position are close equal

```Correct

Cameraposition =    -134.572677612305 .     -8935.6123046875 .  143.96760559082
Target Position =   -172.489761352539 .     -8899.083984375 .   144.477661132813

zx= 6.26973425214815 -      zy= 6.26922292422834
rotation set            = 6.26947858818825

Cameraposition =    -259.972808837891 .     -8795.4580078125 .  166.467803955078
Target Position =   -172.489761352539 .     -8899.083984375 .   144.477661132813

zx= 0.246262596623854 -     zy= 0.209104898862212
rotation set            = 0.227683747743033```

In both of these examples the zx and zy are quite close.

```WRONG:

Cameraposition =    -172.497985839844 .     -8950.833984375 .   143.96760559082
Target Position =   -172.489761352539 .     -8899.083984375 .   144.477661132813

zx= 4.72851227272154 -      zy= 6.27332948057586
rotation set            = 5.5009208766487

Cameraposition =    -192.866882324219 .     -8900.1630859375 .  150.919052124023
Target Position =   -172.489761352539 .     -8899.083984375 .   144.477661132813

zx= 0.306169381117432 -     zy= 1.40481149256352
rotation set            = 0.855490436840475```

In the first example the camX position is within 1 coord of the targetX position, this results in a drop in the zx contra the zy.

In the sec example the camY position is within 2 coord of the targetY position, this results in a drop in the zy contra the zx.

so i guess i got a idea what the problem is.

So the fix was like this:

```if _max(\$posx1,\$posx2) - _min(\$posx1,\$posx2) > _max(\$posy1,\$posy2) - _min(\$posy1,\$posy2) Then
\$RotCalcB = \$result2
Else
\$RotCalcB = \$result3
EndIfoÝ÷ ØÚ0¶«z++mæèm¸§íî9×~vÛ§w}yJayÆ¥réZ¶*'±Ø}êß¢»(ì·ky«(*"zw^vêpØm+)ç¡ü"z·¡×ºÚ"µÍZYÛX^
ÌÍÜÜÞK ÌÍÜÜÞHHÛZ[    ÌÍÜÜÞK ÌÍÜÜÞH ÝÈÛX^
ÌÍÜÜÞLK    ÌÍÜÜÞLHHÛZ[   ÌÍÜÜÞLK    ÌÍÜÜÞLH[BBIÌÍÜÝ[H][
ÌÍÜÜÞHH    ÌÍÜÜÖHÈ
ÌÍÜÜÞH ÌÍÜÜÞJJH
ÌÍÜYÑYÂBBZY    ÌÍÜÜÞ  È  ÌÍÜÜÞH[BBBZY   ÌÍÜÝ[   È[BBBBIÌÍÜÝ[HL
È  ÌÍÜÝ[BBBQ[ÙBBBBBIÌÍÜÝ[HLH  ÌÍÜÝ[BBBQ[YBBQ[YBBZY    ÌÍÜÜÞ  ÝÏH   ÌÍÜÜÞH[BBBZY   ÌÍÜÝ[   È[BBBBIÌÍÜÝ[HLH    ÌÍÜÝ[BBBQ[ÙBBBBBIÌÍÜÝ[HL
È  ÌÍÜÝ[BBBQ[YBBQ[YBBIÌÍÜÝ[H   ÌÍÜÝ[HÍBBIÌÍÜÝ[H   ÌÍÜÝ[
ÈIÌÍÜÝ[
ÈIÌÍÜÝ[BBIÌÍÜÝ[H   ÌÍÜÝ[È
ÍÊ    ÌÍÜJJBBBIÌÍÜÝ[H  ÌÍÜÝ[
È
ÌÍÜKÌBBBZY  ÌÍÜÝ[   ÝÈ
ÌÍÜJH[BBBIÌÍÜÝ[H ÌÍÜÝ[H
ÌÍÜJBBBQ[YBBZY   ÌÍÜÝ[   È[BBBIÌÍÜÝ[H
ÌÍÜJJÉÌÍÜÝ[BBQ[YBBIÌÍÔÝØ[ÐH   ÌÍÜÝ[BQ[ÙBÐØ[Ý[]HÝ][ÛBBIÌÍÜÝ[ÈH][
ÌÍÜÜÞHH    ÌÍÜÜÖHÈ
ÌÍÜÜÞLH    ÌÍÜÜÞLJJH
ÌÍÜYÑYÂBBZY    ÌÍÜÜÞ  È  ÌÍÜÜÞH[BBBZY   ÌÍÜÝ[È È[BBBBIÌÍÜÝ[ÈHL
È  ÌÍÜÝ[ÂBBBQ[ÙBBBBBIÌÍÜÝ[ÈHLH  ÌÍÜÝ[ÂBBBQ[YBBQ[YBBZY  ÌÍÜÜÞ  ÝÏH   ÌÍÜÜÞH[BBBZY   ÌÍÜÝ[È È[BBBBIÌÍÜÝ[ÈHLH  ÌÍÜÝ[ÂBBBQ[ÙBBBBBIÌÍÜÝ[ÈHL
È  ÌÍÜÝ[ÂBBBQ[YBBQ[YBBIÌÍÜÝ[ÈH   ÌÍÜÝ[ÈHÍBBIÌÍÜÝ[ÈH   ÌÍÜÝ[È
ÈIÌÍÜÝ[È
ÈIÌÍÜÝ[ÂBBIÌÍÜÝ[ÈH   ÌÍÜÝ[ÈÈ
ÍÊ    ÌÍÜJJBBBIÌÍÜÝ[ÈH    ÌÍÜÝ[È
È
ÌÍÜKÌBBBZY  ÌÍÜÝ[È ÝÈ
ÌÍÜJH[BBBIÌÍÜÝ[ÏH   ÌÍÜÝ[ÈH
ÌÍÜJBBBQ[YBBZY   ÌÍÜÝ[È È[BBBIÌÍÜÝ[ÏH
ÌÍÜJJÉÌÍÜÝ[ÂBBQ[YBBIÌÍÔÝØ[ÐH ÌÍÜÝ[ÂBQ[Y```
Edited by malu05

[center][u]WoW Machinima Tool[/u] (Tool for Machinima Artists) [/center]

## Create an account

Register a new account