realbasic-games
[Top] [All Lists]

Re: Incrementing or decrementing a heading in radians

To: <realbasic-games at lists dot realsoftware dot com>
Subject: Re: Incrementing or decrementing a heading in radians
From: "Martin Dillon" <martin dot dillon at optusnet dot com dot au>
Date: Wed, 31 Aug 2005 14:44:31 +1000
Delivered-to: realbasic-games at lists dot realsoftware dot com
References: <20050830170037 dot 86C5ED08DB5 at lists dot realsoftware dot com>
> From: "Joseph J. Strout" <joe at realsoftware dot com>
> 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.

Yep. Love the biology - but trigonometry is not my natural habitat!

> >I can compute the direction D (in radians) from one point to the other
OK.
>
> Using ATan2, right?

Actually I'm using ATan.  I then parse the result depending on the quadrant
that x2,y2 falls in relative to x1,y1, but I eventually get the right answer
(i.e. D the direction from x1,y1 to x2,y2).

I just looked at ATan2 and I'm not sure it's what I want.  It seems to
compute the angle from the x axis of a line that passes from the origin
through the point of interest. I need to compute the direction from one
point to another.

There may be another confounding factor here. I am using a cartesian plane
on my Canvas in which I define zero radians (or 0 or 360 degrees or 2pi
radians) to point straight up to the top of the screen. This has caused me
headaches because the y axis starts with zero at the top and increases
towards the bottom of the screen. Am I walking on thin ice with this
approach?

Eventually once I work out my 2D algorithms, I'll be moving this project to
RB3Dspace. How does the orientation of coordinates in RB3D compare with a
canvas?

> >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.

My creatures can't turn sharply, so I'm using a gradual turn. For some
reason D-H gives bad answers on some sides of the heading. Perhaps it has to
do with the orientation of my cartesian plane? The results change suddenly
depending on the quadrant the target points fall in relative to the location
x1,y1

> >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.

I'm certain I have D and H computed correctly. I can't understand why T =
D-H gives me a warped answer for some sides of the creature.

> >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

This is a nice approach.  Once I get my logic straightened out I'll try it
out.
Thanks Joe.

Martin

_______________________________________________
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>

<Prev in Thread] Current Thread [Next in Thread>