Note that there are some explanatory texts on larger screens.

plurals
  1. PONot a number error (NAN) doing collision detection in an iphone app
    primarykey
    data
    text
    <p>I have a set of balloons that I am trying to get to bounce off of each other like balls. When I start to move them and then detect collision, the routine for the collisions eventually returns a NAN for the velocity of the balloons. I end up with a position of something like x=270, y= -nan(0x400000). I've been looking at the code all day and I still can't find the error, any help would be appreciated. Here's the code:</p> <pre><code> - (void)MoveBalloons { for (CurBalloon=1; CurBalloon &lt;= NumBalloons; ++CurBalloon) { //check for edge hCurBalloont if (uBalloon[CurBalloon].pos.y &gt; 456) { uBalloon[CurBalloon].pos.y = 456; uBalloon[CurBalloon].Vel_y = -uBalloon[CurBalloon].Vel_y; } if (uBalloon[CurBalloon].pos.y &lt; 24) { uBalloon[CurBalloon].pos.y = 24; uBalloon[CurBalloon].Vel_y = -uBalloon[CurBalloon].Vel_y; } if (uBalloon[CurBalloon].pos.x &gt; 289) { uBalloon[CurBalloon].pos.x = 289; uBalloon[CurBalloon].Vel_x = -uBalloon[CurBalloon].Vel_x; } if (uBalloon[CurBalloon].pos.x &lt; 31) { uBalloon[CurBalloon].pos.x = 31; uBalloon[CurBalloon].Vel_x = -uBalloon[CurBalloon].Vel_x; } //call loop to go through the balloons for each touch. //[self CollisionCheck]; NSUInteger h; float dist, tempvelx, tempvely, temp2velx, temp2vely;; for (h=1; h &lt;= NumBalloons; ++h) { //check if balloon is too close if (CurBalloon == h) { //skip the check ifit's the same balloon } else { dist = distanceBetweenPoints(uBalloon[CurBalloon].pos, uBalloon[h].pos); if (dist &lt;= (kBalloonNear)) { //balloon's touching tempvelx = uBalloon[CurBalloon].Vel_x; tempvely = uBalloon[CurBalloon].Vel_y; temp2velx = uBalloon[h].Vel_x; temp2vely = uBalloon[h].Vel_y; tempvelx = (uBalloon[CurBalloon].Vel_x - uBalloon[h].Vel_x) * (sinf(angleBetweenPoints(uBalloon[CurBalloon].pos,uBalloon[h].pos)))*(uBalloon[CurBalloon].Vel_x - uBalloon[h].Vel_x) * (sinf(angleBetweenPoints(uBalloon[CurBalloon].pos,uBalloon[h].pos))) - (uBalloon[CurBalloon].Vel_y - uBalloon[h].Vel_y) * sinf(angleBetweenPoints(uBalloon[CurBalloon].pos, uBalloon[h].pos)) * cosf(angleBetweenPoints(uBalloon[CurBalloon].pos, uBalloon[h].pos)) + uBalloon[h].Vel_x; tempvely = (uBalloon[CurBalloon].Vel_y - uBalloon[h].Vel_y) * (cosf(angleBetweenPoints(uBalloon[CurBalloon].pos,uBalloon[h].pos))) * (uBalloon[CurBalloon].Vel_y - uBalloon[h].Vel_y) * (cosf(angleBetweenPoints(uBalloon[CurBalloon].pos,uBalloon[h].pos))) - (uBalloon[CurBalloon].Vel_x - uBalloon[h].Vel_x) * sinf(angleBetweenPoints(uBalloon[CurBalloon].pos, uBalloon[h].pos)) * cosf(angleBetweenPoints(uBalloon[CurBalloon].pos, uBalloon[h].pos)) + uBalloon[h].Vel_y; temp2velx = (uBalloon[CurBalloon].Vel_x - uBalloon[h].Vel_x) * (cosf(angleBetweenPoints(uBalloon[CurBalloon].pos,uBalloon[h].pos))) * (uBalloon[CurBalloon].Vel_x - uBalloon[h].Vel_x) * (cosf(angleBetweenPoints(uBalloon[CurBalloon].pos,uBalloon[h].pos))) + (uBalloon[CurBalloon].Vel_y - uBalloon[h].Vel_y) * sinf(angleBetweenPoints(uBalloon[CurBalloon].pos, uBalloon[h].pos)) * cosf(angleBetweenPoints(uBalloon[CurBalloon].pos, uBalloon[h].pos)) + uBalloon[h].Vel_x; temp2vely = (uBalloon[CurBalloon].Vel_y - uBalloon[h].Vel_y) * (sinf(angleBetweenPoints(uBalloon[CurBalloon].pos,uBalloon[h].pos)))*(uBalloon[CurBalloon].Vel_y - uBalloon[h].Vel_y) * (sinf(angleBetweenPoints(uBalloon[CurBalloon].pos,uBalloon[h].pos))) - (uBalloon[CurBalloon].Vel_x - uBalloon[h].Vel_x) * sinf(angleBetweenPoints(uBalloon[CurBalloon].pos, uBalloon[h].pos)) * cosf(angleBetweenPoints(uBalloon[CurBalloon].pos, uBalloon[h].pos)) + uBalloon[h].Vel_y; uBalloon[CurBalloon].Vel_x = tempvelx; uBalloon[CurBalloon].Vel_y = tempvely; uBalloon[h].Vel_x = temp2velx; //set to old value of CurBalloon balloon. uBalloon[h].Vel_y = temp2vely; } } } if (fabsf(uBalloon[CurBalloon].Vel_x) &gt;= kBalloonMaxSpeed) uBalloon[CurBalloon].Vel_x = kBalloonMaxSpeed; if (fabsf(uBalloon[CurBalloon].Vel_y) &gt;= kBalloonMaxSpeed) uBalloon[CurBalloon].Vel_y = kBalloonMaxSpeed; if (fabsf(uBalloon[CurBalloon].Vel_x) &lt;= -kBalloonMaxSpeed) uBalloon[CurBalloon].Vel_x = -kBalloonMaxSpeed; if (fabsf(uBalloon[CurBalloon].Vel_y) &lt;= -kBalloonMaxSpeed) uBalloon[CurBalloon].Vel_y = -kBalloonMaxSpeed; if (uBalloon[CurBalloon].Vel_y &gt; KBalloonSpeed) { //if travellCurBalloonng fast, slow CurBalloont up fast to normal speed uBalloon[CurBalloon].Vel_y = uBalloon[CurBalloon].Vel_y - kBalloonSlowRate; } if (uBalloon[CurBalloon].Vel_x &gt; KBalloonSpeed) { //if travellCurBalloonng fast, slow CurBalloont up fast to normal speed uBalloon[CurBalloon].Vel_x = uBalloon[CurBalloon].Vel_x - kBalloonSlowRate; } if (uBalloon[CurBalloon].Vel_y &lt; KBalloonSpeed) { //if travellCurBalloonng fast, slow CurBalloont up fast to normal speed uBalloon[CurBalloon].Vel_y = uBalloon[CurBalloon].Vel_y + kBalloonSlowRate; } if (uBalloon[CurBalloon].Vel_x &lt; KBalloonSpeed) { //if travellCurBalloonng fast, slow CurBalloont up fast to normal speed uBalloon[CurBalloon].Vel_x = uBalloon[CurBalloon].Vel_x + kBalloonSlowRate; } //slow to 0 if velocCurBalloonty CurBalloons CurBalloonnsCurBalloonde the slow rate if (fabsf(uBalloon[CurBalloon].Vel_x) &lt;= kBalloonSlowRate) uBalloon[CurBalloon].Vel_x = 0; if (fabsf(uBalloon[CurBalloon].Vel_y) &lt;= kBalloonSlowRate) uBalloon[CurBalloon].Vel_y = 0; uBalloon[CurBalloon].Vel_x = uBalloon[CurBalloon].Vel_x - kBalloonFallRate; //keep movCurBalloonng down //add velocCurBalloonty to poCurBalloonnts uBalloon[CurBalloon].pos.y= uBalloon[CurBalloon].pos.y + uBalloon[CurBalloon].Vel_y; uBalloon[CurBalloon].pos.x = uBalloon[CurBalloon].pos.x + uBalloon[CurBalloon].Vel_x; } } </code></pre> <p>Here's the functions written in c that are used for distance and the angle. I'm even making sure to never have a sqrt(-1) and division by 0: </p> <pre><code> CGFloat distanceBetweenPoints (CGPoint first, CGPoint second) { CGFloat deltaX = second.x - first.x; CGFloat deltaY = second.y - first.y; if ((deltaX*deltaX + deltaY*deltaY ) == -1) { return sqrtf(-.99); } else { return sqrtf(deltaX*deltaX + deltaY*deltaY ); } } CGFloat angleBetweenPoints(CGPoint first, CGPoint second) { if (first.x - second.x == 0) { return (pi / 2); } else { return atanf((first.y-second.y) / (first.x - second.x)); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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