angleToP = (initial_direction - 90) / 180 * Math.PI;
P.x = Origin.x + r * Math.cos(angleToP);
P.y = Origin.y + r * Math.sin(angleToP);
dx = Destination.x - P.x;
dy = Destination.y - P.y;
h = Math.sqrt(dx * dx + dy * dy);
if (h < r)
return;
d = Math.sqrt(h * h - r * r);
theta = Math.acos(r / h);
phi = Math.atan(dy / dx);
Q.x = P.x + r * Math.cos(phi + theta);
Q.y = P.y + r * Math.sin(phi + theta);
Smooth turns in pathfinding
Hello
I wasn't sure in which forum to post this, but i figured, since more a matter of converting pseudo-code to real code (AS3 in my case) it would be more appropriate to post it here instead of the Math-forum.
I'm programming a game in Flash(AS3) and I'm implemented path-finding to it.
Now I want to make the turns in the path smooth.
I'm using this tutorial to do that:
http://www.gamasutra.com/features/20010314/pinter_02.htm
But I'm having problems.
The first problem I'm having is to calculate the point Q, I've translated the pseudo-code on the page and looked it over dozens of times but it still seems to be off.
At the moment I'm ignoring left-turns, I'm trying to get it work properly with right-turns only instead.
Here is the relevant piece of code I've written:
This is the result I'm getting:
The top 2 ones are mine, and the bottom one is the one from the tutorial.
As you can see, the two bottom ones have nearly identical points and radius, so they *should* produce the same result, but for some reason they don't...
In the first line in my code, maybe you noticed that I converted angleToP from degrees to radians (/ 180 * Math.PI), because the sin/cos/atan/acos in flash take angle in radians, but in the pseudo-code they seem to take degrees?
Would this maybe mean that I would have to change the code somewhere else too? Or am I wrong somewhere?
If someone could help me with this I'd *really* appreciate it.
Thanks in advance
Can't you simply interpolate a curve?
http://local.wasp.uwa.edu.au/~pbourke/miscellaneous/interpolation/
http://local.wasp.uwa.edu.au/~pbourke/miscellaneous/interpolation/
Thanks for the answer, but the thing is that i need a consistent turning-radius, for tile-based pathfinding
Quote:phi = Math.atan(dy / dx);atan isn't a great thing to use here, since you care about the sign of the angle. Use atan2 instead. Actually, that part of the pseudocode looks rather suspect. It looks like you should be negating the phi+theta before calculating Q.
Just looking at the pictures, it looks to me as though your theta is equal to the tutorials theta + phi.
Maybe you need to subtract phi from your theta for some reason?
It'll matter where you're referencing the angles from.
Maybe you need to subtract phi from your theta for some reason?
It'll matter where you're referencing the angles from.
By the way, another reason why I don't want to use interpolated curves is that I also want consistent speed.
I tried using atan2 instead as you suggested, the direction ended up inversed so I negated dy&dx, the result was same as before except that now it worked in all directions, one step in the right direction =)
I'm impressed you could see that. I did as you suggested, actually that's the way it's done in the pseudo-code for *left*-turning. It got inversed again, so I removed the negation of phi+theta again and now it's working great =)
Thanks for all the help, I really appreciate it!
Quote:Original post by SneftelQuote:phi = Math.atan(dy / dx);atan isn't a great thing to use here, since you care about the sign of the angle. Use atan2 instead. Actually, that part of the pseudocode looks rather suspect. It looks like you should be negating the phi+theta before calculating Q.
I tried using atan2 instead as you suggested, the direction ended up inversed so I negated dy&dx, the result was same as before except that now it worked in all directions, one step in the right direction =)
Quote:Original post by bzroom
Just looking at the pictures, it looks to me as though your theta is equal to the tutorials theta + phi.
Maybe you need to subtract phi from your theta for some reason?
It'll matter where you're referencing the angles from.
I'm impressed you could see that. I did as you suggested, actually that's the way it's done in the pseudo-code for *left*-turning. It got inversed again, so I removed the negation of phi+theta again and now it's working great =)
Thanks for all the help, I really appreciate it!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement