开发者

Xcode - User drawn lines collision detection

Im making a game in which a UIImageView(ball) randomly bounces around the edges of the iphone screen. The user then must draw lines with their finger to guide the ball into a hole/target. I have managed to implement the randomly bouncing ball and the ab开发者_如何转开发ility to draw onto the screen. My problem is that I cant for the life of me get a collision detected between the ball and the user drawn line. The ball just passes through it. I have tried to use a CGRectIntersectsRect but i'm pretty sure that this method wont work. What i'm basically wanting is some way of getting the ball to bounce off of the user drawn line. If anyone has any idea on how to do this and could help I would be very grateful.

Thanks in advance

Ive added some code to try and help you guys understand what my set up is.

touches began is where the user first puts their finger on the screen

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

}

touches moved is when the finger is moved across the screen

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

}

Heres what i'm currently trying to use for the actual collision detection...

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

}


I don't know Xcode, but, presumably these two drawn objects (the ball and the line) share some coordinate space. The ball has, I assume, a position and a radius, and the line has some end points. Before moving the ball from where ever it is, to where ever it will be in the next frame, you "test" the proposed new location to see if the circle has touched the line. If the line is "infinite", or at least, going out to the screen edges, than the closest approach between the two objects will be the line that connects the center of the circle to the line and is perpendicular to that line. The slope of the perpendicular line is -1 * 1/m of the original line (where m is the slope). You can start at the center of your circle and draw a test line with that slope, and calculate where the intersection of the test line with the user-drawn line will be with some algebra. Then just calculate the length of that segment, and if it is shorter than the radius of the ball, you hit the line.

If the line is a short segment in comparison to the ball, you could pick several points along the line and test them directly against the center of the ball, again comparing the distance to the center against the radius.


please post some code.

timing is important to collision detection and you just might not be asking the question at the right time. first test your code. make a ball and a line that clearly colide and run you detector. If it tests correctly then it most likely something to do with not asking "are these objects colliding" at the right time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