![]() ![]() That is, do a transformation on the enemy's velocity and trajectory to reflect how the enemy is moving in relation to you. Things get even more involved when you and the enemy are both moving, although it's possible to construct the math so that you "hold yourself still". If you can't rotate your weapon instantaneously, then the problem becomes more difficult because the time it takes to rotate your weapon to point at the enemy depends on how fast and in what direction the enemy is traveling. This typically requires successive approximations. The key, then, is to find the point on the enemy's path that, were you to fire the projectile immediately, it would intersect the enemy at the proper time. So if you draw a line from your current position to intercept any position on the enemy's expected path of travel, you can determine how long it will take your projectile to hit the enemy. You know how fast your projectile can travel. ![]() If you know where you and the enemy are, and the speed and direction of the enemy, then you can determine the enemy's position (and, consequently, his distance and direction from you) at any time. To simplify, we'll assume that you can aim your weapon instantaneously. This is a surprisingly difficult problem. Your enemy is moving in a constant direction at a constant velocity. Once you write code that can do the above, move on to: Use your favorite search engine to find a description of how to find the angle between two points on a plane (you did say 2D). You need to compute the angle between you and your enemy, rotate your weapon to point at the enemy, and then fire. I'll start with the simplest solution and expand on that.įirst, assume that both you and your enemy are stationary. (Sketch of algorithm: Can you fire at time 0? No. Here's where you probably want to use successive approximation to find it. (Basically: work out the necessary firing angle, if any, for a shot at time r, as described above, rotate to that angle, stop, wait until time r, then fire.)īut you probably want to know the earliest time at which the shooter can fire. Suppose that the shooter can complete a half rotation in time r. The firing angle is still ∠(A − B + t(VA − VB)). What if the shooter waits until time u before firing? Then the bullet hits the target when |A − B + t(VA − VB)| = ( t − u) s. In this case the firing angle is ∠(A − B + t (VA − VB)). It's still a straightforward quadratic equation to work out the time to hit: |A − B + t(VA − VB)| = t s. the bullet velocity is added to the shooter's velocity.) (I'm supposing Newtonian relativity here, i.e. Now suppose that the shooter is not stationary but has constant velocity VB. Having determined t, you can now work out the firing angle, which is just ∠(A − B + t VA). This is a straightforward quadratic equation in t, which you should be easily able to solve (or determine that there is no solution). The bullet hits at time t such that |A − B + t VA| = t s. Let the target be at the position A and moving with velocity VA, and the shooter be stationary at the position B and can fire bullets with speed s. The simplest case is a stationary shooter which can rotate instantly. (Which you can compute with the function atan2 in many languages.) Notation: I write vectors in capital letters, scalars in lower case, and ∠V for the angle that the vector V makes with the x-axis. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |