Steering Algorithm Problem

This is a discussion on Steering Algorithm Problem within the Game Programming forums, part of the General Programming Boards category; Ok, So I've been working on this 2D game for a while, actualy a bunch - some I left unfinished ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    32

    Steering Algorithm Problem

    Ok,
    So I've been working on this 2D game for a while, actualy a bunch - some I left unfinished for some other ideas and some I've finished... but I seem to always run into problems with the same thing... choosing which way to turn the entity to achieve a given "course". My algorithm will always make the entity's direction "jump" sometimes instead of gradually turning, why do you think this is?

    This are the problem's parameters:
    - An Entity can turn at a certain rate, either left or right.
    - A chosen direction is given for the entity to face.
    - The entity must then choose the shortest way to start turning towards that direction... left or right.
    - Entity's angle and desired angle are represented in radians, by doubles from 0 to 2*PI. No angle can go over 2*PI or under 0, if they do, they are subtracted 2*PI or added 2*PI respectively...

    Method I've been using:
    alpha - initial direction
    beta - desired direction
    Code:
    if (alpha > PI)
    {
        if (beta > (alpha - PI))
            left
        else
            right
    }
    else
    {
        if (beta < (alpha + PI))
            right
        else
            left
    }
    ====
    Then I take the direction found and if it's left, I subtract the turning speed angle from current angle or if it's right I add to it.
    BUT - before adding/subtracting I check to see that when we add/sub the turn speed, we don't pass the desired angle, if we will, I don't add/sub I just set the current direction to the desired one.

  2. #2
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    In my experience, the best way to do this is to normalize to one of your angles instead of zero, that way you avoid wrapping around the long way. I would say something like:
    Code:
    while desiredHeading > currentHeading + PI:
        desiredHeading -= 2 * PI
    while desiredHeading <= currentHeading - PI:
        desiredHeading += 2 * PI
    diffHeading = desiredHeading - currentHeading
    currentHeading += diffHeading * turnSpeed * dtime
    Where turnSpeed is some value between 0 and 1 that's basically the gain on your feedback, telling the system how much of the error in heading to correct every second.
    Last edited by ichijoji; 10-17-2006 at 12:26 PM.
    Illusion and reality become impartiality and confidence.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    32

    wow,

    I dont think I quite understand what you're doing here...
    could you explain this a little more? how is this going to solve my "jumping around" problem?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  2. Dijkstra algorithm problem.
    By apacz in forum C++ Programming
    Replies: 5
    Last Post: 05-28-2005, 05:16 PM
  3. Simple algorithm problem
    By stodd04 in forum C Programming
    Replies: 11
    Last Post: 03-04-2005, 11:08 AM
  4. Algorithm question
    By PJYelton in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2002, 10:52 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21