2

I have icons that are children of node "shelf", which is a child of "self"(the scene). I also have decorations that are children of node "vehicle", which is also a child of "self".

When an icon is dragged off of the shelf and onto the vehicle, the icon node is removed and a decoration node is spawned in its place, which is made a child of "vehicle" instead of "shelf". This is a problem: when the decoration node is spawned, it jumps far away in the scene because the coordinate systems of "shelf" and "vehicle" are incongruous.

How can I convert the new decoration node from its position in the "shelf" node to its new position in "vehicle" so that it doesn't jump?

(I can post code if you need it, but it will be very long and messy.)

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
13rave
  • 135
  • 2
  • 15
  • you want to convert from one coordinate system to another or coordinates of one view, in terms of an another view? either way, have you tried this? http://stackoverflow.com/questions/15109958/understanding-convertpointtoview – Nitin Alabur May 10 '14 at 20:38
  • umm I'm not sure. I'll have to look into views. I want to convert the coordinates of a node in its parent's coordinate system to another coordinate system of a different node. Does that make sense? :S – 13rave May 10 '14 at 20:39
  • Do both coordinate systems 0,0 start at top left? – Nitin Alabur May 10 '14 at 20:39
  • I think they start at the top left corner of the node itself, which will not be 0,0 in scene coordinates. – 13rave May 10 '14 at 20:41
  • Since you tagged the question [tag:ios], I assume that you are talking about [tag:sprite-kit] instead of [tag:scenekit] – David Rönnqvist May 11 '14 at 09:42

1 Answers1

2

Assuming you're using Sprite Kit, you can use the SKNode method convertPoint:toNode: to convert points between coordinate systems.

So for instance, as soon as dragging ends on top of the vehicleNode:

CGPoint iconPos = iconNode.position; // this is position on shelf node
iconPos = [shelfNode convertPoint:iconPos toNode:vehicleNode];

// ... remove iconNode from shelfNode
// ... make new decorationNode

decorationNode.position = iconPos; // this is now position on vehicle node

// ... add decorationNode to vehicleNode
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
radnoise
  • 253
  • 2
  • 12
  • 1
    I assume that you're talking SceneKit? You should probably say that in the subject of your question – Duncan C May 10 '14 at 20:57
  • I tried this earlier... it's not working. I'm not sure what I'm doing wrong! – 13rave May 10 '14 at 21:17
  • When I print out position before and after it is converted, this gets printed: First {81.779503, 63.055557} Second {81.779503, 63.055557} – 13rave May 10 '14 at 21:22
  • If you could post some code, that would give us a better idea of what you're doing exactly. – radnoise May 10 '14 at 21:27
  • Okay, I will give it another shot and post some code if it doesn't work :P – 13rave May 10 '14 at 22:52
  • 1
    @DuncanC Since the answer is using `SKNode` (as opposed to `SCNNode`) I'm assuming that this is supposed to be about [tag:sprite-kit]. – David Rönnqvist May 11 '14 at 09:49