2011-03-17 23 views
2

我正在製作一個UIImageView(球)隨機在iPhone屏幕邊緣周圍彈跳的遊戲。然後用戶必須用手指畫出線條以將球引導到孔/目標中。我設法實現了隨機彈跳球以及在屏幕上繪製的能力。我的問題是,我不能在我的生活中發現球和用戶畫出的線之間的碰撞。球剛穿過它。我試圖使用CGRectIntersectsRect,但我很確定這種方法不會工作。我基本想要的是讓球從用戶畫線反彈的方式。如果任何人有任何想法如何做到這一點,並可以幫助我將非常感激。Xcode - 用戶畫出的線條碰撞檢測

在此先感謝

伊夫添加一些代碼,試圖幫助你們理解我的設立是什麼。

觸摸開始是用戶首先將他們的手指在屏幕上

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

fingerSwiped = NO; 
UITouch *touch = [touches anyObject]; 
lastPoint = [touch locationInView:self.view]; 
startPoint = [touch locationInView:self.view]; 
lastPoint.y -= 0; 

} 

觸摸移動時,當手指在屏幕上

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

fingerSwiped = YES; 
UITouch *touch = [touches anyObject]; 
CGPoint currentPoint = [touch locationInView:self.view]; 
currentPoint.y -= 0; 
UIGraphicsBeginImageContext(self.view.frame.size); 

[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width,     self.view.frame.size.height)]; 

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 

// sets the width of the line 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); 
//sets the colour of the line drawn 

//red 
//CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); 
//green 
//CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 1.0, 0.0, 1.0); 
//blue 
//CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 1.0, 1.0); 
//black 
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0); 
CGContextBeginPath(UIGraphicsGetCurrentContext()); 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
//draws a line to the point where the user has touched the screen 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
CGContextStrokePath(UIGraphicsGetCurrentContext()); 
drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

lastPoint = currentPoint; 

fingerMoved++; 

} 

繼承人什麼,我目前正試圖移動使用的實際碰撞檢測...

float a = lastPoint.y - startPoint.y; 
float b = startPoint.x - lastPoint.x; 
float c = lastPoint.x * startPoint.y - startPoint.x * lastPoint.y; 
float d = abs(a * ball.center.x + b *ball.center.y + c)/sqrtf(powf(a,2)+ powf(b,2)); 

if(d ==0){ 
    NSLog(@"collision detected"); 

} 

回答

1

我不知道的Xcode,但是,presu可能這兩個繪製的對象(球和線)共享一些座標空間。我認爲球有一個位置和一個半徑,線有一些終點。在將球從原來的位置移動到下一個位置的任何位置之前,您需要「測試」提議的新位置,以查看該球是否觸及了該線。如果線條是「無限」或至少走出到屏幕邊緣,那麼比兩個對象之間最接近的方法將是將圓心與線相連並與該線垂直的線。垂直線的斜率是原始線的-1 * 1/m(其中m是斜率)。你可以從你的圓圈的中心開始,繪製一條具有該斜率的測試線,並計算測試線與用戶繪製線的交點與某個代數的交點。然後,只需計算該線段的長度,並且如果它小於球的半徑,就可以點擊線條。

如果線條與球體相比較短,您可以沿着線條挑選幾個點並直接對球體中心進行測試,再次將中心距與半徑進行比較。

+0

這似乎是一種非常可行的方式,但另一個問題是用戶(玩家)必須像繪畫應用一樣用手指畫出多條線,這些線會用作某種固體對象,球可能會反彈。因此,按照他們所建議的方式進行操作可能會適用於大概1行,但對於很多行業來說可能會變得麻煩。除非我得到了錯誤的結局...... – Tossy12 2011-03-17 16:24:48

1

請張貼一些代碼。

時間對碰撞檢測很重要,您可能不會在正確的時間提問。首先測試你的代碼。製作一個球和一條清晰的線路,讓你運行探測器。如果它測試正確,那麼它很可能與在適當的時間不要求「這些對象碰撞」有關。

+0

你需要哪部分代碼?我沒有任何關於球和用戶畫線之間的碰撞檢測的代碼,因爲我刪除了我試過的所有東西,因爲我沒有得到我想要的結果。不過,我使用了touchesBegan,touchesMoved和touchesEnded方法來讓玩家在屏幕上畫線。我試圖在這裏粘貼儘可能多的代碼,但沒有足夠的字符空間。 – Tossy12 2011-03-17 16:03:01

+0

我添加了我的touchesMoved方法,如果這有什麼用?如果您需要其他東西,請不要猶豫,問。 – Tossy12 2011-03-17 16:21:38