# Calculate the angle to fire to hit enemy

0 favourites
• 34 posts
From the Asset Store
🙌 Cinema Impact Hit Sound Pack comes with 260 high-quality sound effect
• sqiddster - tried that with closest family member (sounds odd). The aiming process worked as normal, but it remained targeting the first sprite, no matter which one was closest. Should I be using a distance condition instead?

https://dl.dropbox.com/u/50465867/Construct/Targetting.capx

*SCRATCH THAT* me being stupid! Missed the positioning section at the top. Also changed it so that it rotates towards, rather than pops direction. Brilliant formula Sqiddster!

• AnD4D Cool. I would indeed recommend a rotate towards so that it doesn't shift angle instantaneously.

• I adapted the algorithm Ashley came up with here for C2

I also modified the comments for the smart aiming formula to make the explanations slightly easier to understand (although it's still complex math)

aiming.capx

Ah it ends up behing exactly equivalent to what squid came up with.

since

asin(targetSpeed * sin(angle(target,turret) - targetMovingAngle)

= asin (((targetX-turretX)*targetVectorY - (targetY-turretY)*targetVectorX)/distance(target,turret))Yann2013-01-15 14:48:05

• Wow, Yann! That's impressive stuff! I was trying to think of ways of making sqiddster's code less accurate so the AI isn't such a brilliant shot all the time. Definately appreciated :) Thank you!

• That's some comment. My brain just melted with all the trig.

• In terms of making the AI less accurate, I needed this in my case, because I have ships firing several lasers at once. Obviously if they all just fire at the precise angle, they'll overlap each other and just appear as one laser, which isn't what I was after. But the simplest answer for me was to just add a random angle to the result, somewhere between -5 and +5, so give a nice spread of lasers.

• Thanks for that capx Yann. The amount of comments are very useful for helping to understand how this works. To be honest, in my case, I'm fairly happy with just pasting the code into my code, all that math hurts my brain. But it's good to know it's doing exactly what I wanted :)

• Another bit that would make a difference as well in stopping the AI from being quite so good is the bullet speed itself. If it is too much faster than the player movment speed, there isn't going to be much they can do to avoid being hit.

So throwing off the accuracy a bit will help much, but you may also want to toy with bullet speeds to make sure it still feels fun in terms of having a chance to dodge bullets that way a good player can still have the rush of living on the edge and flying as close as they dare to the path of the enemy bullets. Just a thought, I know it doesn't help in terms of the overall objective of predictive targeting, just more along the lines of fun factor.

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• You're right, in my game at least that's becoming a factor. The enemy ships are faster than the player and the bullets are faster too, making them hard to dodge (impossible if the enemy gets right ontop of the player). But if I make them too slow, it's too easy to just run away. Difficult to balance, but hopefully I'll find the right balance with endless tweaking :)

• Hello, back again.

I got the system working properly with the targeting, however, I then thought about having the enemy able to target the weapons on the ship to disable them.

This caused an issue in the sense that the turrets do not actually have their own speed or moving angle, as they are pinned, or simply always set to a certain position of a moving object, which is then moving around on its own. I considered having a variable on the item which reads off of the moving sprites speed/moving angle, and then feeding that into the turrets. However, if the sprite is rotating, the turret speed is increased even more.

You can move the blue targets around using the arrow keys, and you should see that they are always being hit... however, if you move them right off screen, the pink target is very rarely hit.

Is there a way to improve this using method, as it only uses one variable (at the moment) and only a couple of lines.

Would be greatly appreciated.

• I'll have a look at the capx when I get home. If I don't reply it means I've forgotten so just sqiddster me again :P

• I've still not had any luck - you sqiddster?

I've made it less accurate, but not smooth (it's very shaky). I'll figure that out next :)

• Dang, I forgot. I'll sqiddster myself so I remember.

• Does the problem lie in the revolving object? Because you'll need a new and more complicated algorithm to deal with revolution!

• sqiddster The idea was to have a space ship that could move around freely. The player ship's turrets would shoot the enemy weapons if it got within a certain range. I was thinking of basing it on vector rather than speed, as the turrets didn't have movement (so to speak) even though they do move.

I've tried it with Ashley / code, but they all seemed to aim *dumb*.

If the ships were just moving I could base the speed, etc off of the ship... but as you said, it's the rotation that seems to mess things up.

This one got us beat? <img src="smileys/smiley11.gif" border="0" align="middle" />

• 34 posts