1. Hey! Guest! The 33rd GMC Jam will take place between May 23rd, 12:00 UTC (Friday noon) and May 27th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GM:S 1.4 [SOLVED] Flying in formation, speed problem

Discussion in 'Programming' started by Nathan Laing, May 11, 2019.

  1. Nathan Laing

    Nathan Laing Member

    Joined:
    Jul 31, 2016
    Posts:
    353
    Greetings,

    I have coded a flight formation system. 5 wing men can follow a wing leader around in up to 14 different formations. It works ok, however, there is one problem I am having great difficulty fixing which is best described with the attached image;

    When the wing leader turns on an angle, the aircraft on the inside of the arc "bump" or "glitch" around, because they try to get to their formation point which becomes behind them instead of in front of them (when the wing leader turns).

    Clearly, to maintain formation, the ships on the inside of the arc need to reduce their speed during a turn, and I am using basic logic with arbitrary values for a slower speed which helps, but it's just not cutting it. I either need "correct' math, or perhaps, I need a way to get them to temporarily ignore their usual flight instructions and maybe smoothly lerp to the new position.

    Desperate for any assistance!

    Code:
    x += dcos(image_angle) * speed;
    y -= dsin(image_angle) * speed;
    

    [​IMG]
     
  2. TinyGamesLab

    TinyGamesLab Member

    Joined:
    Jun 21, 2018
    Posts:
    134
    Imagine the plane has a position and a speed vector (vspeed and hspeed). It also has a target position which is determined by the lead plane. You can then add the speed vector to the position difference vector and you should get the correction needed.
    If you feel like it, you could try to use some flocking logic behind your code. It will not be as smooth as hard coding the movement but I bet it will be a lot more fun!
    Take a look at this article:
    https://gamedevelopment.tutsplus.co...ignment-cohesion-and-separation--gamedev-3444
     
    hippyman likes this.
  3. Nathan Laing

    Nathan Laing Member

    Joined:
    Jul 31, 2016
    Posts:
    353
    Thanks for writing in @TinyGamesLab!
    Flocking is indeed interesting, I remember seeing a demo of it in Gamemaker a while back. I took a look at the page you provided and searched for some other similar ones also. "Alignment/Separation/Cohesion" are recurring themes. In my case, this time, I am more interested in a hard-coded solution.

    What does this look like in the form of an equation?
    Each wing man has a target x and y, and I could find the difference between the wing man's current x and y against it's corresponding target coordinates, but specifically, how may I add these differences to speed?
     
  4. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,695
    Do notice if you start using steering behaviors: the standard implementation lacks one thing which is facing (direction) or rather, the agent's facing equals the direction of their velocity vector. This has two implications: 1) agent with zero speed has no facing and 2) you have to be ok with agents turning up to 180 degrees in a single step. If this is not fine, you have to implement facing and turn rate on top of steering behaviors.
     
  5. Nathan Laing

    Nathan Laing Member

    Joined:
    Jul 31, 2016
    Posts:
    353
    It is not fine for the planes to instantly change direction. To this end, I have already implemented a turn speed factor. I need the math to calculate by how much a plane needs to slow down during a turn in order to maintain a position such that the target x/y remains in front of the plane. Perhaps, this is as simple as doing psuedo-code: "if angle difference between 'agent' and target position is negative(anti-clockwise), incrementally reduce speed---else incrementally maintain/increase speed"... This does not provide an exact mathematical answer, but it might visually produce the desired result, although the question remains as to by how much one reduces/increments speed---arbitrary...
     
    Last edited: May 11, 2019
  6. TinyGamesLab

    TinyGamesLab Member

    Joined:
    Jun 21, 2018
    Posts:
    134
    I'll see if I can make a small example in the next couple of days to show what I meant.
     
  7. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    123
    something to take into consideration.. have the lead plane make a softer turn, so the wingmen can accommodate and not crash into eachother... just a thought
     
  8. Nathan Laing

    Nathan Laing Member

    Joined:
    Jul 31, 2016
    Posts:
    353
    I went and took a look at some actual flying formation videos on youtube to see how turns in formation are handled in real life--and the short answer is, they aren't. At least, not in the sense one might have imagined in a video game -- that is, hard turns in tight formation.
    Regardless, in order to create this unrealistic vision of formation flying, I think what needs to happen is that the planes on the inside of the arc need to have their formation coordinates temporarily 'suspended' or adjusted (while they move in the same general direction as the wing leader) until such time as their 'true' formation coordinates can be reached without a sharp turn.
     
  9. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    123
    In a good formation, the wingtips do not overlap each other. The wingman flies slightly behind the lead aircraft, and slightly below as depicted in this graphic:



    [​IMG]


    they can share the X,Y coords because of the Z axis..



    to make your turn the outside needs to speed up/go father and the inside needs to make a tighter turn/slow down..
    example: column right
    [​IMG]

    and then adjust to "get back into formation" quickly
     
  10. Nathan Laing

    Nathan Laing Member

    Joined:
    Jul 31, 2016
    Posts:
    353
    Thank you for posting @woods! That's certainly true about needing to speed up and slow down. As it happens, I do have the planes coded to speed up and slow down, however, it's not fully solving the problem due to the way in which I am setting the planes' target coordinates. This is best explained with an image (below).
    As we can see, when the wing leader turns, (30 degrees in the example) the new target coordinates rotate with the wing leader. Consequently, the planes on the inner arc actually need to fly backwards to reach those coordinates (and they actually do fly back around in a big arc to get there), but this is wrong.
    What I will need to do is set a different set of coordinates to be some distance from the old coordinates (at the same angle) in front of the planes. Some more Lengthdir play...

    [​IMG]
     
  11. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    123
    lil bit of brainstorming...

    could you set a temporary "turn_point" for each of the following wingmen and tail pilot after the lead makes his maneuver? and then have them catch up to get back in formation..

    ex:
    leader turns right 30^ at x,y
    when wingmen get to x-10,y or whatever close enough point, they make the same 30^ turn
    rinse repeat for each plane following
    ..once everyone has turned... speed up to get back to formation

    just a thought ;o)
     
  12. Nathan Laing

    Nathan Laing Member

    Joined:
    Jul 31, 2016
    Posts:
    353
    @woods Yes, I've been on that train of thought and coded a version of that before as well. It sounds good on paper, but I've spent countless hours, more than 8 weeks or so on this, and all my approaches are just not working. In desperation I was wondering if there was a specific math formula, but in hindsight it seems that there wouldn't be one -- I guess I will just need to "hammer on" with more logic gates.
     
  13. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    123
    hmmm... wondering...

    if lead makes a spd(6) turn(30^), inside wingman makes half the turn(15^) and doubles the spd(12), then makes the second half of the turn(15^), then spd(6) is he is the correct position?

    if so, the lead can wait for wingman to catch up before making another 30^ turn... the extended wingman would consider the closer wingman his lead.
     
  14. Nathan Laing

    Nathan Laing Member

    Joined:
    Jul 31, 2016
    Posts:
    353
    Alright, going by the principle "simple is often the best", I cut down on some stuff and I've solved (for the most part) this problem, although the planes on the inside arc wobble a tiny bit after getting back to their regular formation target location -- though I think I could probably iron that out with a little more time.

    Now that the current iteration is quite reasonable, I'll share the relevant code portion, which might help others on the logic:

    Code:
    case "STATE_MAINTAIN_FORMATION":
    {
    // find the direction we need to go to get to the formation position
    var pdir     = point_direction(x, y, formation_x, formation_y);
    
    // calculate the angle difference
    var ad       = angle_difference(image_angle, pdir);
    
    // the distance we must exceed before changing to the "FORM UP" state
    var maintain_threshold = 32 + (sprite_get_width(sprite_index) * image_xscale);
    
    // check for a large difference between the plane's current angle, and the angle of the formation point
    // if it's too large just copy the wing leader's angle, fly in that direction and slow down
    if (abs(ad) >= 50)
    {
    // as the plane is technically in formation, the plane's pointing direction is ok to match the wing leader's
    var pdir_wl  = wingleader_id.image_angle;
    
    // point and fly in that direction
    scr_plane_AI_fly_at_angle(pdir_wl);
    
    // slow down
    var desired_speed = wingleader_id.current_speed * 0.3;
    scr_plane_AI_set_speed(desired_speed);
    }
    // otherwise we will maintain speed with wing leader, point and fly in the direction of the formation point
    else
    {
    var desired_speed = wingleader_id.current_speed;
    scr_plane_AI_set_speed(desired_speed);
    scr_plane_AI_follow_target_point(formation_x, formation_y);
    }
    // actually move the plane
    scr_plane_move();
    
    // if the angle difference isn't too large (i.e. we are not in the process of slowing down and matching wing leader's angle) and we have exceeded the acceptable distance to be considered in formation, go to the form up state
    if (abs(ad) < 50 and distance_to_formation_position > maintain_threshold)
    {
    formation_state = "STATE_FORM_UP";
    }
    }
    break;
    
    

    Thank you everyone and @woods for writing in! Brainstorming and sharing together is what it's all about! ;)
     
    Last edited: May 17, 2019 at 2:42 PM
  15. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    123
    awesome.. glad to see you got it working ;o)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice