我正在製作一個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行,但對於很多行業來說可能會變得麻煩。除非我得到了錯誤的結局...... – Tossy12 2011-03-17 16:24:48