2011-03-17 35 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