Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Average of several directions - maths problems

Discussion in 'Scripting' started by KrisM, Mar 21, 2015.

  1. KrisM

    KrisM

    Joined:
    Oct 18, 2014
    Posts:
    28
    I've got a problem that previous posts aren't providing an easy solution to.

    I've got a range of wind zones that may (or may not) overlap. The wind direction is the same as the facing direction of the windzones.
    I've got a compass that I want to add a wind direction pointer to. The compass also rotates based on player facing direction.

    I need to work out the average direction of all windzones the character is within. To complicate matters, each windzone has a windspeed, so if 'windzone1' has a windspeed of 2, and 'windzone2' has a windspeed of 1, I need the compass pointer to lean towards the windzone1 direction.

    I'm mostly looking for the (scripted) maths for finding the average of the angles based on the strength of the wind speed (if something like this is possible). If my reasoning is accurate, the rotating compass shouldn't impact on the maths at all (as the wind direction will compare to compass direction and player direction in the same way).

    Any help people can provide is greatly appreciated.
     
  2. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    First decide how to determine the wind velocity due to one of your zones at a given position - e.g. you may want it to reduce as the player leaves the zone, either based on position or time. Figure that out for a single zone at a time. Then to get the overall wind velocity, just add up all the vectors.

    If you then want to convert that into a pure direction, normalize the output vector, and that is what you should display on your compass.
     
  3. KrisM

    KrisM

    Joined:
    Oct 18, 2014
    Posts:
    28
    Thanks for the feedback. I'm unsure what normalizing is (will look into it), and wasn't aware you could simply add up the vectors (which is interesting in itself... but logical when I think about it).

    If this works as I'm thinking, it should be relatively simple to sort out. Thanks for the help.