Xcode-用户绘制的线碰撞检测

Xcode-用户绘制的线碰撞检测,xcode,drawing,collision-detection,paint,Xcode,Drawing,Collision Detection,Paint,我正在制作一个游戏,其中一个UIImageView(球)在iphone屏幕边缘随机反弹。然后,用户必须用手指画线,将球引导到孔/目标中。我成功地实现了随机弹跳球和在屏幕上画图的功能。我的问题是,我一生都无法在球和用户绘制的线之间检测到碰撞。球刚好穿过它。我曾经尝试过使用CGRectIntersectsRect,但我很确定这种方法行不通。我基本上想要的是某种让球从用户画的线反弹出去的方法。如果有人知道如何做到这一点,并能提供帮助,我将不胜感激 提前谢谢 我添加了一些代码来帮助你们理解我的设置 触摸

我正在制作一个游戏,其中一个UIImageView(球)在iphone屏幕边缘随机反弹。然后,用户必须用手指画线,将球引导到孔/目标中。我成功地实现了随机弹跳球和在屏幕上画图的功能。我的问题是,我一生都无法在球和用户绘制的线之间检测到碰撞。球刚好穿过它。我曾经尝试过使用CGRectIntersectsRect,但我很确定这种方法行不通。我基本上想要的是某种让球从用户画的线反弹出去的方法。如果有人知道如何做到这一点,并能提供帮助,我将不胜感激

提前谢谢

我添加了一些代码来帮助你们理解我的设置

触摸开始是用户首先将手指放在屏幕上的地方

- (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++;

}
触摸移动是指手指在屏幕上移动

- (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");

}

我不知道Xcode,但是,大概这两个绘制的对象(球和线)共享一些坐标空间。我假设球有一个位置和半径,线有一些端点。在将球从原来的位置移动到下一帧中的任何位置之前,请“测试”建议的新位置,以查看圆是否已触线。如果直线是“无限的”,或者至少是延伸到屏幕边缘,那么两个对象之间最近的接近点将是连接圆心和直线并垂直于该直线的直线。垂直线的坡度为原始线的-1*1/m(其中m为坡度)。您可以从圆的中心开始,绘制具有该坡度的测试线,并使用一些代数计算测试线与用户绘制的线的交点。然后只计算该段的长度,如果它比球的半径短,你就击中了线

如果与球相比,该线是一个较短的线段,则可以沿该线拾取多个点,并直接对球的中心进行测试,再次将到中心的距离与半径进行比较

请发布一些代码


时间对碰撞检测很重要,您可能没有在正确的时间提出问题。首先测试你的代码。做一个球和一条线,清楚地划过你的身体。如果测试正确,那么很可能与没有在正确的时间询问“这些物体是否碰撞”有关

您需要代码的哪一部分?我没有关于球和用户画线之间碰撞检测的任何代码,因为我删除了我尝试的所有内容,因为我没有得到我想要的结果。然而,我使用了touchesbeated、touchesMoved和touchesend方法,允许玩家在屏幕上画线。我试着在这里粘贴尽可能多的代码,但是没有足够的字符空间。我在上面添加了我的touchesMoved方法,如果有用的话?如果你还需要什么,请不要犹豫。这似乎是一个非常可行的方法,但另一个问题是,用户(玩家)必须像绘画应用程序一样用手指画多条线,这些线将充当某种实心物体,球可以在其中反弹。所以,按照你们建议的方式去做,也许一行就可以了,但对很多人来说,可能会变得一团糟。除非我找错了方向。。。