Legacy GM Homing Missile programing

Z

Ziliock

Guest
How can I change gradually the direction of an object to make it pursue another object?
Thanks by the way
 
J

Jordan Robinson

Guest
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.
 
W

whale_cancer

Guest
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)
 
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:
Z

Ziliock

Guest
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)
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
 

Yal

šŸ§ *penguin noises*
GMC Elder
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.
 
Z

Ziliock

Guest
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.
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
 
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.
 
B

bojack29

Guest
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 :)
 
Z

Ziliock

Guest
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 :)
Just I put that in the write code's action?
In the create evente or step?
 
Z

Ziliock

Guest
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 :)
Thanks you!How can I slow the speed?
 
B

bojack29

Guest
Code:
speed = point_distance(x, y, target.x, target.y) / slowdownSpeed;
 
Z

Ziliock

Guest
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 :)
Sorry it was a problem with the object who is partent of it
 
Top