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