# How do I make an intelligent homing missile (w/bullet beha.)

Get help using Construct 2

### » Fri Sep 30, 2016 2:55 pm

The background:
In my current little project, I created a rocket (with the bullet behaviour) and a moving target.
The rocket (obviously) has a specific, constant speed and the target is moving horizontally by using the 'Sine' behaviour.

I now want to implement an option to make the rocket act as if it was a homing missile.
This means that I want the rocket to head towards the targets direction, considering the own speed and angle and the targets movement direction and speed (though it isn't constant).

I don't want a ridiculously long function, but it should not just be
Code: Select all
`Set Self.Angle towards position Target.X, Target.Y`

My thoughts:
Initially, I used exactly that action which (of course) didn't have a high success rate, since the target is moving.

So I considered giving the rocket the 'Turret' behaviour, setting it to 'Predicted aim' and control the angle by that.
That worked quite well.
I can even control the rockets accuracy with the turret's turning speed and the range.

My question is now whether there is any way to control the rocket which works even better than that?
I just started using C2 (two weeks ago) and I know that this is quite ambitious, but I wanna see where I can get and I know that this awesome community is always up for a challenge. ;)

Here is an image of my current function:
http://i.imgur.com/gHmy7Gd.png (sorry can't make it clickable yet)

Have a nice day, thanks for any reply! :)
B
27
S
11
G
8
Posts: 528
Reputation: 7,091

### » Fri Sep 30, 2016 3:36 pm

anglelerp(self.angle,angle(self.x,self.y,target.x,target.y,turningspeed*dt)
B
175
S
50
G
199
Posts: 8,622
Reputation: 124,020

### » Fri Sep 30, 2016 7:18 pm

@newt

You probably meant "anglelerp(self.angle,angle(self.x,self.y,target.x,target.y),turningspeed*dt)" right?
Do you take the turning speed from the turret behaviour?
Like this? http://i.imgur.com/DLnW3TE.png

The setting I have now results in some pretty heavy jittering of the bullet (I would upload a gif but I'm in a remote location atm and the internet is via satellite and thus, really slow).
"We can't solve problems by using the same kind of thinking we used when we created them."
- Albert Einstein
B
27
S
11
G
8
Posts: 528
Reputation: 7,091

### » Fri Sep 30, 2016 7:47 pm

Another simple idea to try, depending on the desired effect:

Set Angle of Motion to: angle(missile.x, missile.y, target.x, target.y) > missile.angle ? missile.angle + x : missile.angle - x

Replace X with any number or variable based on how much of a turning radius you want.

Simply, this is comparing the angle of the missile's movement and angle between missile and target and closing the gap between them. Of course if they are exactly equal I guess it will zig-zag or waiver a bit.
B
46
S
27
G
11
Posts: 535
Reputation: 10,694

### » Fri Sep 30, 2016 7:52 pm

I just found this thread: https://www.scirra.com/forum/viewtopic.php?t=68116&start=0

Wonder why I didn't find that through the search function...

I'm sorry for repeating this question, I'll try your solution @badmoodtyler and try the solutions from the thread.

[EDIT: I tried your solution @badmoodtyler, but it keeps turning in a counter-clockwise direction, so it always chooses the second option which actually can't be possible.. :(

I'd love to send you a video but my internet won't allow it..]
Last edited by randomly on Fri Sep 30, 2016 8:04 pm, edited 1 time in total.
B
27
S
11
G
8
Posts: 528
Reputation: 7,091

### » Fri Sep 30, 2016 7:54 pm

Turningspeed is whatever you want it to be.
B
175
S
50
G
199
Posts: 8,622
Reputation: 124,020

### » Fri Sep 30, 2016 8:12 pm

@newt Thanks, that works perfectly. I thought you meant some kind of behaviour expression.
What does the "dt" do again? I understand what the "dt" expression does on its own, but what does it do in this expression?

Does it ensure that the missile just turns when a frame is being rendered and not every tick?
"We can't solve problems by using the same kind of thinking we used when we created them."
- Albert Einstein
B
27
S
11
G
8
Posts: 528
Reputation: 7,091

### » Fri Sep 30, 2016 8:20 pm

It's the time since the last tick.
It makes the movement frame rate dependant.
If the frame rate slows down, so does the turning speed.
B
175
S
50
G
199
Posts: 8,622
Reputation: 124,020

### » Fri Sep 30, 2016 8:38 pm

randomly wrote:I just found this thread: viewtopic.php?t=68116&start=0

Wonder why I didn't find that through the search function...

I'm sorry for repeating this question, I'll try your solution @badmoodtyler and try the solutions from the thread.

[EDIT: I tried your solution @badmoodtyler, but it keeps turning in a counter-clockwise direction, so it always chooses the second option which actually can't be possible..

I'd love to send you a video but my internet won't allow it..]

One of the angles is probably a negative value which will mess up that equation. I really don't get why it returns or even allows for negative angles— if the angle is -10 why doesn't it just return 350?

So if you can compensate for that it should fix that, I think, but obviously I don't have a full grasp on this
B
46
S
27
G
11
Posts: 535
Reputation: 10,694