Need help converting rotation vectors

Talk about general things concerning Forged Alliance Forever.

Moderators: FtXCommando, Ze Dogfather

Need help converting rotation vectors

Postby Resin_Smoker » 23 Jan 2015, 13:20

When running the following script:

Code: Select all
do  -- start of non-destructive hook (defaultunits.lua)
local util = import('utilities.lua')

#-------------------------------------------------------------
#  CONSTRUCTION UNITS
#-------------------------------------------------------------
local oldConstructionUnit = ConstructionUnit
ConstructionUnit = Class( oldConstructionUnit ) {

   DoTakeDamage = function( self, instigator, amount, vector, damageType )

      local heading = self:GetHeading()
      local bone = self:GetBoneDirection( 0 )
      local ori = self:GetOrientation()
      local dmghead = math.atan2( vector[1], vector[3] )

      LOG('***********************')
      LOG(' heading: ', repr( heading ) )
      LOG(' bone: ', repr( bone ) )
      LOG(' orientation: ', repr( ori ) )
      LOG(' dmg vector: ', repr( vector ) )
      LOG(' dmghead math.atan2 ( x & y ): ', repr( dmghead ) )

      oldConstructionUnit.DoTakeDamage( self, instigator, amount, vector, damageType )
   end,

}

end -- end of non-destructive hook


I see the following output...
Spoiler: show
INFO: ***********************
INFO: heading: \0000.0011143079027534
INFO: bone: \0000.0011644108453766
INFO: orientation: \000{ <metatable=table: 1AAAC398>
INFO: -0.031980533152819,
INFO: -0.00023144396254793,
INFO: -0.024617159739137,
INFO: 0.99918532371521
INFO: }
INFO: dmg vector: \000{ <metatable=table: 1AAAC398>
INFO: 0.44805908203125,
INFO: -0.04585075378418,
INFO: 0.111083984375
INFO: }
INFO: dmghead math.atan2 ( x & y ): \0001.327773809433


What i'm attempting to do here is determine the angle of a projectiles impact relative to the unit it impacted. With this it should be possible to know if the front / side / rear facing of a unit was impacted.

While the impact vector is three axis (dmg vector) the unit orientation is four (quad) vector. Hence I can not use this data without first converting either data set before using it for a comparison. However my issue is that I've next to no experience with this and would love some advice / help on finding the solution.

Resin
Last edited by Resin_Smoker on 23 Jan 2015, 13:58, edited 1 time in total.
Resin_Smoker
Evaluator
 
Posts: 858
Joined: 14 Mar 2012, 17:58
Has liked: 54 times
Been liked: 106 times

Re: Need help converting vectors from one type to another

Postby Resin_Smoker » 23 Jan 2015, 13:44

Found a very interesting webpage with all sorts of conversions... https://github.com/grrrwaaa/gct753/blob ... s/quat.lua

Resin
Resin_Smoker
Evaluator
 
Posts: 858
Joined: 14 Mar 2012, 17:58
Has liked: 54 times
Been liked: 106 times

Re: Need help converting rotation vectors

Postby Resin_Smoker » 23 Jan 2015, 14:13

So far this is producing a viable output...

Code: Select all
   QuatToEuler = function( self, quat )      
      local x, y, z, w = unpack( quat )   
      local sqx = x * x
      local sqy = y * y
      local sqz = z * z
      local sqw = w * w
      local euler = {}            
      euler[1] = math.asin(-2.0 * ( x * z - w * y ) )
      euler[2] = math.atan2( 2.0 * ( y * z + w * x ), ( sqw - sqx - sqy + sqz ) )
      euler[3] = math.atan2( 2.0 * ( x * y + w * z ), ( sqw + sqx - sqy - sqz ) )
      return euler
   end,


Now to figure out a way to use it best.

Resin
Resin_Smoker
Evaluator
 
Posts: 858
Joined: 14 Mar 2012, 17:58
Has liked: 54 times
Been liked: 106 times

Re: Need help converting rotation vectors

Postby Resin_Smoker » 23 Jan 2015, 14:20

One thing that I've noticed, it appears that the target unit that i'm attacking is producing an orientation output that is not static despite the face that the unit is stationary. Check out the QuatToEuler output log...

Spoiler: show
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.45103454589844,
INFO: -0.055665969848633,
INFO: 0.27105712890625
INFO: }
INFO: quat to euler: \000-0.00035091885365546\0000.031428750604391\0000.019819026812911
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.45115661621094,
INFO: -0.042640686035156,
INFO: 0.26837158203125
INFO: }
INFO: quat to euler: \000-0.00061906321207061\0000.034089740365744\0000.027962151914835
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.45132446289063,
INFO: -0.041934967041016,
INFO: 0.194580078125
INFO: }
INFO: quat to euler: \000-0.00063757202588022\0000.047590512782335\0000.028991591185331
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.45062255859375,
INFO: -0.037351608276367,
INFO: 0.19329833984375
INFO: }
INFO: quat to euler: \0000.00017512885096949\0000.048699889332056\0000.012716017663479
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.45040893554688,
INFO: -0.067955017089844,
INFO: 0.106201171875
INFO: }
INFO: quat to euler: \0000.00049650622531772\0000.034507818520069\0000.0058859814889729
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.45169067382813,
INFO: -0.080066680908203,
INFO: -0.044891357421875
INFO: }
INFO: quat to euler: \0001.8253631424159e-005\0000.030404286459088\0000.021862957626581
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.45437622070313,
INFO: -0.14375495910645,
INFO: -0.06109619140625
INFO: }
INFO: quat to euler: \000-0.0002092887298204\0000.044387925416231\0000.02873201481998
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.45120239257813,
INFO: -0.047054290771484,
INFO: -0.25149536132813
INFO: }
INFO: quat to euler: \0005.4799951612949e-005\0000.035529766231775\0000.022493848577142
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.450927734375,
INFO: -0.071207046508789,
INFO: -0.2886962890625
INFO: }
INFO: quat to euler: \0000.00029197707772255\0000.023598846048117\0000.012560334987938
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.45088195800781,
INFO: -0.063501358032227,
INFO: -0.29876708984375
INFO: }
INFO: quat to euler: \0000.00016640903777443\0000.041170116513968\0000.015513479709625
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.45259094238281,
INFO: -0.063764572143555,
INFO: -0.29989624023438
INFO: }
INFO: quat to euler: \000-0.00076583976624534\0000.050464242696762\0000.034828823059797
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.4527587890625,
INFO: -0.063791275024414,
INFO: -0.30001831054688
INFO: }
INFO: quat to euler: \000-0.00074305635644123\0000.041224215179682\0000.033959068357944
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.45089721679688,
INFO: -0.063503265380859,
INFO: -0.29879760742188
INFO: }
INFO: quat to euler: \000-5.0702947191894e-006\0000.026482362300158\0000.010820140130818
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.44996643066406,
INFO: -0.063360214233398,
INFO: -0.29815673828125
INFO: }
INFO: quat to euler: \0000.0002496785891708\0000.030508734285831\0000.0011575418757275
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.45124816894531,
INFO: -0.063556671142578,
INFO: -0.29901123046875
INFO: }
INFO: quat to euler: \000-0.0004417872405611\0000.0404985062778\0000.019916355609894
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.4530029296875,
INFO: -0.063827514648438,
INFO: -0.3001708984375
INFO: }
INFO: quat to euler: \000-0.0011352526489645\0000.034660559147596\0000.037413485348225
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.45234680175781,
INFO: -0.063724517822266,
INFO: -0.29974365234375
INFO: }
INFO: quat to euler: \000-0.00081377703463659\0000.031360380351543\0000.027428731322289
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.45072937011719,
INFO: -0.0634765625,
INFO: -0.29867553710938
INFO: }
INFO: quat to euler: \000-0.0002150805958081\0000.035355776548386\0000.0092899054288864
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.45115661621094,
INFO: -0.063543319702148,
INFO: -0.2989501953125
INFO: }
INFO: quat to euler: \000-0.00045102796866558\0000.029411427676678\0000.016609160229564
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.45220947265625,
INFO: -0.063705444335938,
INFO: -0.29965209960938
INFO: }
INFO: quat to euler: \000-0.00083460239693522\0000.037723042070866\0000.028960956260562
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.451904296875,
INFO: -0.063655853271484,
INFO: -0.2994384765625
INFO: }
INFO: quat to euler: \000-0.00055513269035146\0000.051534995436668\0000.023467356339097
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.45072937011719,
INFO: -0.0634765625,
INFO: -0.29867553710938
INFO: }
INFO: quat to euler: \0006.5726992033888e-005\0000.038912989199162\0000.01055018696934
INFO: ***********************
INFO: dmg vector: \000{ <metatable=table: 14EF9028>
INFO: 0.45088195800781,
INFO: -0.063501358032227,
INFO: -0.29876708984375
INFO: }
INFO: quat to euler: \000-2.3692831746303e-005\0000.021091366186738\0000.013953930698335


