我正在制作一个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