fuzzygoat
fuzzygoat

Reputation: 26223

Positioning new SKSpriteNodes at corners of existing rotated SKSpriteNode

I need to spawn 4 new SKSpriteNodes (independent not parented) at the corners of an existing sprite. Its pretty basic stuff but I am struggling to find the correct formula, I though I had it but its not working as expected. Could some either point me towards the correct formula or help me out with some guidance on calculating the 4 [x, y] pairs for any given rotation. Much appreciated.

enter image description here

  1. Move rotation center for each point to origin.
  2. xnew = x * cos(angle) - y * sin(angle)
  3. ynew = y * cos(angle) + x * sin(angle)
  4. Move rotation center for each point back to original position.

Upvotes: 0

Views: 489

Answers (1)

Matt
Matt

Reputation: 4989

You could use CGPointApplyAffineTransform along with CGAffineTransformMakeRotation if you didn't want to do the math yourself. Your math though looks almost right at first glance, I believe you just need to subtract out the center point. So xnew would be (x - x_c) * cos(a) - (y - y_c) * sin(a). Can't test right now so can't be 100% sure.

Upvotes: 1

Related Questions