2013-02-01 46 views
0

我試圖實現碰撞檢測和一個精靈的基於物理的反彈關閉的多點觸控旋轉另一個精靈。在圖像中,看到的是,兩個黃色的點是同時的觸摸點,我已經計算出基於在ccTouchesBegan方法那些觸摸以及所述ccTouchesMoved方法雲中的適當的位置和角度。隨着用戶移動和旋轉兩次觸摸,雲隨之而來並相應旋轉。多點觸控雪碧旋轉帶衝突檢測

enter image description here

我嘗試雲計算的角度傳遞給另一個方法時鳥用CGRectIntersectsRect和我自己的滾動排序碰撞/反彈了數學的雲相撞,但是這並沒有產生預期的結果,因此我轉向Box2D。

經過Ray Wenderlich教程的4或5之後,我很難將我用多點觸摸雲創建的運動與Box2D的body,fixture,mousejoints和world進行網格劃分。

我讀過這樣的:Cocos2d - Collision Detection of Rotated Sprite

...並且已經經歷了這一點:Ray Wenderlich Collision Detection

我最近ccTouchesMoved方法是這樣的:

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    if (_mouseJoint != NULL) return; 

    UITouch *myTouch = [touches anyObject]; 
    CGPoint location = [myTouch locationInView:[myTouch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 
    b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO); 


    if (_cloudFixture->TestPoint(locationWorld)) { 
     b2MouseJointDef md; 
     md.bodyA = _groundBody; 
     md.bodyB = _cloudBody; 
     md.target = locationWorld; 
     md.collideConnected = true; 
     md.maxForce = 1000.0f * _cloudBody->GetMass(); 

     _mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md); 
     _cloudBody->SetAwake(true); 
    } 

    NSSet *allTouches = [event allTouches]; 
    if([allTouches count] == 2){ 
     UITouch * touch1 = [[allTouches allObjects] objectAtIndex:0]; 
     CGPoint location1 = [touch1 locationInView: [touch1 view]]; 
     location1 = [[CCDirector sharedDirector] convertToGL:location1]; 

     UITouch * touch2 = [[allTouches allObjects] objectAtIndex:1]; 
     CGPoint location2 = [touch2 locationInView: [touch2 view]]; 
     location2 = [[CCDirector sharedDirector] convertToGL:location2]; 

     //EASIER TO WORK WITH INTS 
     int touch1X = location1.x; 
     int touch2X = location2.x; 

     int touch1Y = location1.y; 
     int touch2Y = location2.y; 

     //FIND THE LEFT-MOST TOUCH 
     int minX = (touch1X < touch2X) ? touch1X : touch2X; 
     int maxX = (minX == touch1X) ? touch2X : touch1X; 

     //FIND THE BOTTOM-MOST TOUCH 
     int minY = (touch1Y < touch2Y) ? touch1Y : touch2Y; 
     int maxY = (minY == touch1Y) ? touch2Y : touch1Y; 

     int touchXDiff = maxX - minX; 
     int touchYDiff = maxY - minY; 

     [_cloud setPosition:ccp(touchXDiff/2 + minX, touchYDiff/2 + minY)]; 

     //ROTATE CLOUD AS IT MOVES 

     int offDiffX = touch1X - touch2X; 
     int offDiffY = touch1Y - touch2Y; 

     float angleRadians = atanf((float)offDiffY/(float)offDiffX); 
     float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians); 
     float cloudAngle = -1 * angleDegrees; 

     //[_cloud setRotation:cloudAngle]; 
     _cloudBody->SetTransform(_cloudBody->GetPosition(), cloudAngle); 
    } 

} 

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    if (_mouseJoint == NULL) return; 

    UITouch *myTouch = [touches anyObject]; 
    CGPoint location = [myTouch locationInView:[myTouch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 

    NSSet *allTouches = [event allTouches]; 
    if([allTouches count] == 2){ 
     UITouch * touch1 = [[allTouches allObjects] objectAtIndex:0]; 
     CGPoint location1 = [touch1 locationInView: [touch1 view]]; 
     location1 = [[CCDirector sharedDirector] convertToGL:location1]; 

     UITouch * touch2 = [[allTouches allObjects] objectAtIndex:1]; 
     CGPoint location2 = [touch2 locationInView: [touch2 view]]; 
     location2 = [[CCDirector sharedDirector] convertToGL:location2]; 

     //EASIER TO WORK WITH INTS 
     int touch1X = location1.x; 
     int touch2X = location2.x; 

     int touch1Y = location1.y; 
     int touch2Y = location2.y; 

     //FIND THE LEFT-MOST TOUCH 
     int minX = (touch1X < touch2X) ? touch1X : touch2X; 
     int maxX = (minX == touch1X) ? touch2X : touch1X; 

     //FIND THE BOTTOM-MOST TOUCH 
     int minY = (touch1Y < touch2Y) ? touch1Y : touch2Y; 
     int maxY = (minY == touch1Y) ? touch2Y : touch1Y; 

     int touchXDiff = maxX - minX; 
     int touchYDiff = maxY - minY; 


     //ROTATE CLOUD AS IT MOVES 

     int offDiffX = touch1X - touch2X; 
     int offDiffY = touch1Y - touch2Y; 

     float angleRadians = atanf((float)offDiffY/(float)offDiffX); 
     float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians); 
     float cloudAngle = -1 * angleDegrees; 

     //BEFORE BOX2D 
     //[_cloud setRotation:cloudAngle]; 
     //[_cloud setPosition:ccp(touchXDiff/2 + minX, touchYDiff/2 + minY)]; 

     //WITH BOX2D 
     _cloudBody->SetTransform(_cloudBody->GetPosition(), cloudAngle); 
     b2Vec2 locationWorld = b2Vec2((touchXDiff/2 + minX)/PTM_RATIO, (touchYDiff/2 + minY)/PTM_RATIO); 
     _mouseJoint->SetTarget(locationWorld); 
    } 

} 

編輯(爲了清楚):發生什麼事情是,當實現Box2D代碼時,雲只響應第一次觸摸,不會調整到觸摸之間創建的角度,不遵循這兩個方面。我如何用Box2D來實現這一點?

有沒有人有任何建議?

+0

看不到任何的問題在這裏...只是大量的文字和代碼的... – Morion

+1

通常的問題是締結一個「?」 ...... ......像畢竟文本和代碼:「有人有什麼建議嗎?」 也許你應該嘗試做一些有益的事情,而不是嘲諷一個真棒網站。 – d2burke

回答

0

可愛的藝術品!

你會想與速度和位置信息灌輸給你的多點觸控的身體,從而獲得其他機構與它進行交互的正確碰撞響應。

的運動體實際上是非常適合於這個目的。您所做的是設置雲的幾何形狀,並在多點觸控位置更新時,計算將雲從其當前位置移動到由觸摸定義的新目標位置所需的線性和角速度。這是你如何移動運動體(用速度)。你可以輕輕地移動或嚴格地跟隨運動---運動體,它將會僵硬地碰撞,並且不會因碰撞而產生任何動量。

您還需要弄清楚如何處理的事實,作爲手指移動的運動體的實際長度,並會發生變化。

希望幫助