【问题标题】:Xcode - User drawn lines collision detectionXcode - 用户绘制的线条碰撞检测
【发布时间】:2011-03-17 14:29:23
【问题描述】:

我正在制作一个 UIImageView(ball) 在 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++;

}

这是我目前尝试用于实际碰撞检测的内容...

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 drawing collision-detection paint


    【解决方案1】:

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

    如果与球相比,线段较短,您可以沿线选取几个点,并直接针对球的中心进行测试,再次将到中心的距离与半径进行比较。

    【讨论】:

    • 这似乎是一种非常可能的方式,但另一个问题是用户(玩家)必须像绘画应用程序一样用手指绘制多条线,而这些线将充当某种形式球可以在其中反弹的固体物体。因此,按照您建议的方式执行此操作对于 1 行可能没问题,但对许多人来说可能会变得混乱。除非我有错误的一端......
    【解决方案2】:

    请贴一些代码。

    时间对于碰撞检测很重要,您可能没有在正确的时间提出问题。首先测试你的代码。制作一个球和一条线,清楚地碰撞并运行你的探测器。如果它测试正确,那么很可能与没有在正确的时间询问“这些物体是否发生碰撞”有关。

    【讨论】:

    • 您需要哪部分代码?我真的没有任何关于球和用户画线之间的碰撞检测的代码,因为我已经删除了我尝试的所有内容,因为我没有得到我想要的结果。但是,我使用了 touchesBegan、touchesMoved 和 touchesEnded 方法来允许玩家在屏幕上画线。我尝试在这里粘贴尽可能多的代码,但没有足够的字符空间。
    • 我已经在上面添加了我的 touchesMoved 方法,如果那有什么用吗?如果您需要其他任何东西,请不要犹豫。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多