Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Web Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Patch for the ASP.NET Padding Oracle vulnerability has been releasedAutomated Monitoring of Remote VNC Servers »
    comments

    Basic Tracked MovementAs part of a simulation I've been developing I recently had to decide how to model movement of vehicles within a 2D space. To keep things simple I settled on a tracked vehicle. Each time the simulation updates I calculate how much the vehicle rotates and how far it moves based on the speed of it's tracks. From there I calculate the new coordinates for redrawing the vehicle.

    Knowing the current location (x,y) and heading (h) I initially adopted a simplified way to calculate the rotation (g) and speed (s) based on the speed of the vehicle's tracks (T1 and T2) (Figure 1). With these variables I create a right angled triangle and use maths to calculate how far the vehicle moves in the x and y coordinates in order to find it's finishing location (x',y').

    1. x' = x + s * sin(h + g)
    2. y' = y + s * cos(h + g)

    This method is a reasonable approximation for the movement of a tracked vehicle: It'll move faster when you expect it to and it'll turn faster when you expect it to. However, as a stand up mathematician pointed out to me, it's not an accurate representation of what should actually be happening. I had also observed vehicles having problems turning to face something close to and behind them resulting in endless circling of the target.

    Improving Accuracy

    A more accurate representation is the vehicle travelling a distance, s, along the circumference of a circle who's radius can be calculated based on T1, T2 and the distance between the tracks (2d) (Figure 2).

    Figure 2: More Accurate MovementIf T2 traces a circle of radius r then T1 traces a circle of radius r + 2d. Since d is static we can say that the ratio of T1 to T2 is proportional to the ratio of the radii. This gives us the following equation to solve:

    1. T1   r + 2d
    2. -- = -------
    3. T2     r

    Solving this for r gives us:

    1. r =     2d
    2.     ----------
    3.      T1
    4.      --  -  1
    5.      T2

    Rotation

    We now have a triangle with 2 sides of known length. If we divide this triangle in half we can use the right angles to determine the vehicle will move 2g radians around the circle (Figure 3).

    We can say that the ratio between 2g and 2 PI radians (360 degrees) is the same as the ratio between s and the circumference of the circle (2 * PI * (r + d)). Setting them to equal we can solve for g.

    Figure 3: Solving Triangles

    1. 2g                s
    2. ------  =  -----------------
    3. 2 * PI      2 * PI * (r + d)
    4.        s
    5. 2g = -------
    6.      r + d
    7.  
    8. 2g = T1 + T2
    9.     --------
    10.        2
    11.     --------
    12.      r + d
    13.  
    14. 2g =    T1 + T2
    15.      --------------
    16.       2 * ( r + d )
    17.  
    18. g  =     T1 + T2
    19.       -------------
    20.       4 * ( r + d )

    If we substitute in our value for r and simplify we end up with:

    1. g = T1 - T2
    2.     --------
    3.        4d

    Which is a fairly simple calculation that scales by the size of the organism. The other thing missing from the approximate calculation is that the change in heading is actually 2g, as can be seen from the symmetry in our diagrams.

    Distance

    Now that we know the angles we can use the Law of Sines to calculate the straight line distance, t, between our vehicle's start and end points.

    1. r + d        t / 2
    2. --------- = -------
    3. sin(PI/2)    sin(g)
    4.  
    5. r + d     t / 2
    6. -----  = -------
    7.   1       sin(g)
    8.  
    9. sin(g) * (r + d) = t / 2
    10.  
    11. t = 2 * sin(g) * (r + d)

    Once again we can substitute in our value for r and simplify to get:

    1. t = d(T1 + T2)
    2.     ---------- * 2 * sin(g)
    3.       T1 - T2

    Now that we know the distance and we know the angle we can use the same method we used earlier to calculate the new coordinates:

    1. x' = x + t * sin(h + g)
    2. y' = y + t * cos(h + g)

    Applying Our Findings

    So far we've seen a lot of (possibly confusing) numbers and symbols but what do we actually have to do do add this to a simulation? Each time our vehicle updates we need to:

    1. Calculate g.
    2. Calculate t.
    3. Calculate x'.
    4. Calculate y'.
    5. Adjust the vehicle's coordinates to x',y'.
    6. Adjust the vehicle's heading (h) by 2g.

    You may have noticed there's a possible division by zero error when T1 equals T2. To protect against this we can check if they're equal (or catch the exception) and use the calculation:

    1. x' = x + T1 * sin(h)
    2. y' = y + T1 * cos(h)

    Example

    Let's say our vehicle has the properties:

    T1 = 1.0 (Track 1 Speed)
    T2 = 0.5 (Track 2 Speed)
    d = 2.0 (Half distance between tracks)
    x = 0 (Coordinate)
    y = 0 (Coordinate)
    h = 2 (Heading)

    1. Calculate g

    1. g = T1 - T2
    2.     --------
    3.        4d
    4. g = 1.0 - 0.5     0.5
    5.     ---------  =  --- = 0.0625
    6.         8          8

    2. Calculate t

    1. t = d(T1 + T2)
    2.     ---------- * 2 * sin(g)
    3.       T1 - T2
    4. = 0 + (2 * (1.0 + 0.5) * 2 * sin(0.0625) ) / ( 1.0 - 0.5 )
    5. = (6 * 0.06246 ) / 0.5
    6. = 0.37476 * 2
    7. = 0.74951

    3. Calculate x'.

    1. x' = x + t * sin(h + g)
    2. = 0 +  0.74951 * sin(2.0625)
    3. = 0.66072

    4. Calculate y'.

    1. y' = y + t * cos(h + g)
    2. = 0 + 0.74951 * cos(2.0625)
    3. = -0.35387

    2. Adjust h by 2g.

    1. h' = h + 2g
    2. h' = 2 + 2 * 0.0625
    3. h' = 2.125

    After this update our new state is:

    x = 0.66072
    y = -0.35387
    h = 2.125

    In the following Java example the Vehicle's update method would be called from the main program loop.

    1. public class Vehicle
    2. {
    3.   private double x, y, heading;
    4.   private int size, maxSpeed;
    5.   /*
    6.      Other methods for initialising and controlling the vehicle.
    7.   */
    8.   public void update()
    9.   {
    10.     double tracks[2] = controller.getTrackSpeeds(); // Get the track speeds from whatever class is controlling the vehicle.
    11.                                                     // Could be human input or any kind of AI controller.
    12.     // Calculate g.
    13.     double d = this.size / 2;
    14.     double g = (tracks[0] - tracks[1]) / ( d * 4);
    15.  
    16.     // Calculate t.
    17.     double t = 0;
    18.     if(tracks[0].equals(tracks[1])) // Use straight line calculation to avoid division by zero.
    19.     {
    20.       t = tracks[0];
    21.     }
    22.     else
    23.     {
    24.       t = ( d * ( tracks[0] + tracks[1] ) * 2 * Math.sin(g) ) / ( tracks[0] - tracks[1] );
    25.     }
    26.  
    27.     // Update x.
    28.     this.x -= t * Math.sin(this.heading + g); // See note below.
    29.                                              
    30.     // Update y.
    31.     this.y += t * Math.cos(this.heading + g);
    32.  
    33.     // Update the vehicle's heading.
    34.     this.heading += 2 * g;
    35.   }
    36. }

    When updating the x coordinate we subtract because, when using a JPanel:

    • 0,0 is the top left corner.
    • A heading of 0 is down.

    Using a normal sine wave an increase in heading (h) would result in an increase in sine h and therefore, an increase in x which is the opposite of what is expected/required.

    Inverting the wave ( multiply by -1 ) produces the correct behaviour.

    How Much Difference Does It Really Make?

    The accuracy of the basic method decreases as the section of the circle traversed in one increment moves further from a straight line. Therefore, it decreases as:

    Comparison Charts

    • Speed increases.
    • Turning circle decreases.

    To demonstrate just how much difference there can be I've used a fairly extreme case to produce the charts opposite:

    T1 = 10
    T2 = -9
    d = 2

    As we can see a vehicle turning in a tight circle at a fairly high speed has a much bigger turning circle using the approximate movement calculations than when using the improved calculations.

    As the size of the vehicle is now taken into account we can make a vehicle less nimble with a larger turning circle simply by increasing it's size. We can get a turning circle like the one below by increasing d to 60 without any need to introduce extra variables to clamp the rotation rate.

    Larger Vehicle

    The other difference which is fairly hard to quantify is that the vehicle movement was much smoother once the new calculations were implemented. This could be because the initial technique caused lots of over-rotating which required constant readjustment.

    5900 views
    InstapaperVote on HN

    1 comment

    Comment from: robby [Visitor]
    robby Yo, interesting stuff. I just tackled the exact same problem, but glancing at the way you solved this I'm unsure if your equations generalize if:
    1. right wheel going faster than left
    2. both are going backwards
    2b. where one goes goes faster than the other, then the other goes faster than the one
    3. one is going backwards and the other going forwards
    3b. where one goes goes faster than the other, then the other goes faster than the one
    07/29/12 @ 01:13

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)