Note that there are some explanatory texts on larger screens.

plurals
  1. PO2d collision between line and a point
    primarykey
    data
    text
    <p>Im trying to understanding collision detection in 2d world. I recently got this tutorials <a href="http://www.gotoandplay.it/_articles/2003/12/bezierCollision.php" rel="nofollow">http://www.gotoandplay.it/_articles/2003/12/bezierCollision.php</a>. I have question which puzzled me a lot - on the flash demo ball is dropping without responding if i try to swap the starting and end point. Can someone explain me , how the simulation works. I have modified this the sample code. It works perfect until the start and end point are swapped, Here is same code in objective c</p> <p>Thanks in advance. .</p> <pre><code>-(void)render:(ccTime)dt { if(renderer) { CGPoint b = ball.position; float bvx = ball.vx; float bvy = ball.vy; bvx += .02; bvy -= .2; b.x += bvx; b.y += bvy; float br = ball.contentSize.width/2; for ( int p = 0 ; p &lt; [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 - b.x; float py = p0y - b.y; float brx = py * _sin + px * _cos + p0x; float bry = py * _cos - px * _sin + p0y; float cp = ( b.x - p0x ) * ( p1y - p0y ) - ( b.y - p0y ) * ( p1x - p0x ); if ( bry &gt; p0y - br &amp;&amp; brx &gt; p0x &amp;&amp; brx &lt; p1rx &amp;&amp; cp &gt; 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; b.x = dy * __sin + dx * __cos + p0x; b.y = dy * __cos - dx * __sin + p0y; } } ball.position = b; ball.vx = bvx; ball.vy = bvy; if ( b.y &lt; 42) { ball.position = ccp(50, size.height - 42); ball.vx = .0f; ball.vy = .0f; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload