Jump to content


Photo

How to make a Heat Seeking missile?


  • Please log in to reply
12 replies to this topic

#1 MMMStudios

MMMStudios

    GMC Member

  • New Member
  • 136 posts

Posted 25 February 2012 - 05:07 PM

I wanted to know how to make a Heat Seeking missile for my Dive Man (MegaMan) game. I certainly know how to make it move towards an object, but I want it to curve. For example, if Dive Man was shooting on the opposite direction of the nearest object, I don't want it to just move directly to it, I want it in a curve (Not really good at describing; sorry). Thanks for all help given, I'm still getting use to GML coding, because I don't think your able to do this in D&D.

Thanks!
  • 0

#2 shledder

shledder

    GMC Member

  • GMC Member
  • 80 posts
  • Version:GM8

Posted 25 February 2012 - 06:05 PM

I would suggest setting the speed of the missile to be constant, or constantly increasing. Set an alarm for how ever "smart" you want the missile to be. When the alarm times out, call a script which checks to see if the direction of the missile is the direction it needs to be going to hit (you'll need some trigonometry and vectors here): if so, do nothing; if not direction += x where x is how fast you want the missile to be able to turn.

Hope this gets you started
  • 0

#3 fluidic ice

fluidic ice

    GMC Member

  • GMC Member
  • 812 posts
  • Version:GM:Studio

Posted 25 February 2012 - 06:08 PM

(you'll need some trigonometry and vectors here)

You wouldn't really need trig or vectors to do this, the code below will curve a missile around to a target slowly to provide a curving feel. To make it curve even more, you could randomize a point to pass through on it's way to the target, like a detour which would increase the curve, or another way would be to initially shoot the missile at an angle that's not in the direction of the target to get it to curve more.

CREATE:
Turn_Speed = 2; //How fast the missile turns.

STEP:
Target_Direction = point_direction(x,y,Obj_Player.x,Obj_Player.y); //The target the missile will go towards.
diff = Target_Direction - image_angle;
while (diff > 180) diff -= 360;
while (diff < -180) diff += 360;
if (abs(diff) < Turn_Speed)
   {
    image_angle = Target_Direction;
    }
 else
     {
     image_angle += (sign(diff) * Turn_Speed);
    }
direction = image_angle;

Edited by fluidic ice, 25 February 2012 - 06:14 PM.

  • 0

#4 GMRescue

GMRescue

    GMC Member

  • GMC Member
  • 125 posts
  • Version:GM8

Posted 26 February 2012 - 11:47 AM

Dear fluidic ice,

A heatseeking missile requires simple GML coding.

If the missile seeks obj_man

If your player is called obj_man (you can modify this name) and you want the player to chase obj_man, than this code might be fitting:

In the step event:

move_towards_point(obj_man.x,obj_man.y,(speed that you want))

Speed that you want might be: 4, 5 or 6.

If there is more than one player


If there are more players and the missile chases the nearest one:

Step event:

move_towards_point(instance_nearest(x,y,obj_man).x,instance_nearest(x,y,obj_man).y,(speed that you want))

It's not that hard, but if you have any questions, PM me or reply to this.


Good luck with your game.

- GMRescue
  • 0

#5 fluidic ice

fluidic ice

    GMC Member

  • GMC Member
  • 812 posts
  • Version:GM:Studio

Posted 26 February 2012 - 12:30 PM

Dear fluidic ice,

A heatseeking missile requires simple GML coding.
- GMRescue

Huh? I wasn't the one asking how to do it if that's what you thought?
I was just showing the code I use to get an object to curve around it it's target. =p
  • 0

#6 GMRescue

GMRescue

    GMC Member

  • GMC Member
  • 125 posts
  • Version:GM8

Posted 27 February 2012 - 05:50 AM


Dear fluidic ice,

A heatseeking missile requires simple GML coding.
- GMRescue

Huh? I wasn't the one asking how to do it if that's what you thought?
I was just showing the code I use to get an object to curve around it it's target. =p

Ok, you can watch a tutorial on YouTube.
  • 0

#7 greep

greep

    Menaces with Spikes

  • GMC Member
  • 2398 posts
  • Version:GM7

Posted 27 February 2012 - 06:24 AM

Fluidic's code is the one that is needed, though. Your missile is 100% accurate, the OP wants something that can take a while to correct it's direction.

Edited by greep, 27 February 2012 - 06:28 AM.

  • 0

#8 MasterOfKings

MasterOfKings

    The True Master

  • GMC Member
  • 4888 posts
  • Version:GM8

Posted 27 February 2012 - 12:47 PM

Slightly smaller version...
if (instance_exists(myTarget)) {
    direction+=(sin(degtorad(point_direction(x,y,myTarget.x,myTarget.y)-direction))*rotSpeed);
    image_angle=direction;
}
myTarget should be assigned to an instance id, and the code shown above should be put into the step event.

rotSpeed is a variable relating to how fast the missile can turn.
  • 0

#9 MMMStudios

MMMStudios

    GMC Member

  • New Member
  • 136 posts

Posted 28 February 2012 - 02:14 AM

Dear fluidic ice,

A heatseeking missile requires simple GML coding.

If the missile seeks obj_man

If your player is called obj_man (you can modify this name) and you want the player to chase obj_man, than this code might be fitting:

In the step event:

move_towards_point(obj_man.x,obj_man.y,(speed that you want))

Speed that you want might be: 4, 5 or 6.

If there is more than one player


If there are more players and the missile chases the nearest one:

Step event:

move_towards_point(instance_nearest(x,y,obj_man).x,instance_nearest(x,y,obj_man).y,(speed that you want))

It's not that hard, but if you have any questions, PM me or reply to this.


Good luck with your game.

- GMRescue


Well, I was asking for one that can curve.

Anyways, I really like the method you gave Fluid Ice. I forgot to state though, that I wanted the missile to also do a loop when the player shoots it from the other side and it aims towards an object behind him, instead of just flipping straight to the object. (Hopefully I'm descriptive enough.)
  • 0

#10 fluidic ice

fluidic ice

    GMC Member

  • GMC Member
  • 812 posts
  • Version:GM:Studio

Posted 28 February 2012 - 02:44 AM

Do you mean if the player has their back to the target? If so the missile should already do a 180 over time and come back to hit the target?
If not are you able to draw a quick sketch in paint about what sort of movement you were going for?
  • 0

#11 MasterOfKings

MasterOfKings

    The True Master

  • GMC Member
  • 4888 posts
  • Version:GM8

Posted 28 February 2012 - 03:34 AM

Did you even read my code?

Yes, it does the same as fluidic ice's, but it's shorter and more efficient.
  • 0

#12 MMMStudios

MMMStudios

    GMC Member

  • New Member
  • 136 posts

Posted 28 February 2012 - 07:44 PM

Did you even read my code?

Yes, it does the same as fluidic ice's, but it's shorter and more efficient.


I read your code, but I already saw Fluid Ice's code so I didn't really see why I should take time to switch codes just for efficiency.

Anyways, yes Fluid Ice that's exactly what I want the player to be able to do. If he has his back to the object I want the missile to do a 180 loop instead of just flipping.
  • 0

#13 itsosmpl

itsosmpl

    GMC Member

  • New Member
  • 7 posts
  • Version:GM8

Posted 02 May 2012 - 09:20 PM

Slightly smaller version...

if (instance_exists(myTarget)) {
    direction+=(sin(degtorad(point_direction(x,y,myTarget.x,myTarget.y)-direction))*rotSpeed);
    image_angle=direction;
}
myTarget should be assigned to an instance id, and the code shown above should be put into the step event.

rotSpeed is a variable relating to how fast the missile can turn.


i used this one and it works perfect, thanks allot!
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users