Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

Nick George
Ranch Hand
Posts: 815
I'll do my part to the new forum by posting the question I'm currently working on:

{velocities are "per loop" of the game}

Consider a target (T) with a position (xT,yT), and a velocity (deltaXT,deltaYT). Now consider a rotating gun turret (G) with a position (xG,yG), and an orientation theta, in degrees. Said gun turret fires a bullet (B) originating from (xG,yG) (or very close to it), with the velocity in radians (s,theta) changed into rectangular, where 's' is a given magnitude, and theta is the above-mentioned orientation of the GunShip. All coords can be changed to radians and back to rectangular with great ease.

given (x1,y1), (deltaX1,deltaY1), (x2,y2), and s, solve for theta such that the bullet will hit the target.

Note that finding (deltaXB,deltaYB) is not an issue, for once theta is found, it is simply (s,theta) in rectangular.

The game I've been working on has involved endless math and equation-making, and if this forum can serve as a place to bounce ideas around with people way smarter than me, it may be a great addition to the ranch.

-Joe

[Clarification]
[Emboldened main question, so as to draw attention to its significance. Kna'mean?]
[ July 31, 2004: Message edited by: Joseph George ]

David Weitzman
Ranch Hand
Posts: 1365
The projectile and target both need to travel different distances at different speeds, but it takes them each the same amount of time to cover the distance. Since speed is distance over time, and time is distance over speed, we set the times equal and get this:

Distance Turret / Speed Turret = Distance Target / Speed Target

Distance Turret / Distance Target = Speed Turret / Speed Target

Since we know the speeds, we know this:

The initial distance between the Turret and Target, and the ratio of the distances Turret-Crash and Target-Crash. We also know the measure of angle Turret-Target-Crash. Let's pause to rephrase this without unnecessary infO:

We have a triangle ABC, we know the length of AB, the measure of B, and the value of AC/BC.

The law of sines tells us that AC/sin(b)=BC/sin(a). Thus sin(a)=sin(b)*BC/AC.

We know sin(b) since B is the angle formed by drawing a line from the turret to the target and then in the direction the target is moving. We know BC/AC since that's the ratio of the distances Target-Crash/Turret-Crash. It's just a matter of calculating A, the angle to fire at (where 0 means firing straight at the target, not necessarily due east). I've glossed over the conversion between absolute angles and angles in the triangle formed by the significant points, but here's some code that does the full calculation:

(Edited code. Behavior is identical but removed the laziness where playerToCrashOverTurretToCrash was misnamed)
[ August 01, 2004: Message edited by: David Weitzman ]

Nick George
Ranch Hand
Posts: 815
Wow. It would be tough to imagine a more helpful and thourough answer. I was able to implement your triangle comparatively quickly. I am very grateful. Now, it is 4:30 AM here, and time for the ol' Joe to go to bed. Thanks.