At 10:42 PM +1000 8/30/05, Martin Dillon wrote:
I'm using a Canvas as a 2D cartesian plane. I have a creature
travelling in the direction of random heading H (expressed in
radians). Every so often it pauses at a random point x1,y1 and then
needs to change the value of H to make it move towards a random
point x2,y2. It then repeats this indefinitely moving to new random
points.
Sounds like fun.
I can compute the direction D (in radians) from one point to the other OK.
Using ATan2, right?
I can compute the side (left or right) of the heading H that x2,y2
falls on OK.
I'm having trouble computing a turn_amount T in radians that I can
increment to H (turning right relative to H) or decrement from H
(turning left relative to H). I seem to get incorrect results when
x2,y2 falls within certain quadrants relative to x1,y1 for certain
headings.
Hmm, I'm not sure why that would be, assuming your you have D and H
computed correctly, as well as the left/right side. Assuming you
want a gradual turn, just add (to go right) or subtract (to go left)
a fixed amount to the heading on each frame, until the difference
(which |D-H|, adjusted into the range {-pi,pi}) is less than the
fixed amount; then just set H=D.
If you don't want a gradual turn, then just assign H=D right away.
I'm sure this should be simple and I must be missing something. I'd
be grateful for any pseudocode suggesting ways to compute a value
for T that can be added or subtracted from H to give my creature the
new heading it needs.
Er... you already have the new heading it needs; that's D, right?
The amount you'd add or subtract is D-H, so you have H = H + (D-H),
which reduces to H=D.
Note that I want my creature to turn left or right as appropriate to
get to x2,y2 - rather than needlessly turning more than pi radians
(180 degrees).
Ah, well just shift it into the range {-pi,pi} as I alluded to above:
delta = D-H
while delta < -kPi
delta = delta + k2Pi
wend
while delta > kPi
delta = delta - k2Pi
wend
Best,
- Joe
--
Joe Strout REAL Software, Inc.
Vote for REALbasic (twice!) in the LinuxWorld Reader's Choice Awards:
http://linux.sys-con.com/general/readerschoice.htm
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>
|