GM:S 1.4 Homing Missile programing

Discussion in 'Programming' started by Ziliock, Jun 29, 2016.

Tags:
  1. Ziliock

    Ziliock Guest

    How can I change gradually the direction of an object to make it pursue another object?
    Thanks by the way
     
  2. Jordan Robinson

    Jordan Robinson Member

    Joined:
    Jun 22, 2016
    Posts:
    86
    Here's a script I wrote a while back which allows an instance to rotate smoothly. You can adjust the maximum allowed speed of rotation and damping:

    Code:
    /*  This script will smoothly rotate the calling instance to a specified point.
    
        Arguments:
            0 = target x position
            1 = target y position
            2 = maximum rotate speed (must be positive)
            3 = a damping factor (acceleration) as a value between 0 and 1*/
    
    var dir, spd, dmp, rot;
    
    dir = point_direction(x, y, argument0, argument1);
    spd = max(argument2, 0);
    dmp = clamp(argument3, 0, 1);
    
    image_angle -= clamp(angle_difference(image_angle, dir) * dmp, -spd, spd); //Smoothly rotate to aim position.
     
  3. whale_cancer

    whale_cancer Member

    Joined:
    Jun 29, 2016
    Posts:
    399
    Get the direction of your object by using point_direction(x1, y1, x2, y2) (1 is your pursuing object, 2 is your object being pursued)
    Get the direction your missile is moving by using 'direction'
    Check to see if these are not the same value
    In the step event of the missile, If missiles direction and the direction to the object it is homing are not the same value, adjust your missile's direction to be closer to the value found using point_direction (however much you adjust it will essentially be its turning rates per frame measured in degrees)
     
    Ziliock likes this.
  4. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,088
    Here's a method that should cause a homing missile to do the least ammount of turning necessary in order to intercept the target. You'll need to initialize "old_dir_to_target", as well as "turn_rate" in the missile's create event.

    Note: this code will fail if the missile and target are moving exactly parallel and at the same speed. (pretty unlikely). Also note that this code cannot handle angular acceleration, the missile must always turn at diff_an or at its max turn rate, whichever is lower.

    What this code does is compare the direction from the missile to the target, to that same direction computed during the previous step. Then it tries to turn the missile in such a way that it cancels out that difference, setting the missile on an intercept course in the process. I think this is called "proportional navigation".


    Watch out though, this missile will be extremely accurate!

    Code:
            var dir_to_target = point_direction(x,y,target.x,target.y);
            var diff_an = angle_difference(dir_to_target,old_dir_to_target);
            direction += min(turn_rate,abs(diff_an))*sign(diff_an);
            old_dir_to_target = dir_to_target;
    
    EDIT: corrected error in calculation. Added abs() function
     
    Last edited: Jun 29, 2016
    Leif likes this.
  5. Ziliock

    Ziliock Guest

    Let me see.I think I'm use (x1, y1, x2, y2) like:
    if x is smaller then 0 = x+1
    You know something abount Drag'n Drop?
    If you don't know please try to be a little more specific
     
  6. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,460
    You can get pretty okay homing like this; I use it for flying enemies a lot. (Except with code, but you don't seem to like/appreciate that, so here's how to do it with DND)

    If y is smaller than player y,
    add 0.2 to vertical speed.
    else
    subtract 0.2 from vertical speed.
    If x is smaller than player x,
    add 0.2 to horizontal speed.
    else
    subtract 0.2 from horizontal speed.
     
    nujumkey and Ziliock like this.
  7. Ziliock

    Ziliock Guest

    Thanks a lot you know?But I'm alrready know that formula.
    And it tend to miss a lot.Is perfect for enemies but stressful when the player uses it
     
  8. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,088
    Just moving toward the target will produce orbiting behavoir (meaning it will miss a lot).

    Check out the code I posted above, it won't orbit, it's really accurate, but it is still conceivable that the missile may be dodged.
     
  9. nujumkey

    nujumkey Guest

  10. bojack29

    bojack29 Member

    Joined:
    Jun 21, 2016
    Posts:
    407
    try this for the missile

    Code:
    var a = point_direction(x, y, target.x, target.y);
    direction += sign(dsin(a - direction)) * 5;
    image_angle = direction;
    
    That should work :)
     
  11. Ziliock

    Ziliock Guest

    Just I put that in the write code's action?
    In the create evente or step?
     
  12. obsidiannovels

    obsidiannovels Member

    Joined:
    Jun 20, 2016
    Posts:
    242
  13. bojack29

    bojack29 Member

    Joined:
    Jun 21, 2016
    Posts:
    407
  14. Ziliock

    Ziliock Guest

    Thanks you!How can I slow the speed?
     
  15. bojack29

    bojack29 Member

    Joined:
    Jun 21, 2016
    Posts:
    407
    Code:
    speed = point_distance(x, y, target.x, target.y) / slowdownSpeed;
    
     
  16. Ziliock

    Ziliock Guest

    Sorry it was a problem with the object who is partent of it
     
  17. bojack29

    bojack29 Member

    Joined:
    Jun 21, 2016
    Posts:
    407
    That code doesnt work?
     
  18. Snail Man

    Snail Man Member

    Joined:
    Jun 20, 2016
    Posts:
    282
    Check out this smooth rotation script: I use it all the time in my games
     
  19. Ziliock

    Ziliock Guest

    It works perfectly.Thanks a lot
     

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