Keep in mind that i am comparing the projectiles impact vectors (dmg vector) with the units current orientation vectors (quat to eular).
Resin_Smoker
Evaluator
 
Posts: 858
Joined: 14 Mar 2012, 17:58
Has liked: 54 times
Been liked: 106 times

Re: Need help converting rotation vectors

Postby Vee » 23 Jan 2015, 14:22

A simple way to determine the angle between two vectors is by using the inner product. Given vectors v and w, we have:

Code: Select all
v*w = |v| |w| cos a


Where |v|, |w| are the length of the vectors, and a is the angle, and v*w is the inner product (vx*wx + vy*wy +vz*wz). If your vectors are direction vectors they have length 1, so then it's simply v*w = cos a. If that is 0 then v and w are perpendicular, if it's positive then they are pointing in a similar direction, and if it's negative they are pointing in an opposite direction.

BTW, what do you mean by quad vector? Is that a vector in homogeneous coordinates?
Vee
Evaluator
 
Posts: 677
Joined: 04 Dec 2013, 20:43
Has liked: 275 times
Been liked: 225 times
FAF User Name: Vee

Re: Need help converting rotation vectors

Postby Resin_Smoker » 23 Jan 2015, 14:27

Shit i was right, the orientation data from GetOrientation() changes constantly even if the unit is not moving! Apparently hover units wobble all over the place making the orientation vary from one moment to the next. Units such as the UEF engineers do not hover and hence the output does not vary in the slightest.

Looks like i will be basing the rest of my work off of the UEF engineers until i have a working script i can test with hover units.

Resin
Resin_Smoker
Evaluator
 
Posts: 858
Joined: 14 Mar 2012, 17:58
Has liked: 54 times
Been liked: 106 times

Re: Need help converting rotation vectors

Postby Resin_Smoker » 23 Jan 2015, 14:29

Vee wrote:BTW, what do you mean by quad vector? Is that a vector in homogeneous coordinates?


Quaternion old chap!

x,y,z,w = quad or four

Resin
Resin_Smoker
Evaluator
 
Posts: 858
Joined: 14 Mar 2012, 17:58
Has liked: 54 times
Been liked: 106 times

Re: Need help converting rotation vectors

Postby Resin_Smoker » 23 Jan 2015, 14:31

Vee any ideas on how we can check for front, rear and side impacts now that I have the proper Quat-to-Euler conversions?

Front impact, dmg reduction.
Side impact, normal dmg.
Rear impact, dmg increase.

ect...

Resin
Resin_Smoker
Evaluator
 
Posts: 858
Joined: 14 Mar 2012, 17:58
Has liked: 54 times
Been liked: 106 times

Re: Need help converting rotation vectors

Postby Vee » 23 Jan 2015, 14:38

Ah, quaternions. I was confused by the d in quad. In that case it's probably simplest to rotate the impact vector by the inverse of the orientation quaternion. It's highly likely that there is a library function for that in the game already, since that's the whole point of quaternions. Then you can look at the x,y coordinates of the resulting vector to determine from which direction it's coming.

I don't think converting to euler angles is going to help you, since at the end you still want to rotate the impact vector, which is actually easier with quaternions.

Is there a list of lua library functions provided by the game?

BTW, based only on the orientation and damage and the impact vector you can't actually determine 100% which side was hit. It could well be that the projectile came pretty much from the front direction, but it hit the side of the unit.
Vee
Evaluator
 
Posts: 677
Joined: 04 Dec 2013, 20:43
Has liked: 275 times
Been liked: 225 times
FAF User Name: Vee

Re: Need help converting rotation vectors

Postby Resin_Smoker » 23 Jan 2015, 15:51

Vee wrote:
BTW, based only on the orientation and damage and the impact vector you can't actually determine 100% which side was hit. It could well be that the projectile came pretty much from the front direction, but it hit the side of the unit.


Yeah splash damage is always going to be an issue with this, regardless of where it came from...

Resin
Resin_Smoker
Evaluator
 
Posts: 858
Joined: 14 Mar 2012, 17:58
Has liked: 54 times
Been liked: 106 times

Next

Return to General Discussions

Who is online

Users browsing this forum: No registered users and 1 guest