Note that there are some explanatory texts on larger screens.

plurals
  1. POPathfinding / Deciding on direction
    text
    copied!<p>i'm making a simple iPhone game using cocos2d-iphone. I have an array of fiends, the "fiendSet" which has to navigate around a field full of obstacles. I spent the last three nights trying to get my A* pathfinding to work. I found the actual A* implementation here on stackoverflow and it works brilliantly. However, once i try to move my fiends around i run into trouble.</p> <p>Each of my fiends has a CGPoint called motionTarget which contains the x and y values for where the fiend has to go. If only set the positions x and y to absolute values once a second, it works, like so:</p> <pre><code>-(void) updateFiendPositions:(ccTime)dt { for (MSWFiend *currFiend in fiendSet) { currFiend.position = ccp(currFiend.motionTarget.x,currFiend.motionTarget.y); } } </code></pre> <p>However, this doesn't look very nice, the fiends just "jump" 20px each second instead of animating nicely. I only implemented this as a placeholder method to verify the pathfinding. Now i want smooth animation. This is what i did:</p> <pre><code>-(void) updatePositions:(ccTime) dt { for (MSWFiend *currFiend in fiendSet) { if (currFiend.motionTarget.x != -1 &amp;&amp; currFiend.motionTarget.y != -1) { float x,y; if ((int)floor(currFiend.position.x) &lt; (int)floor(currFiend.motionTarget.x)) { x = currFiend.position.x+(currFiend.speed*dt); } if ((int)floor(currFiend.position.x) &gt; (int)floor(currFiend.motionTarget.x)) { x = currFiend.position.x-(currFiend.speed*dt); } if (abs((int)floor(currFiend.position.x)-(int)floor(currFiend.motionTarget.x)) &lt; 2) { x = currFiend.motionTarget.x; } if ((int)floor(currFiend.position.y) &lt; (int)floor(currFiend.motionTarget.y)) { y = currFiend.position.y+(currFiend.speed*dt); } if ((int)floor(currFiend.position.y) &gt; (int)floor(currFiend.motionTarget.y)) { y = currFiend.position.y-(currFiend.speed*dt); } if (abs((int)floor(currFiend.position.y)-(int)floor(currFiend.motionTarget.y)) &lt; 2) { y = currFiend.motionTarget.y; } currFiend.position = ccp(x,y); } } } </code></pre> <p>This works great for fiends moving in one direction. As soon as a fiend is supposed to go around a bend, trouble starts. Instead of for example first going up, then right, then down; my fiends will combine the up/right motion into one, they are "cutting corners". I only want my fiends to move either north/south <em>OR</em> east/west for each position update, not both. In other words, i don't want to animate changes to x and y simultaneously. I hope this explanation is clear enough..</p> <p>I'm pretty sure i have a logic error somewhere.. i just havn't been able to figure it out for the last three sleepless nights after work.. Help!</p>
 

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