2d collision between line and point

I am trying to understand collision detection in a 2d world. I recently got this tutorial http://www.gotoandplay.it/_articles/2003/12/bezierCollision.php . I have a question that puzzled me very much - it falls on a flash demo ball without answering if I try to change the start and end points. Can someone explain to me how the simulation works. I modified this sample code. It works fine until the start and end points are replaced, here is the same code in object c

Thanks in advance..

-(void)render:(ccTime)dt { if(renderer) { CGPoint b = ball.position; float bvx = ball.vx; float bvy = ball.vy; bvx += .02; bvy -= .2; bx += bvx; by += bvy; float br = ball.contentSize.width/2; for ( int p = 0 ; p < [map count] ; p++ ) { line *l = [map objectAtIndex:p]; CGPoint p0 = l.end; CGPoint p1 = l.start; float p0x = p0.x, p0y = p0.y, p1x = p1.x, p1y = p1.y; // get Angle // float dx = p0x - p1x; float dy = p0y - p1y; float angle = atan2( dy , dx ); float _sin = sin ( angle ); float _cos = cos ( angle ); // rotate p1 ( need only 'x' ) // float p1rx = dy * _sin + dx * _cos + p0x; // rotate ball // float px = p0x - bx; float py = p0y - by; float brx = py * _sin + px * _cos + p0x; float bry = py * _cos - px * _sin + p0y; float cp = ( bx - p0x ) * ( p1y - p0y ) - ( by - p0y ) * ( p1x - p0x ); if ( bry > p0y - br && brx > p0x && brx < p1rx && cp > 0 ) { // calc new Vector // float vx = bvy * _sin + bvx * _cos; float vy = bvy * _cos - bvx * _sin; vy *= -.8; vx *= .98; float __sin = sin ( -angle ); float __cos = cos ( -angle ); bvx = vy * __sin + vx * __cos; bvy = vy * __cos - vx * __sin; // calc new Position // bry = p0y - br; dx = p0x - brx; dy = p0y - bry; bx = dy * __sin + dx * __cos + p0x; by = dy * __cos - dx * __sin + p0y; } } ball.position = b; ball.vx = bvx; ball.vy = bvy; if ( by < 42) { ball.position = ccp(50, size.height - 42); ball.vx = .0f; ball.vy = .0f; } } } 
+4
source share
2 answers

The order of the points determines the orientation on the curve. If the start point is on the left and the end point is on the right, then the curve is oriented so that "up" points above the curve. However, if you change the start / end points, the curve is oppositely oriented, so now the "up" actually points below the curve.

When your code detects a collision and then adjusts the speed, it uses the curve orientation. That is why when the ball falls on the curve, when the start / end points change places, it seems that they are jumping over the curve.

To fix this, the collision resolution code must check on which side of the curve the ball is (relative to the orientation of the curve) and adjust accordingly.

+2
source

If you swap l.end and l.start, it will be used for a line without a segment (l.start, l.end). This is due to the fact that all values ​​are signed here. The algorithm rotates the plane so that the line is horizontal, and one of the ends of the segment does not move. After that, it is easy to see if the ball touches the line. And if so, its speed should change: in a rotating plane, it simply changes the y coordinate, and we must turn it back to get a horizontal line again. This is actually not a good implementation. All this can be done without sin, cos, only vectors.

+1
source

All Articles