# Calculate the angle to fire to hit enemy

Get help using Construct 2

### » Mon Dec 10, 2012 11:58 pm

I've read up on various topics on this but they all seem to include strange mathematical symbols and are coded in other languages. Can anybody explain in plain English (or a Capx) how this is achieved?

If I have a stationary object which needs to fire at a moving object (both seen from directly above), how do I calculate the angle to fire the bullet at? I know the speed of the bullet and the enemy is moving at a constant speed.
B
26
S
6
G
4
Posts: 456
Reputation: 5,858

### » Tue Dec 11, 2012 4:13 am

Have an invisible sprite that is constantly attached to the shooting sprite, and also setting its angle towards the target every tick. Then when you need to shoot, just get that object to create the bullet and its angle will be automatically set the same as the invisible sprite, which is in turn pointed directly at the target.
B
48
S
15
G
7
Posts: 603
Reputation: 8,246

### » Tue Dec 11, 2012 4:41 am

This is the method I use:

Bullet: on created > Set angle: *angle(bullet.x, bullet.y, enemy.x, enemy.y)*

When setting the angle, just copy-and-paste the section between the *s, then change the names from bullet and enemy to whatever you have named your bullet and enemy.
B
28
S
8
G
5
Posts: 429
Reputation: 7,007

### » Tue Dec 11, 2012 9:30 am

Sorry, I didn't describe it properly. What I meant was, the object is moving so the bullet needs to be fired ahead of the object, otherwise the object moves along it's course and the bullet drops behind. 'Deflection shooting' I think they call it. I need to calculate where to fire the bullet ahead of the object so that they'll collide.
B
26
S
6
G
4
Posts: 456
Reputation: 5,858

### » Tue Dec 11, 2012 9:36 am

It looks like you'll have to understand that "complicated math" if you want a complicated prediction. It's like throwing a basketball in a moving hoop: you see how fast it's going, predict where it will be, and throw.

In terms of C2, a simple solution would be to create an empty sprite where the object will be, angle the bullet towards it, and fire. If the bullet is somewhat slow, then speed complicates the matter further.

Good luck getting a working sample.
B
19
S
5
G
1
Posts: 104
Reputation: 2,920

### » Tue Dec 11, 2012 10:22 am

There's a fairly simple solution which isn't perfect, which is to work out the distance between the firing object and the target object, divide that by 6 or 7 (will vary depending on the bullet speed), then add that distance infront of the object in the angle it's travelling, and fire towards that point. That does give fairly decent results but it's not perfect and only works with set bullet speeds.

I'll have to go back and work out what the hell a quaternion is and how to translate that to C2.
B
26
S
6
G
4
Posts: 456
Reputation: 5,858

### » Tue Dec 11, 2012 11:30 am

Great, you got me all mathexcited. Unfortunately I don't have time right now to work out the equations for you, but I'll see what I can do later ;)
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,739

### » Tue Dec 11, 2012 1:27 pm

Science!

It works rather well, if I may say so ;)
sqiddster2012-12-11 13:27:30
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,739

### » Tue Dec 11, 2012 1:35 pm

Great Sample
B
65
S
9
G
7
Posts: 176
Reputation: 8,924

### » Tue Dec 11, 2012 3:01 pm

Wow, excellent stuff, and it's all contained on a single line! Now that'll save me a lot of headaches, thanks :)
B
26
S
6
G
4
Posts: 456
Reputation: 5,858

Next

### Who is online

Users browsing this forum: Google [Bot] and 23 guests