Reputation: 95
I have a node with the shape of a box in a scene. The shape node should have its own coordinate space, so if I declare a point at (100,100) in the nodes coordinate space and rotate the box pi/4 rad the point should change in the scenes coordinate space.
My problem is I cant get this to work, I'm using the following code to convert a point in the nodes coord space to the scenes coord space, the placement of the node is 200,200 in the scene:
startPointInNodeSpace = CGPointMake(0, size.height/2);
CGPoint start = [self.scene convertPoint:CGPointMake(startPointInNodeSpace.x, startPointInNodeSpace.y) fromNode:self.parent];
CGPoint end = [self.scene convertPoint:CGPointMake(startPointInNodeSpace.x, startPointInNodeSpace.y + 100) fromNode:self.parent];
NSLog(@"start position in node: (%f,%f)\nend position in node: (%f,%f)",startPointInNodeSpace.x,startPointInNodeSpace.y,startPointInNodeSpace.x,startPointInNodeSpace.y + 100);
NSLog(@"start position in scene: (%f,%f)\nend position in scene: (%f,%f)",start.x,start.y,end.x,end.y);
The code is inside a subclass of SKSpriteNode
.
My log:
start position in node: (0.000000,20.000000)
end position in node: (0.000000,120.000000)
start position in scene: (0.000000,20.000000)
end position in scene: (0.000000,120.000000)
As you can see it is not converting the points at all but I dont know what I am doing wrong.
Visual Illustration of what I want to achieve:
Upvotes: 0
Views: 1816
Reputation: 95
I was using the following code in the scene to rotate the node:
SKAction *rotation = [SKAction rotateByAngle: M_PI/4.0 duration:0];
[MYNODE runAction: rotation];
When I changed it to this inside the nodeclass instead it worked as expected:
self.zRotation = M_PI/4;
Upvotes: 0
Reputation: 5655
I think the problem is that you're converting from self.parent
instead of self
— the node's parent is the scene, so there's no conversion to be done. Try converting from self
instead:
startPointInNodeSpace = CGPointMake(0, size.height/2);
endPointInNodeSpace = CGPointMake(startPointInNodeSpace.x, startPointInNodeSpace.y + 100);
CGPoint start = [self.scene convertPoint:startPointInNodeSpace fromNode:self];
CGPoint end = [self.scene convertPoint:endPointInNodeSpace fromNode:self];
(Note that you can just pass startPointInNodeSpace
into the method, rather than creating an identical point. Structs are copied by value, so the method can't mutate your variable.)
Upvotes: 2