Author Topic: Convert CGAffineTransform to rotate a sprite  (Read 1913 times)

UncleAcid

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 51
    • View Profile
Convert CGAffineTransform to rotate a sprite
« on: March 07, 2014, 04:32:59 PM »
Hi,

I have several XML animations that involve rotating a sprite and the animation is using CGAffineTransform (it's from the iOS source for a port I'm working on), and I can't figure out how to convert those 4 numbers into correct Z rotation for NGUI. I've gotten my sprite to rotate but it's not right (for example; should rotate 360 but kinda rocks back and forth). Any help is greatly appreciated, thanks.

Example of one of the frames:
  1. <array>
  2.     <real>0.366912841796875</real>
  3.     <real>-0.929046630859375</real>
  4.     <real>0.929046630859375</real>
  5.     <real>0.366912841796875</real>
  6. </array>
  7.  

and my reading code:
  1. List<object> transformMatrix = (List<object>)frameAction["setTransform"];
  2.                                        
  3. float a = (float)(double)transformMatrix[0];
  4. float b = (float)(double)transformMatrix[1];
  5. float c = (float)(double)transformMatrix[2];
  6. float d = (float)(double)transformMatrix[3];
  7.  
  8. item.cachedTransform.rotation = new Quaternion(); // What goes here ?
  9.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Convert CGAffineTransform to rotate a sprite
« Reply #1 on: March 08, 2014, 12:46:33 PM »
I have no idea what CGAffineTransform is, or those 4 values mean. 4 values generally imply either a Vector4, or a Quaternion. A matrix has 16 values. I can't be of much help here as it's not an NGUI question at all. Even setting the rotation of something is a Unity question. I will say this though -- you should be setting transform.localRotation, not world rotation that you're setting now.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Convert CGAffineTransform to rotate a sprite
« Reply #2 on: March 08, 2014, 08:50:02 PM »
From the looks of the values, it could be quaternions, but it's all a bit of dark magic to me.

Take a look at https://docs.unity3d.com/Documentation/ScriptReference/Quaternion.html

And set the local rotation so it's set in relation to the hierarchy.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Convert CGAffineTransform to rotate a sprite
« Reply #3 on: March 09, 2014, 09:31:58 AM »
The thing about quaternions is that they differ if the coordinate system is left-handed vs right-handed.