0

好吧,所以我有一个node.size是(5,5)矩形的节点。它的位置是(100,100),其锚点是默认值(0.5,0.5)。旋转后在节点上的位置

我试图找到最右边的点,但仍然居中Y.自然,这返回了点(102.5,100)。太棒了。我通过简单地使用CGPointMake(node.position + node.size.width/2, node.position);

发现了这个点,但是,我想旋转节点,同时仍然知道该点是什么。我想知道他们是否可以使用节点zRotation执行某种算术,以便在旋转之后找到新点(节点帧上的相同点)。

为了说清楚我想知道节点旋转后最初102.5,100点的位置。

谢谢大家非常提前为所有的答案的

+0

寻找“左右圆/半径旋转点”或简单地添加一个节点的那个位置作为其它节点的孩子,然后转动另一个节点和你旋转 – LearnCocos2D 2014-09-29 08:13:31

+0

后得到了孩子的位置非常感谢您的建议,这正是我迄今为止所做的。我会考虑周围的圆/半径旋转点,但现在我刚刚安装在父内的点的子节点。我只是想知道,如果他们是一个更简单的,CPU密集型的过程比我每一个需要它的时候创建一个新的节点。再次感谢你。 – Ryoku 2014-09-30 04:12:23

回答

2

[R是从精灵的锚点的兴趣点的距离。在你的情况,

r = sqrt(dx*dx+dy*dy) = 2.5; 

其中DX = 2.5和DY = 0

另外,让THETA是旋转角度,这是初始为零。我们现在在极坐标上有一个点,(r,theta),表示点(2.5,0)。我们现在可以通过改变theta旋转关于精灵锚点的点。旋转精灵后,我们将([RTHETA)以直角坐标系由

x = r * cos(theta); 
y = r * sin(theta); 

,并添加精灵的场景坐标点转换位置。

x = x + sprite.position.x; 
y = y + sprite.position.y; 

编辑:我觉得这是你想实现什么。

@interface MyScene() 

@property SKSpriteNode *object1; 
@property SKSpriteNode *object2; 

@end 

@implementation MyScene 

-(id)initWithSize:(CGSize)size 
{ 
    if(self = [super initWithSize:size]) { 
     _object1 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(10,5)]; 
     _object1.position = CGPointMake(100, 100); 
     [self addChild:_object1]; 
     SKAction *wait = [SKAction waitForDuration: 2.5f]; 
     SKAction *rotate = [SKAction rotateByAngle:3*M_PI/4 duration:2.0f]; 
     SKAction *addObject = [SKAction runBlock:^{ 
      _object2 = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(10,5)]; 

      _object2.zRotation = _object1.zRotation; 

      CGFloat dx = _object1.size.width/2; 
      CGFloat dy = 0; 
      CGFloat r = sqrtf(dx*dx + dy*dy); 
      CGFloat x = r*cosf(_object1.zRotation); 
      CGFloat y = r*sinf(_object1.zRotation); 

      _object2.position = CGPointMake(_object1.position.x + 2*x, _object1.position.y + 2*y); 

      [self addChild:_object2]; 
     }]; 
     SKAction *seq = [SKAction sequence:@[wait, rotate, addObject]]; 
     [_object1 runAction:seq]; 
    } 
    return self; 
} 
+0

这很完美,非常感谢。起初我并不理解它,但是我研究极坐标的那一瞬间就完全有意义了。非常感谢你,我认为这将完全符合我的要求。 – Ryoku 2014-09-30 04:13:56

+0

你是完全正确的。主要的问题是当我真的假设将它设置为0时,我在勾股定理方程中包含了对象框架高度,因为我只需要知道从对象中心开始的x轴的长度。非常感谢你,你一直是一个惊人的帮助。 – Ryoku 2014-09-30 08:11:50

0

好吧,我不得不张贴代码,所以我不得不作出回答。

所以我试着你的方法0x141E,它的工作完美。我遇到的唯一问题是,只要角度不是0.5PI的倍数,它似乎计算不正确。也许它只是我编码它的方式。我的代码的目标是通过测量一个对象的最左边最中点与最右边其他对象的中点之间的距离来放置一个对象。这样,他们会与右侧接触左侧......基本上并排。我首先放置了一个物体,然后使用给定的算法添加了第二个物体,但是我如上所述的结果在可以被0.5 PI除法的角度之间变得有点时髦。这里是我所做的,如果你想看看我在说什么(如果你想看看我在说什么(如果你想将代码直接导入到XCode中),它应该可以工作(对不起,但我没有足够的代表来附加图像,或者我会给一个截图):

//This is the MyScene implementation file 
@interface MyScene() 
{ 
    SKSpriteNode *_object1; 
    SKSpriteNode *_object2; 
} 
@end 

@implementation MyScene 
-(id)initWithSize:(CGSize)size 
{ 
    if(self = [super initWithSize:size] 
    { 
     object1 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(10,5)]; 
     [self addChild:object1]; 
     //The wait is just to make it feel smoother and not so sudden when starting up the simulator 
     SKAction *wait = [SKAction waitForDuration: 2.5f]; 
     SKAction *rotate = [SKAction rotateByAngle:0.25*M_PI duration:2.0f]; 
     SKAction *addObject = [SKAction runBlock:^{ 
      _object2 = [spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(10,5)]; 

      //I set the rotation of the second object to the rotation of the first so they will always be touching side-by-side 
      _object2.zRotation = _object1.zRotation; 

      //I used the full width and heigth rather than half to save time. The two ojects are of equal width and height so instead of finding both distances and adding them together I just doubled the distance 
      CGFloat r = sqrtf(_object2.frame.size.width*_object2.frame.size.width+_object2.frame.size.height*_object2.frame.size.height); 
      CGFloat x = r*cosf(_object2.zRotation); 
      CGFloat y = r*sinf(_object2.zRotation); 

      object2.position = CGPointMake(_object2.position.x + x, _object2.position + y); 

      [self addChild:_object2]; 
     } 
     SKAction *seq = [SKAction sequence:@[wait, rotate, addObject]]; 
     [self runAction:seq]; 
    } 
} 
@end 

再次感谢您的所有帮助,并且非常感谢您的帮助。

+0

请参阅我编辑的答案。 – 0x141E 2014-09-30 07:45:40